Skip to content

Instantly share code, notes, and snippets.

@krispayne
Last active August 10, 2016 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krispayne/79aa5b65661ceee0880d4277f2f04636 to your computer and use it in GitHub Desktop.
Save krispayne/79aa5b65661ceee0880d4277f2f04636 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Displays a dialog for the user prompting them to restart after a software update
# Variables
# CocoaDialog location
CD="/Applications/Utilities/cocoaDialog.app/Contents/MacOS/CocoaDialog"
# Get the logged in user
loggedInUser=$(ls -l /dev/console|cut -d' ' -f4)
runPrompt() {
# CocoaDialog message box asking for the user to select restart
restartDialogPrompt=$(${CD} msgbox --title "Restart Please" \
--text "An important update has been installed and requires a restart" \
--icon-file "/usr/local/assets/rh.png" \
--informative-text "It is important that you select Restart, but you may select to restart at a more convenient time." \
--no-newline --button1 "Restart Now" --button2 "Give me 5 Minutes" --button3 "Give me 20 Minutes" --float)
# If the user selects restart, then `shutdown -r now`. Else, display timer.
if [ "${restartDialogPrompt}" == "1" ]; then
echo "User selected Restart"
/sbin/shutdown -r now
exit
elif [ "${restartDialogPrompt}" == "2" ]; then
echo "User selected 5 minutes"
timerSeconds="300"
elif [ "${restartDialogPrompt}" == "3" ]; then
echo "User selected 20 minutes"
timerSeconds="1200"
fi
# Setup our pipe
rm -f /tmp/hpipe
mkfifo /tmp/hpipe
sleep 0.2
# Display Restart Countdown Clock
"${CD}" progressbar --title "Please Restart Your Computer" --text "Preparing to reboot this Mac..." \
--posX "center" --posY "199" --width 500 --float \
--icon-file "/usr/local/assets/rh.png" \
--icon-height 48 --icon-width 48 --height 90 < /tmp/hpipe &
# Send progress through the named pipe
exec 3<> /tmp/hpipe
echo "100" >&3
sleep 1.5
# Time logic
startTime=$(date +%s)
stopTime=$((startTime+timerSeconds))
secsLeft=${timerSeconds}
progLeft="100"
# Loop through timerSeconds
while [[ "${secsLeft}" -gt 0 ]]; do
sleep 1
currTime=$(date +%s)
progLeft=$((secsLeft*100/timerSeconds))
secsLeft=$((stopTime-currTime))
minRem=$((secsLeft/60))
secRem=$((secsLeft%60))
echo "${progLeft} ${minRem} minute(s) ${secRem} seconds until your Mac reboots. Please save any work now." >&3
done
# display yet another fucking dialog giving the user another excuse to not do what they're fucking told
restartNowFuckTard=$(${CD} msgbox --title "Restart Please" \
--text "An important update has been installed and requires a restart" \
--icon-file "/usr/local/assets/rh.png" \
--informative-text "It is important that you select Restart, but you may select to restart at a more convenient time." \
--no-newline --button1 "Restart Now" --float)
if [ "${restartNowFuckTard}" == "1" ]; then
echo "User selected Restart"
/sbin/shutdown -r now
exit
else
echo "User didn't select anything, but IDGAF"
/sbin/shutdown -r now
exit
fi
# Restart after timer is done.
#/sbin/shutdown -r now
#exit
}
# Check to see if the logged in user is root
# if so, quit
if [ "${loggedInUser}" != 'root' ]; then
runPrompt
else
echo "User is root, exiting"
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment