#! /bin/sh
# $Id: botchk,v 1.4 1999/12/21 17:35:08 fabian Exp $

# Modified by CyBex 4 OIdentd and configuration better sorted ;-)
# Whatz New? It's commented with # This is New :)


# change this to the nickname of your bot (capitalization COUNTS)
botname="Lamest"
# change this to the ident
export IRCUSER="$botname"
# change this to the name of your bot's script in that directory:
botscript="conf.$botname"
# change this to the name of your bot's userfile (capitalization COUNTS)
userfile="user.$botname"
# change this to the directory you run your bot from:
botdir="/path/to/your/eggdrop/conf/file"

########## 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

# This is New

    echo  > ~/.oidentd.conf "global {"
    echo >> ~/.oidentd.conf " reply \"$IRCUSER\""
    echo >> ~/.oidentd.conf "}"
   
    ./$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

# This is New

      echo  > ~/.oidentd.conf "global {"
      echo >> ~/.oidentd.conf " reply \"$IRCUSER\""
      echo >> ~/.oidentd.conf "}"

      ./$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

# This is New

        echo  > ~/.oidentd.conf "global {"
        echo >> ~/.oidentd.conf " reply \"$IRCUSER\""
        echo >> ~/.oidentd.conf "}"

        ./$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
