#! /bin/sh
# Darkbot utility to auto generate crontab.

# Copyright (C) 1996 Darkbot Project.

# This program is free software, you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2.  This
# program is distributed in the hope that it will be useful, but without
# any warranty, without even the implied warranty of merchantability or
# fitness for a particular purpose.  See the COPYING file for details.


# Just run this script and a new one with the same name will be created
# with the actual path of your Darkbot installation. At the same time 
# a crontab job will be created with 10 mins interval.

# to do: user defined timers on command line utility launch

PID=0624233000
MAKE_DBCRON () {
cat > dbcron << EOF[DBCRON]
#! /bin/sh
# Darkbot's crontab utility

DBPATH="${DBPATH:-$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)}"
DBBIN="${DBBIN:-darkbot}"
DBPIDFILE="${DBPIDFILE:-$DBPATH/$DBBIN.pid}"

# This file was automatically generated the first time you've run  dbcron.
# If for any reason you've changed your  Darkbot directory or it's  binary
# you'll need to modify the variables DBPATH DBBIN and DBPIDFILE on top of
# this file accordingly to the actual values.
# Defaulty crontab job is of 10 mins interval. If you wish to change it
# edit '0,10,20,30,40,50 * * * *'. If you don't understand it type 'man crontab'
# -----------------------------------------------------------------------------
# What this script does:
# Checks the pid file, exits if the bot is already running, removes a stale
# pid file if needed, and starts the binary again if it exists.
# ____________ Do not change anything below this line! ____________#
cd "\$DBPATH" || exit 1
if [ -r "\$DBPIDFILE" ]; then
	DBPID=\$(cat "\$DBPIDFILE")
	if kill -0 "\$DBPID" >/dev/null 2>&1; then
		exit 0
	fi
	echo "stale pid file (erasing it)"
	rm -f "\$DBPIDFILE"
fi
echo "Darkbot is dead! Restarting ..."
if [ -x "\$DBPATH/\$DBBIN" ]; then
	"\$DBPATH/\$DBBIN"
	exit 0
fi
echo "could not reload"
EOF[DBCRON]
}

# PREP SCRIPT

# check if it is on the right place
ABORT () {
echo -e "\nThis utility will stop now!\n\nLeav\
ing $0...\n\n\n"; sleep 1; exit 0
}
DB_SCRIPTS_PATH () {
dirutil="scripts"
filutil="`basename $0`"
# check if dir is correct
if ! echo `pwd` | grep -q "\/"$dirutil""; then
# file is not being launched from $dirutil
 if [ -d "$dirutil" ]; then # dir is there
 # if file is not in the correct dir
  if [ ! "`ls "$dirutil" | grep "$filutil"`" ]; then
  # file is not in the correct dir so move it
clear
echo -e "$PNCI\n\n##### WARNING #####\n\n"$filutil" must be located \
in "$dirutil" directory.\n"
echo "Moving it now...."
sleep 2
cat > db_wrong_path << EOF[WP]
mv -f $0 "$dirutil"/
cd "$dirutil"/
echo -e "\n| Your "$filutil" is now located in your "$dirutil" directory |
            | Launch it from there when necessary |\n\n"
sleep 4
$0
rm -f ../db_wrong_path
exit 0
EOF[WP]
. db_wrong_path
  else # file is in dir so just cd
cd "$dirutil"/
  fi
 else # dir is not there so don't bother - tell user to move it
clear
echo -e "$PNCI\n\n##### WARNING #####\n\n"$filutil" must be located \
in "$dirutil" directory and launched from there or Darkbot's root.
Please move it and launch it again."
sleep 3
ABORT
 fi
fi
}
#DB_SCRIPTS_PATH

# check if crontab is present if not there is no reason to run this script
if ! type crontab >/dev/null 2>&1 ; then
echo -e "\nThis script needs 'crontab' command to run, which appears it's \
not installed on this system.
Leaving..."
exit 0
fi

#check if crond is active
if [ ! "$(ps ax | grep -v grep | grep crond 2>/dev/null)" ]; then
echo -e "\nThis script needs 'crond' running, which appears it's \
not on this system.
Leaving..."
fi

# dbcron engine creation. make vars for darkbot path, dir and pid file
DBPATH=$(cd ..; pwd)
if [ -x "$DBPATH/darkbot" ]; then
DBBIN="darkbot"
DBPIDFILE=$DBBIN.pid

# vars are done so let's tell crontab how we want it setup
# check every 10 minutes; send messages to null
# echo '0,10,20,30,40,50 * * * *' "$DBPATH/scripts/dbcron" > crontable.$$ #debug only
echo '0,10,20,30,40,50 * * * *' "$DBPATH/scripts/dbcron >/dev/null 2>&1" >> crontable.$$
crontab crontable.$$; rm -f crontable.$$

# transfer crontab engine file with new vars on top to a new file with same
# name as this one. THIS FILE CONTENTS WILL BE GONE!
 MAKE_DBCRON
 clear
echo -e "\n\nDarkbot crontab utility\n\nCrontab job created successfully!
To check if everything is okay, kill your bot if it is connected to IRC.
It should automatically restart in 10 minutes.\n
Suggestions, improvements?
Support: http://darkbot.sourceforge.net\n\n"
else
echo -e "\nDarkbot's Crontab Utility.
WARNING!!!!\nDarkbot's binary not found.
Please run ./configure if you haven't do so or restore \
file names to it's default values.\n
Leaving $0..." 
exit 0
fi
