#!/bin/bash

# Source the configuration file
. /etc/autosuspend.conf

# Log system notice
logit()
{
        logger -p local0.notice -s -- AutoSuspend: $*
        return 0
}

# Look for other CLIENTS in the network
IsOnline()
{
        for i in $*; do
                ping $i -c1
                if [ "$?" == "0" ]; then
                  logit "CLIENT $i is still active, auto suspend terminated"
                  return 1
                fi
        done
        return 0
}

IsRunning()
{
        for i in $*; do
                if [[ `pgrep -c $i` > 0 ]] ; then
                        logit "PROCESS $i still active, auto suspend terminated"
                        return 1
                fi
        done
        return 0
}

IsDaemonActive()
{
        for i in $*; do
                if [[ `pgrep -c $i` > 1 ]] ; then
                        logit "DAEMON $i still active, auto suspend terminated"
                        return 1
                fi
        done
        return 0
}

IsBusy()
{

        # Netstat Watch

        # Best to run this first in IsBusy, so watchfile can be deleted if it exists!
        # No suspend if netstat has show activity on marked ports in the past hour
        # Requires running auxillary cron driven shell script (netstat-watch.sh) for activity test
	# the netstat-watch.sh will create the WATCHFILE that this script is checking for the existance
        # of, then it deletes it so that the whole process can repeat again.
	# Typically ran every 10 minutes or so and dumping output to null on both stdout and stderr
	# Like this...
	#    */10 * * * * /home/erick/bin/netstat-watch.sh > /dev/null 2>&1
        if [ -e $WATCHFILE ]; then
	    logit "auto suspend is terminated by existance of $WATCHFILE"
	    # Always Remove file so that we can test again next time.
	    rm $WATCHFILE
	    return 1
	fi 

        # Samba
        if [ "$SAMBANETWORK" ]; then
                if [ `/usr/bin/smbstatus | grep $SAMBANETWORK | wc -l ` != "0" ]; then
                  logit "SAMBA connected, auto suspend terminated"
                  return 1
                fi
        fi

        #daemons that always have one process running
        IsDaemonActive $DAEMONS
        if [ "$?" == "1" ]; then
                return 1
        fi

        #backuppc, wget, wsus, ....
        IsRunning $APPLICATIONS
        if [ "$?" == "1" ]; then
                        return 1
        fi

        # No Suspend if there are any users logged in
        if [[ `who | wc -l` > 0 ]];then 
        logit "USERS still connected, auto suspend terminated";
        return 1
        fi
        
        IsOnline $CLIENTS
        if [ "$?" == "1" ]; then
                return 1
        fi

        return 0
}

IDLE_FILE="/var/spool/autosuspend_idle_marker"
OFFFILE="/var/spool/autosuspend_off"

# turns off the auto suspend
if [ -e $OFFFILE ]; then
        logit "auto suspend is turned off by existents of $OFFFILE"
        exit 0
fi

if [ "$AUTO_SUSPEND" = "no" ]; then
        logit "auto suspend is turned off in configuration file /etc/autosuspend.conf"
elif [ "$AUTO_SUSPEND" = "yes" ]; then
        # Check if system is busy now.
        IsBusy
        if [ "$?" == "0" ]; then
                # Idle now. Has the system been idle on the previous check?
                if [ -e $IDLE_FILE ]; then
                        # Suspend now.
                        logit "AUTO SHUTDOWN CAUSED"
                        rm -f $IDLE_FILE
			# Set up WOL for next time it needs to start.
                        ethtool -s eth0 wol g
			# Put the boot time and shutdown time into the logfile.
			tail -1 /var/www/bootlog.txt >> /var/www/shutdown.txt
	                date >> /var/www/shutdown.txt  
			echo "------------------------------------" >> /var/www/shutdown.txt
			# Shut it down in 5 minutes, 5 minute warning allows for abort and safeguards against rogue script.
                        shutdown -P +5
                        exit 0
                else
                        # Mark system as idle for the next check.
                        touch $IDLE_FILE
                        logit "marked as idle for next check"
                        exit 0
                fi
        else
        # Busy now. Mark system as busy for the next check.
                rm -f $IDLE_FILE
                logit "marked as busy for the next check"
                exit 0
        fi
        exit 0

else
        logit "misconfiguration. Check /etc/autosuspend.conf"
fi
exit 1
