#!/bin/sh

# Configures the Darkbot

clear
echo
echo
echo "----------------------------------------------------"
echo "Welcome to Darkbot v6 configuration, `whoami`."
echo "This will lead you through the setup of your darkbot"
echo 
echo "The configuration will take a few minutes, and"
echo "will require you to answer questions... so don't"
echo "go anywhere!"
echo "----------------------------------------------------"
echo
while [ -z "$EEDITOR" ]
do
	echo -n "Enter the name of the text editor you would like to use [pico]: "
	read EEDITOR
	if [ -z "$EEDITOR" ]; then
		echo "No editor specified, defaulting to pico."
		EEDITOR=pico
fi
done
echo
echo "You will need to build the setup.ini for Darkbot..."
echo 
echo "Hit ENTER to continue."
read x
echo
while [ -z "$NICK" ]
do
	echo -n "Enter the nickname you would like to use for your bot: "
	read NICK
	if [ -z "$NICK" ]; then
		echo "Enter a nickname for your bot!"
		sleep 1
		echo
fi
done
while [ -z "$USERID" ]
do
	echo -n "Enter the userid you would like to use [`whoami`]: "
	read USERID
	if [ -z "$USERID" ]; then
		echo "No userid specified... defaulting to `whoami`"
		USERID=`whoami`
	fi
done
while [ -z "$CHAN" ]
do
	echo -n "Enter the channel $NICK should join: "
	read CHAN
	if [ -z "$CHAN" ]; then
		echo "You must enter a channel!"
		sleep 1
		echo
	fi
done
while [ -z "$REALNAME" ]
do
	echo -n "Enter the realname info for $NICK [default]: "
	read REALNAME
	if [ -z "$REALNAME" ]; then
		echo "Using default realname.."
		REALNAME="Download me from http://darkbot.net"
	fi
done
while [ -z "$CMDCHAR" ]
do
	echo -n "Enter the command character $NICK should respond to [!]: "
	read CMDCHAR
	if [ -z "$CMDCHAR" ]; then
		echo "No CMDCHAR specified, defaulting to !"
		CMDCHAR=!
	fi
done
while [ -z "$VHOST" ]
do
        echo -n "Enter the VHOST you want to use. If none, hit enter [0]:"
        read VHOST
        if [ -z "$VHOST" ]; then
                echo "None entered. Will not use VHOST."
                VHOST=0
        fi
done
echo 
echo "Writing data settings to setup.ini ....."
echo "NICK=$NICK" > dat/setup.ini
echo "USERID=$USERID" >> dat/setup.ini
echo "CHAN=$CHAN" >> dat/setup.ini
echo "REALNAME=$REALNAME" >> dat/setup.ini
echo "CMDCHAR=$CMDCHAR" >> dat/setup.ini
echo "VHOST=$VHOST" >> dat/setup.ini
echo "AUTOTOPIC=0" >> dat/setup.ini
echo
sleep 1
echo "Okay, now you will need to update the #define settings"
echo "in defines.h ... change the options you want to enable/disable"
echo 
echo "Hit ENTER to continue."
read tmp
$EEDITOR defines.h
sleep 1
echo 
echo "Next you need to specify the server $NICK should connect to."
echo 
while [ -z "$SERVER" ]
do
	echo
	echo -n "Enter the server your bot should connect to [irc.superchat.org]: "
	read SERVER
	if [ -z "$SERVER" ]; then
		echo "No server specified. defaulting to irc.superchat.org"
		SERVER=irc.superchat.org
	fi
done
while [ -z "$PORT" ]
do
	echo -n "Enter the port to connect to on $SERVER [6667]: "
	read PORT
	if [ -z "$PORT" ]; then
		echo "No port specified, defaulting to 6667"
		PORT=6667
	fi
done
echo "$SERVER $PORT (Added: `date`)" > dat/servers.ini
echo
sleep 1
echo
echo
echo
echo
echo 
while [ -z $QUITIT ]
do
	echo
	echo "Darkbot configuration menu -"
	echo
	echo "	1) Read the README files	(recommended)"
	echo "	2) Setup the .ini files		(recommended)"
	echo "	3) Setup your userlist		(required)"
	echo "	4) Add another server		(recommended)"
	echo "	5) Quit configuration		(Hope you *eventually* select this ;)"
	echo
	echo -n "Select one: "
	read CHOICE
	case $CHOICE in
		1) 
			echo
			echo "First take a look at the README file..."
			sleep 4
			$EEDITOR docs/README.txt
			echo
			echo "Next, see what the levels mean (in userlist.db)"
			sleep 4
			$EEDITOR docs/README.LEVELS.txt
			echo
			echo "Finally, check out what is new in Darkbot!"
			sleep 4
			$EEDITOR WHATSNEW
			;;
		2)
			echo
			echo "I will now lead you through the .ini files.."
			echo "The .ini's are all in RAW format... so when you"
			echo "want the bot to MSG your channel \"hello!\", you'd"
			echo "have to enter something like:"
			echo
			echo "PRIVMSG $CHAN :hello!"
			echo
			echo "(Note the :colon after $CHAN, it is required)"
			echo
			echo
			echo "The first .ini you will need to setup, is the perform.ini"
			echo "Perform list is a set of commands you'd like your darkbot"
			echo "to do, when it connects online. This can range from /msging"
			echo "the channel service login commands (so your darkbot can"
			echo "auto get ops), to /joining a second channel...."
			echo "You can later modify this list by editing the perform.ini"
			echo
			echo "Hit ENTER to begin editing your perform.ini ..."
			read x
			$EEDITOR dat/perform.ini
			echo
			echo "Okay, next is the deop.ini, the format is the same as"
			echo "perform.ini.  What deop.ini is, is a list of commands"
			echo "you would like your darkbot to do, when it finds itself"
			echo "deoped in $CHAN. This can range from /msging $CHAN OP ME!"
			echo "to using other bots/channel services to regain ops."
			echo
			echo "Hit ENTER to begin editing your deop.ini"
			read x
			$EEDITOR dat/deop.ini
			;;

		3)
			./Adduser
			;;
		4)
			./AddServer
			;;
		5)
			echo
			echo
			if [ ! -e "darkbot" ]; then
				echo "I did not find the darkbot binary.... running Makefile now."
				sleep 3
				make
				echo
				sleep 1
			fi
			echo
			QUITIT=yes
			;;
		*)
			echo "\"$CHOICE\" is not a valid selection."
			sleep 1
			;;
	esac
done
echo ""
echo "As always, if you run into problems, you can always"
echo "type 'make debug' and when the bot crashes, type 'backtrace'"
echo "to find out exactly why it happened. If you can't make sense"
echo "of the backtrace, feel free to email it to our mailing list"
echo "at darkbot@darkbot.net and we will be happy to help."
echo ""
