#!/bin/sh
#
# $Id: starter,v 1.0.1 2004/03/24 23:27:34 [Xp-AvR] Exp $

evaroot=`pwd`
BINARYDIR=binary
BINARY=evangeline
CFGDIR=cfg
DATADIR=.data/.bdata

clear

case "$1" in
  --cron)
    if [ -f $CFGDIR/$2.cfg ]; then
      $evaroot/$BINARYDIR/abotchk.tcl $CFGDIR/$2 -dir $evaroot -15
    fi
    ;;
  --list)
    echo "[1;1mAvailable bots: [0m"
    opt="$2"
    for i in $CFGDIR/*.cfg; do
      name=`echo $i | sed s/.cfg// | sed s/.cfg// | sed s/"cfg\/"//`
      if [ -f $DATADIR/.pid.$name ]; then
        started="@Work"
      else
        started=""
      fi
      if [ -f $DATADIR/$name.uf ]; then
        echo "  [1;1m$name[0m (OK$started)"
      else
        opt=`echo $opt | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
        if [ "$opt" != "ok" ]; then
          echo "  $name (No userfile$started)"
        fi
      fi
    done
    ;;
  --new)
    if [ -f $CFGDIR/$2.cfg ]; then
      $evaroot/$BINARY -m $CFGDIR/$2.cfg
    else
      echo -n "[1;1mConfig doesn't exist, create? [Y/n]: [0m"
      read createcfg
      if [ "$createcfg" = "y" ] || [ "$createcfg" = "Y" ] || [ "$createcfg" = "" ]; then
        $evaroot/$BINARYDIR/config-wizard.tcl $2
      fi
      if [ -f $CFGDIR/$2.cfg ]; then
        echo ""
        echo -n "[1;1mConfig created, start bot? [Y/n]: [0m"
        read createdcfg
        if [ "$createdcfg" = "y" ] || [ "$createdcfg" = "Y" ] || [ "$createdcfg" = "" ]; then
          $evaroot/$BINARY -m $CFGDIR/$2.cfg
        fi
      fi
    fi
    ;;
  --newcfg)
    if test "$2"; then
      $evaroot/$BINARYDIR/config-wizard.tcl $2
    else
      $evaroot/$BINARYDIR/config-wizard.tcl
    fi
    ;;
  --kill)
    if [ -f $evaroot/$DATADIR/.pid.$2 ]; then
      kill `cat $evaroot/$DATADIR/.pid.$2`
    fi
    ;;
  --restart)
    if [ -f $evaroot/$DATADIR/.pid.$2 ]; then
      kill `cat $evaroot/$DATADIR/.pid.$2`
    fi
    $evaroot/$BINARY $CFGDIR/$2.cfg
    echo "[1;1mRestarted: $2[0m"
    ;;
  --start)
    $evaroot/$BINARY $CFGDIR/$2.cfg
    ;;
  --help)
    echo "[1;1mEvangeline Commands:[0m" >&2
    echo "  --cron    <botnick> set up crontab for bot" >&2
    echo "  --list    [ok/all]  list known bots" >&2
    echo "  --new     <botnick> create new bot" >&2
    echo "  --newcfg  [botnick] create new config" >&2
    echo "  --kill    <botnick> kills the bot" >&2
    echo "  --restart <botnick> restarts the bot" >&2
    echo "  --start   <botnick> start the bot" >&2
    exit 64
    ;;
  *)
    if [ -f $CFGDIR/$1.cfg ]; then
      $evaroot/$BINARY $CFGDIR/$1.cfg
    else
      $evaroot/$0 --help
    fi
    ;;
  esac
  exit 0
