#! /bin/sh
# ^-- little trick to restart using tclsh \
exec tclsh "$0" "$@"
#
# AutoBotchk, (C)1999 Jeff Fisher (guppy@eggheads.org)
#
# updates
# -------
#  03Feb99: wrote the first version and it worked
#  04Feb99: made it easier to use, how? ./autobotchk
#           <config> will create the botchk and crontab
#           automatically, no command line options now.
#  20Feb99: added a '-noemail' setting for [s]
#  21Feb99: added '-5' '-10' '-15' '-30' to allow people to
#           use a diff time value then 10 minutes.
#  10May99: to be compatible, don't use */XX's, etc (Ernst)
#  23Oct99: updated the internal botchk to reflect Tothwolf's
#           changes in the eggdrop copy.
#
# how to use
# ----------
#  well, first you need to put the program into your Eggdrop
#  directory (the one you run from hopefully). then you should
#  probably type 'chmod u+x autobotchk' so the file permissions
#  are set correctly. and finally, type './autobotchk <config>'
#  replacing <config> with the name of your config file right?
#
#  in 1.05z, because of [z], you can now specify the bot directory
#  to use, for example, './autobotchk eggdrop.conf -dir /home/bot'
#  this now allows you to run the program from any dir :P~
#
# other Stuff
# -----------
# [22:36] <cHr|s`> when you type ./autobotchk without any options, it should give you the menu
# [22:36] <cHr|s`> like the nukes do
#  ^ -- he might be Urkel, but we love him anyways ..
# [18:10] <[z]> nope...came back weith can't crontab -l error
#  ^ -- no one said this script was perfect, mmmm z's wife
# [19:27] <[z]> also guppy..would be nice if i could reference my bots directories ;)
#  ^ -- just when I finished 1.05 :P
# [18:50] <[s]> how to set the autobotchk not to recieve e-mails about the cron ??
# [18:51] <slennox> add >/dev/null 2>&1 to the end of the crontab entry
# [18:52] <[s]> what crontab
# [18:52] <[s]> i have gupy's script
# [18:52] <[s]> autobotchk

