#!/bin/sh -e
#
# Download and install the latest Munki release

cat <<EOF

********************************
** Munki / Sal auto-installer **
********************************

EOF
if [ "$(id -u)" -ne 0 ]; then
    echo "Please enter your sudo password when prompted"
    echo ""
fi

echo "Grabbing the latest munki version..."
curl --silent https://api.github.com/repos/munki/munki/releases/latest | grep browser_download_url | cut -c 31- | xargs curl -Lo munki.pkg
installer -pkg munki.pkg -target /
sudo defaults write /Library/Preferences/ManagedInstalls SoftwareRepoURL "https://munki.iff.kfa-juelich.de"
sudo defaults write /Library/Preferences/ManagedInstalls ClientIdentifier "default"
sudo defaults write /Library/Preferences/ManagedInstalls InstallAppleSoftwareUpdates -bool False
sudo defaults write /Library/Preferences/ManagedInstalls ClientResourceURL -string "https://munki.iff.kfa-juelich.de/client_resources"
sudo defaults write /Library/Preferences/ManagedInstalls ClientResourceFileName -string "site_default.zip"
sudo defaults write /Library/Preferences/ManagedInstalls IconURL -string "https://munki.iff.kfa-juelich.de/icons"
rm munki.pkg

echo "Grabbing the latest sal version..."
curl --silent https://api.github.com/repos/salopensource/sal-scripts/releases/latest | grep browser_download_url | cut -c 31- | xargs curl -Lo sal_scripts.pkg
installer -pkg sal_scripts.pkg -target /
defaults write /Library/Preferences/com.github.salopensource.sal ServerURL https://sal.iff.kfa-juelich.de
defaults write /Library/Preferences/com.github.salopensource.sal key jzt84ext6dcd97o5jka01laiswldaz52udwb80crio5zncz8r0sik7syjkbm87617csyflun2f3yxcpmihfg720ccrmjxqk83r5isjdtun8zc5t0azv3o8wozmgkyuz6
rm sal_scripts.pkg