#!/bin/bash

# Script to make the user aware that a restart has not occurred in over 14 days

# Use JamfHelper tool to show alert message
jHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

# Set maximum days to 14 before warning about restart
maxDays="14"

# Check to see if machine has restarted in the last day
# See if "days" exists in the uptime
upTime=`uptime | grep "days"`
if [ -z "$upTime"  ];
then
    echo  "Not yet one day"
    exit 1;
else 
    echo "More than one day"
fi

# Get current uptime
upTimeDays=`uptime | awk '{print $3}'`

# Display uptime
echo "Uptime days: $upTimeDays"

# Advise user of uptime and give the option to reboot
msg="Your University supported Mac has not been restarted for at least $upTimeDays days.

Please restart as soon as it is convenient in order to maintain smooth operation of your system."

# Location of icon
icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"

# If uptime is equal to or greater than 14 days then display message

if [ "$upTimeDays" -ge "$maxDays" ]; then
    echo "Mac has been up for more than $maxDays days"
    # Get answer from user
    result=`"$jHelper" -windowType utility -description "$msg" -title "UoE Supported Desktop Notification" -button1 "Restart now" -button2 "Not yet" -defaultButton 2 -icon "$icon" -iconSize 90`
    # If answer is Restart now, then restart
    if [ $result -eq 0 ];
    then
        echo "I am rebooting...."
        #reboot   
    else
    # Else delay restart
        echo "Not yet..."
        exit 0
    fi
else
    # Mac has been restarted within 14 days
    echo "Mac has been up for less than $maxDays days. Exiting."
    exit 0
fi