#! /bin/sh
#
# $Id: botchk,v 1.7 2003-12-17 00:58:02 wcc Exp $
#
# 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/mybot/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/mybot/botchk >/dev/null 2>&1
#

# Change this to the directory you run your bot from (capitalization COUNTS):
botdir="/home/mydir/mybot"

# Change this to the name of your bot's config file (capitalization COUNTS):
botconfig="config.xml"

# change this to the botname of your bot (capitalization COUNTS):
botname="eggdrop"

# change this to the name of your bot's userfile (capitalization COUNTS):
userfile="users.xml"

########## 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
                # Yes.
                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; 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.
                ./eggdrop $botconfig
                exit 0
        else
                # No userfile.
                echo "No userfile.  Could not reload the bot..."
                echo "No userfile." > CANTSTART.$botname
                exit 1
        fi
fi

exit 0
