#!/bin/bash
########################################################################
# filename:	fan_ctrl2              (PTT AND COS Supervision)
#
# description:	This script is used to run in the background and will
#               count the time PTT or COS line is active.  If active for
#               more than a preset time, fan will turn on.  PTT and COS
#               must then be off for another preset time before fan is
#               shut down.  A locking mechanism is provided to keep fan
#               running until told to shutdown. 'touch $LOCAL/fan_lock'
#               will start the fan and hold it until 'rm -f $LOCAL/fan_lock'
#               is done.
#               As these flags are in ramdisk, reboot will clear them.
#
#         ** Add script to startup file to run always as follows
#   ####################################################################
#   # edit file "~/custom/rc.irlp" as user repeater....                #
#   # Add these lines..                                                #
#   #    echo -n "Enabling cooling fan process... "                    #
#   #    /bin/su - -c "$CUSTOM/fan_ctrl2" repeater >&/dev/null 2>&1 &  #
#   #    echo "(in background) done!"                                  #
#   #                                                                  #
#   ####################################################################
#
# arguments:	always.. but not with me!
#
# flags:        $LOCAL/fanrun   - flag is present whilst fan running.
#               $LOCAL/fan_lock - is present to lock fan ON
#                   (set and cleared by outside app)
#
# history:
# 20090704  V1.00    vk8sj  Original ...
# 20100228  V1.01    vk8sj  Added lock option
# 20110126  V1.02    vk8sj  Few mods to reduce CPU load when idle
# 20110820  V1.03    vk8sj  Mods to locking option & add COS Detect
#
#
########################################################################
#
# test user & load environment
. /home/irlp/custom/environment
#
if [ `/usr/bin/whoami` != "repeater" ] ; then
	echo This program must be run as user REPEATER!
	exit 1
fi
#
tictoc_start=15                                                 # PTT up for n seconds to start fan
tictoc_stop=30                                                  # Overhang till off (n again)
fanon="aux3on"                                                  # where is fan connected?
fanoff="aux3off"                                                #  (aux1, 2 or 3...)
#
rm -f $LOCAL/fanrun                                             # make sure flag file cleared
rm -f $LOCAL/fan_lock                                           # make sure lock removed
$fanoff                                                         # make sure fan is off now
echo "Fan Control Started.."

# Let's begin...

while true; do                                                  # create endless loop
    if ! $BIN/pttstate || ! $BIN/cosstate ; then                # wait for PTT or COS active..
        echo "Activity Detected."                               # DEBUG ONLY - Test
        tictoc=0                                                # init var for loops
        while ! $BIN/pttstate || ! $BIN/cosstate ; do           # if still active..
            let tictoc+=1                                       # crank counter
            echo "Start Timer - "$tictoc
            sleep 1                                             # Take a nap (Short!)
            if [ $tictoc -gt $tictoc_start ] ; then             # time up yet?
                $fanon                                          # fire up fan
                touch $LOCAL/fanrun                             # set flag active
                echo "Fan - ON."                                # DEBUG ONLY - test.
                break                                           # crash out of loop
            fi 
        done                                                    # loop till it drops or breaks
        if [ ! -f $LOCAL/fanrun ] ; then
            echo "Activity Stopped. - Timer "$tictoc            # DEBUG ONLY - Test.
        fi
        tictoc=0                                                # reset var to loop again
        while [ $tictoc -le $tictoc_stop ] ; do
            if [ ! -f $LOCAL/fanrun ] ; then
                break
            fi
            if ! $BIN/pttstate || ! $BIN/cosstate ; then        # tx back up again..
                echo "Reset & hold. Timer "$tictoc              # DEBUG ONLY - Test.
                tictoc=0                                        # reset counter
                while ! $BIN/pttstate || ! $BIN/cosstate ; do   # loop till PTT off
                    sleep 1                                     # zzzzzzz
                done                                            # do nothing (good at that!)
                echo "Counting..."                              # DEBUG ONLY - Test.
            fi
            sleep 1
            let tictoc+=1                                       # crank up the counter
            echo "Rundown Timer - "$tictoc
        done
        if [ -f $LOCAL/fanrun ] ; then                          # If the fan is running..
            if [ ! -f $LOCAL/fan_lock ] ; then
                $fanoff                                         # Kill fan if not locked on
                echo "Fan - OFF."
                rm -f $LOCAL/fanrun                             # remove test flag
            else
                echo "Fan - locked on, Waiting"                 # DEBUG ONLY - test.
                while [ -f $LOCAL/fan_lock ] ; do               # wait for lock removal
                    sleep 1                                     # zzzzzzz a sec..
                done 
                $fanoff                                         # nuke breeze..
                echo "Lock removed.. FAN - OFF."
                rm -f $LOCAL/fanrun                             # remove flag
            fi
        fi
    fi
    if [ -f $LOCAL/fan_lock ] ; then                            # look for lock-on flag, fan stopped
        echo "Fan_Lock detected.. FAN - ON."
        $fanon                                                  # Go! - Blow!
        touch $LOCAL/fanrun                                     # Signal fan running
        while [ -f $LOCAL/fan_lock ] ; do                       # wait for lock removal
            sleep 1                                             # zzzzzzz a sec..
        done
        $fanoff                                                 # nuke breeze..
        echo "Lock removed.. FAN - OFF."
        rm -f $LOCAL/fanrun                                     # remove flag
    fi
sleep 1
done                                                            # continue endless loop
echo "What? - Should never get here!!"
exit 1                                                          # should never get here!!
