#!/bin/csh -f
#
# generate a thermal log by running isi_hw_status on just the temps
# (in table format) every N seconds [default=60]

set logperiod = 60

### figure out what dir this program was found/run in (but if there
### was no path mentioned, don't just assume "./")
if ( "X$0" !~ */* ) then
    set rundir = "";
else
    set rundir = "`dirname $0`/"
endif

set logfile = ""
set hwflg = "-t -f"

while ($#)
    set linearg = $1;
    shift;

    switch ($linearg)
    case "-help":
        echo "usage: $0 [flags] <logfile>"
	echo "  -justtemps  temp, not fans"
	echo "  -justfans   fans, not temp"
	echo "  -addstate   add -s to isi_hw_status"
	echo "  -per N     select log period in seconds (default $logperiod)"
	echo "  Use '-' for a logfile to use standard output"
	echo "  Append a thermal log to the given logfile."
	exit;
	breaksw;
    case "-per":
	set logperiod = $1;
	shift;
	breaksw;
    case "-justfans":
	set hwflg = "-f"
	breaksw;
    case "-justtemps":
	set hwflg = "-t"
	breaksw;
    case "-addstate":
	set hwflg = "$hwflg -s"
	breaksw;
    default:
	if ( "X$logfile" != "X" ) then
	    echo "Only one logfile allowed -- type '$0 -help' for more info"
	    exit
	endif
	set logfile = "$linearg"
	breaksw;
    endsw
end

if ( "X$logfile" == "X" ) then
    echo "Need logfile specified -- type '$0 -help' for more info"
    exit
endif


## set up a redirect which also supports stdout
if ( "X$logfile" == "X-" ) then
    set logout = ""
else
    set logout = ">> $logfile"
endif

## finally, run the commands
eval "${rundir}isi_hw_status -THH $hwflg | tr '\012' ' '"      $logout
eval "echo"                                                 $logout
while (1)
    eval "${rundir}isi_hw_status -TH  $hwflg | tr '\012' ' '"  $logout
    eval "date +%D_%T"                                      $logout
    sleep $logperiod
end
