#!/bin/bash
# Script to install SoftwareUpdateCheck.sh
# 2011-04-26 / Peter Möller, Datavetenskap, LTH
# 2011-05-05: checksum-check
# 2011-12-07: vastly improved output when running
# Location: 
# http://fileadmin.cs.lth.se/cs/Personal/Peter_Moller/scripts/SoftUpdCheck_install.sh


# Make sure the user is "root"
if [ ! "$USER" = "root" ] ; then
  echo "Must be run by root!"
  echo "Exiting..."
  exit 1
fi
# ScriptName is the name of the script
ScriptName="SoftwareUpdateCheck.sh"

# Inform the user of what is going to happen
printf "\e[1;4mInstallation of \"$ScriptName\":\e[0m\n"
echo "This will install \"$ScriptName\" on your computer."
echo
echo "The home page for the project is located here:"
echo "http://cs.lth.se/kontakt/peter_moller/script/softwareupdatechecksh_en"
echo "There you will find more information about the script and also how to remove it."
echo
echo "Now the script will be downloaded and installed:"

# Fetch and launch the launchd-component
printf "1. Fetching the launchd-component (runs the script every 15 minutes)..."
curl -o /tmp/se.lth.cs.softwareupdatecheck.plist http://fileadmin.cs.lth.se/cs/Personal/Peter_Moller/scripts/se.lth.cs.softwareupdatecheck.plist 2>/dev/null
curl -o /tmp/se.lth.cs.softwareupdatecheck.plist.sha1 http://fileadmin.cs.lth.se/cs/Personal/Peter_Moller/scripts/se.lth.cs.softwareupdatecheck.plist.sha1 2>/dev/null
if [ "$(openssl sha1 /tmp/se.lth.cs.softwareupdatecheck.plist | awk '{ print $2 }')" = "$(less /tmp/se.lth.cs.softwareupdatecheck.plist.sha1)" ]; then
  mv /tmp/se.lth.cs.softwareupdatecheck.plist /Library/LaunchDaemons/se.lth.cs.softwareupdatecheck.plist
  chmod 644 /Library/LaunchDaemons/se.lth.cs.softwareupdatecheck.plist
else
  echo "Checksum of \"se.lth.cs.softwareupdatecheck.plist\" does NOT match!! Installation aborted!"
  exit 1
fi
launchctl load /Library/LaunchDaemons/se.lth.cs.softwareupdatecheck.plist
launchctl start se.lth.cs.softwareupdatecheck
printf " done!\n"

# Fetch the script
printf "2. Fetching the main script..."
curl -o /tmp/${ScriptName} http://fileadmin.cs.lth.se/cs/Personal/Peter_Moller/scripts/${ScriptName} 2>/dev/null
curl -o /tmp/${ScriptName}.sha1 http://fileadmin.cs.lth.se/cs/Personal/Peter_Moller/scripts/${ScriptName}.sha1 2>/dev/null
if [ "$(openssl sha1 /tmp/${ScriptName} | awk '{ print $2 }')" = "$(less /tmp/${ScriptName}.sha1)" ]; then
  mv /tmp/${ScriptName} /usr/bin/${ScriptName}
  chmod 755 /usr/bin/${ScriptName}
else
  echo "Checksum of \"SoftwareUpdateCheck.sh\" does NOT match!! Installation aborted!"
  exit 1
fi
printf " done!\n"

# Send a signal that someone has installed the script
# This is only to give me feedback that someone is actually using this
# I will *not* use the data in any way or give it away!
curl -s -f -e "$ScriptName" -o /dev/null http://fileadmin.cs.lth.se/cs/Personal/Peter_Moller/scripts/installed 2>/dev/null

echo "Done installing base parts of \"$ScriptName\". Now proceeding to install GeekTool"
echo "(GeekTool is not a part of \"$ScriptName\"; it is used to display the output of the script"
echo "on the desktop of your computer. You may skip it if you like)."
echo

# Getting GeekTool
SW_VERS="$(sw_vers -productVersion | cut -d\. -f2)"
if [ -z "$(ls /Library/PreferencePanes/GeekTool.prefPane 2>/dev/null)" -a -z "$(ls /Users/*/Library/PreferencePanes/GeekTool.prefPane 2>/dev/null)" -a -z "$(ls /Applications/GeekTool.app 2>/dev/null)" ]; then
  if [ "$SW_VERS" = "6" ]; then
    echo "Fetching GeekTool for Snow Leopard"
    curl -o /tmp/GeekTool.dmg http://update.tynsoe.org/geektool3/Public/GeekTool%203.0.dmg 2>/dev/null
    hdiutil mount /tmp/GeekTool.dmg
    open /Volumes/GeekTool\ 3/
    echo "GeekTool has been downloaded, but you must install it yourself!"
  elif [ -z "$(echo ${SW_VERS/[78]/})" ]; then
    echo "With Mac OS X 10.7 Lion and newer, the \"old\" GeekTool no longer work correctly. A new version is"
    echo "available in the Mac App Store:"
    echo "http://itunes.apple.com/se/app/geektool/id456877552?mt=12"
    echo "Opening this address in a few seconds..."
    sleep 5
    open "http://itunes.apple.com/se/app/geektool/id456877552?mt=12" &
  else
    echo "Sorry: you are running a version of Mac OS X that will not work with GeekTool."
    echo "You will have to find a version for yourself..."
  fi
else
  echo "It looks as if you already have GeekTool installed."
fi

printf "\e[1;4mDone!\e[0m\n"
echo "\"$ScriptName\" is now running and will look for new softwareupdates every 6 houres."
echo "Run the script as an ordinary user to see the data."
echo
printf "\e[1;3mLocations:\e[0m\n"
echo "The script is located in \"/usr/bin\""
echo "The data file is \"/tmp/swu.temp\""
echo "The launchd-file is \"/Library/LaunchDaemons/se.lth.cs.softwareupdatecheck.plist\""
echo "These are the files you need to remove to uninstall!"
echo
echo "The script will automatically look for, and install, updates every two weeks, but you may upgrade"
echo "manually through \"$ScriptName -u\" if you like!"
echo "Also, please feel free to report bugs and email suggestions for improvements!"
echo "Thank you!"
echo "Peter Möller, Department of Computer Science, Lund University, Lund, Sweden"

exit 0