#!/usr/bin/tclsh
# wdog.tcl
# Script to check the mud and reboot if the mud has not updated the syslog
# in over X minutes.
# Written by Gilbert Nguyen for NoMUD
# Last Modified: 02/09/1999

#################################################
## Modify this value to set the sensitivity of ##
## the watchdog.                               ##
#################################################
set timeout 5
set home $env(HOME)/nm



set month [list 0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
set mday  [list 0 31 59 90 120 151 181 212 243 273 304 334 365]
# Thu Nov 19 20:30:47 CST 1998
proc transtime {sdate} {
global month mday 
  if {[llength $sdate] < 4} { return -1 }
  set mnth [expr [lsearch $month [lindex $sdate 1]] - 1]
  if {$mnth == ""} {
    global home
    catch {exec tail $home/syslog} tailout
    puts stderr "$tailout\n" 
    exit 1
  }
  set mon [expr [lindex $mday $mnth] * 1440]
  set day [expr [lindex $sdate 2] - 1]
  set day [expr $day * 1440]
  set time [split [lindex $sdate 3] :]
  set hour [string trimleft [lindex $time 0] 0]
  if {$hour == ""} { set hour 0 }
  set hr [expr $hour * 60]
  set minute [string trimleft [lindex $time 1] 0]
  if {$minute == ""} { set minute 0 }
  set date [expr $mon + $day + $hr + $minute]
  return $date
}

proc getdate {} {
global home
  set fl [open "|tail $home/syslog" r]
  set line ""
  while {![eof $fl]} {
    set output $line
    gets $fl line
  }
  set retstr [lrange $output 0 3]
  return "$retstr"
  put "getdate $retstr"
}

proc find_online {str} {
  set b 0
  regexp "Signal received - (\[0-9\]+) people online" $str a b c
  return $b
}

if {!$tcl_interactive} {
  if [file exists $home/maintenance] { 
    puts "Found file $home/maintenance.  I will not start the mud until \
      this file is removed."
    return }
  catch {exec ps x \| grep bin/circle \| grep -v grep} result
  set pid [lindex $result 0]
  if {![regexp ^(\[0-9\]+)$ $pid]} { 
    puts "MUD is not online.. booting mud now"
    cd $home
    exec ./autorun &
    return
  }

  set d1 [transtime [getdate]]
  catch {exec date} currdate
  set d2 [transtime $currdate]
  if {$d1 == -1 || $d2 == -1} { 
    puts "MUD has no syslog.  Rebooting."
    cd $home
    exec kill $pid
    return
  }
  set diff [expr $d2 - $d1]
  if {$diff > $timeout} {
    catch {exec kill -USR1 $pid} result
    catch {exec sleep 2}
    set numonline [find_online [exec tail $home/syslog]]
    if {$numonline} {
      puts "Non-idle mud instance has been idle for $diff minutes ($numonline online)."
      puts "Removing locked up mud instance PID($pid)."
      catch {exec kill $pid} result
      return
    } 
    set d1 [transtime [getdate]]
    catch {exec date} currdate
    set d2 [transtime $currdate]
    set diff [expr $d2 - $d1]
    if {$diff > $timeout} {
      puts "DEBUG: $d2 - $d1 = $diff"
      puts "Greater than $timeout minutes!!!"
      puts "Removing locked up mud instance PID($pid)."
      catch {exec kill $pid} result
      catch {exec ps $pid} result
      puts "$result"
    }
  }
}