if {$argc == 0} {
 puts "\nusage: $argv0 <eggdrop config> \[options\]"
 puts " -dir     (bot directory to use instead of \[pwd\])"
 puts " -noemail (will make crontab not mail you)"
 puts " -5       (5 minute checks)"
 puts " -10      (10 minute checks)"
 puts " -15      (15 minute checks)"
 puts " -30      (30 minute checks)\n\n"
 exit
} {
 puts "\nAutoBotchk 1.07, (C)1999 Jeff Fisher (guppy@eggheads.org)"
 puts "----------------------------------------------------------"

 set x [join [lindex [split $argv] 0]]
 set q [lrange $argv 1 end]
 set dir [pwd]
 set delay 10
 set email 1
 for {set z 0} {$z < [llength $q]} {incr z} {
  set opt [join [lindex $q $z]]
  switch -- $opt {
   -time -
   -1 {set delay 1}
   -5 {set delay 5}
   -10 {set delay 10}
   -15 {set delay 15}
   -20 {set delay 20}
   -30 {set delay 30}
   -nomail -
   -noemail {set email 0}
   -dir {
    set dir [string trimright [join [lindex $q [expr $z + 1]]] /]
    if {[string match -* $dir]} {
     puts " *** ERROR: you did not supply a directory name\n\n"
     exit
    }
    if {![file isdirectory $dir]} {
     puts " *** ERROR: the directory you supplied is not a directory\n\n"
     exit
    }
    incr z
   }
  }
 }
 switch -- $delay {
  30 { set minutes "0,30" }
  15 { set minutes "0,15,30,45" }
   5 { set minutes "0,5,10,15,20,25,30,35,40,45,50,55" }
   1 { set minutes "*" }
  default { set minutes "0,10,20,30,40,50" }
 }
 if {![file exists $dir/$x]} {
  puts " *** ERROR: you supplied a non-existant filename\n\n"
  exit
 } elseif {![file isfile $dir/$x]} {
  puts " *** ERROR: unknown filetype, config files should be text files\n\n"
  exit
 } elseif {![file readable $dir/$x]} {
  puts " *** ERROR: unable to open the file, permission denied\n\n"
  exit
 } elseif {![file exists $dir/help] || ![file isdirectory $dir/help]} {
  puts " *** ERROR: are you sure you are running from a bot directory?\n\n"
  exit
 } elseif {![file exists $dir/eggdrop]} {
  puts " *** ERROR: are you sure you are running from a bot directory?\n\n"
  exit
 }
 puts " *** Attempting to process '$x', be patient ..."

 set f [open $dir/$x r]
 while {![eof $f]} {
  set z [split [gets $f]]
  if {[join [lindex $z 0]] != "set"} {continue}
  switch -- [join [lindex $z 1]] {
   nick -
   userfile -
   botnet-nick {
    set [join [lindex $z 1]] [join [lindex $z 2]]
    puts "     !! scanned: [join $z]"
   }
  }
 }
 close $f
 if {![info exists {botnet-nick}] && [info exists nick]} {
  puts "         !! setting \$botnet-nick to \$nick"
  set botnet-nick $nick
 }
 if {![info exists {botnet-nick}] || ![info exists userfile]} {
  puts " *** ERROR: could not find either \$userfile or \$botnet-nick"
  puts "      !! you sure its a valid Eggdrop config file?\n\n"
  exit
 }
 if {[catch {open $dir/${botnet-nick}.botchk w} fd]} {
  puts " *** ERROR: unable to open '${botnet-nick}.botchk' for writing"
  puts "      !! without this, this program is useless\n\n"
  exit
 }
 puts $fd "
#! /bin/sh
#
# new botchk  (for eggdrop 1.1+)
#
# Created on: [clock format [clock seconds] -format "%B %d, %Y @ %I:%M%p"]
# Created by: AutoBotchk 1.07 (C)1999 Jeff Fisher
#
# This is a script suitable for use in a crontab.  It checks to make sure
# your bot is running.  YOU NEED A SEPARATE CRON JOB FOR EACH BOT.  If your
# bot isn't found, it'll try to start it back up.
#
# You'll need to edit this script for your bot.
#
# To check for your bot every 10 minutes, put the following line in your
# crontab:
#    0,10,20,30,40,50 * * * *   /home/mydir/botchk
# And if you don't want to get email from crontab when it checks you bot,
# put the following in your crontab:
#    0,10,20,30,40,50 * * * *   /home/mydir/botchk >/dev/null 2>&1
#

# change this to the directory you run your bot from:
botdir=\"$dir\"

# change this to the name of your bot's script in that directory:
botscript=\"eggdrop $x\"

# change this to the nickname of your bot (capitalization COUNTS)
botname=\"${botnet-nick}\"

# change this to the name of your bot's userfile (capitalization COUNTS)
userfile=\"$userfile\"

########## you probably don't need to change anything below here ##########

cd \$botdir

# is there a pid file?
if test -r pid.\$botname
then
  # there is a pid file -- is it current?
  botpid=`cat pid.\$botname`
  if `kill -CHLD \$botpid >/dev/null 2>&1`
  then
    # it's still going -- back out quietly
    exit 0
  fi
  echo \"\"
  echo \"Stale pid.\$botname file, erasing...\"
  rm -f pid.\$botname
fi

if test -r CANTSTART.\$botname
then
  if test -r \$userfile || test -r \$userfile~new || test -r \$userfile~bak
  then
    echo \"\"
    echo \"Userfile found, removing check file 'CANTSTART.\$botname'...\"
    rm -f CANTSTART.\$botname
  fi
fi

# test if we have run botchk previously and didn't find a userfile
if test ! -f CANTSTART.\$botname
then
  echo \"\"
  echo \"Couldn't find bot '\$botname' running, reloading...\"
  echo \"\"
  # check for userfile and reload bot if found
  if test -r \$userfile
  then
    # It's there, load the bot
    ./\$botscript
    exit 0
  else
    if test -r \$userfile~new
    then
      # Bot f*@!ed up while saving the userfile last time.  Move it over.
      echo \"Userfile missing.  Using last saved userfile...\"
      mv -f \$userfile~new \$userfile
      ./\$botscript
      exit 0
    else
      if test -r \$userfile~bak
      then
        # Userfile is missing, use backup userfile.
        echo \"Userfile missing.  Using backup userfile...\"
        cp -f \$userfile~bak \$userfile
        ./\$botscript
        exit 0
      else
        # Well, nothing to work with...
        echo \"No userfile.  Could not reload the bot...\"
        echo \"no userfile\" > CANTSTART.\$botname
        exit 1
      fi
    fi
  fi
fi

exit 0
 "
 close $fd
 puts " *** Wrote '${botnet-nick}.botchk' successfully ([file size $dir/${botnet-nick}.botchk] bytes)"
 if {[catch {exec chmod u+x $dir/${botnet-nick}.botchk} 0]} {
  puts "      *** ERROR: unable to 'chmod u+x' the output file\n\n"
  exit
 }
 puts "      !! chmod u+x completed"
 puts " *** Crontab mode?? Heaven forbid I automated this ..."
 set tmp "[pwd]/.autobotchk"
 if {$delay != "10"} {puts "      *** Using a time delay of $delay minutes."}
 if {$email} {
  set line "$minutes \* \* \* \* $dir/${botnet-nick}.botchk"
 } {
  set line "$minutes \* \* \* \* $dir/${botnet-nick}.botchk >\/dev\/null 2>&1"
  puts "      *** Making crontab line to NOT send e-mail."
 }
 if {[catch {exec crontab -l > $tmp} error ]} {
  if {![string match "*no*cron*" [string tolower $error]] &&
      ![string match "*can't open*" [string tolower $error]]} {
   catch {exec rm -rf $tmp} 0
   puts "      *** ERROR: unable to get crontab listing"
   puts "           *** MSG: $error\n\n"
   exit
  }
 }
 set fd [open $tmp r]
 while {![eof $fd]} {
  set z [split [gets $fd]]
  if {[string match "*$dir/${botnet-nick}.botch*" $z]} {
   puts "      !! ${botnet-nick}.botchk is already crontab'd, but why?\n\n"
   exit
  }
 }
 close $fd
 set fd [open $tmp a]
 puts $fd $line
 puts "      !! adding new crontab line to existing crontab"
 close $fd
 if {[catch {exec crontab $tmp} error]} {
  puts "      *** ERROR: unable to do 'crontab $tmp'"
  puts "           *** MSG: $error\n\n"
  exit
 }
 catch {exec rm -rf $tmp} 0
 puts "      !! crontab reloaded"
 puts ""
 puts "      !! Use 'crontab -r' to remove all your crontab entries"
 puts "      !!     'crontab -l' to view the current crontabs\n\n"
 exit
}
