#!/bin/sh
# $Id$
# Defaults
#
TIME=`date +"%H:%M:%S %Z"`
DATE=`date +"%a, %b %e %Y"`
IRCDCONF="ircd.conf"
SERVNAME="Server.IRC.net"
COMMENT="IRC server!"
ADMIN="Admin-name"
ADMNICK="Admin"
ADMADDR="admin@no.where.com"
OPERNAME="admin"
OPERADDR="*@*"
OPERPASS="password"
NUMERIC=""
PORT="6667"
OPRT="default"
LINKPASS="linkpass"
VIRTUAL="1.2.3.4"
ENCRYPT="Yes"
FLAGS="OAZHWe"
RESTARTPASS="restartpass"
DIEPASS="diepass"
SERVICES="Services.IRC-Net.Org"

# these are so I can use stuff like $1 without it replacing it...like by
# using $dlrsgn$uno for $1
dlrsgn="$"
uno="1"
dos="2"

# remove the temp file if it exists
rm -f mkconf.tmp

# make a quick script for replacing one line with another

cat > repl_str << __EOF__
sed -e "s@^$dlrsgn$uno\(.*\)@$dlrsgn$dos" $IRCDCONF > mkconf.tmp
cp mkconf.tmp $IRCDCONF
rm mkconf.tmp
__EOF__

# mark repl_str as an executable
chmod +x repl_str
rm -f repl_str
# Checking out how to specify not to make a new line with the current OS
c=''
n=''
2>/dev/null
if [ "`eval echo -n 'a'`" = "-n a" ]; then
	c='\c'
else
	n='-n'
fi

# If Settings exist, load the info, otherwise tell them to run Config.

if [ -r Settings ]; then
	. Settings
else
	echo "Hmm, you need to run Config first!"
	echo "Type ./Config"
	echo "If you have run Config, make sure you are in the directory"
	echo "you ran it from."
	exit 1
fi

CONF=$IRCDCONF

if [ -r "$CONF" ]; then
	echo "$IRCDCONF found"
	echo " "
	echo "   I don't suggest running this with the conf file already"
	echo "existing, since you will lose all the data in your conf file."
	echo "This script is just for creating one. If you want to recreate"
	echo "a conf file, type 'recreate'."
	echo $n " [exit] -> $c"
	read cc

	if [ "$cc" != "recreate" ]; then
		echo " "
		echo "   OK, do you want to edit $IRCDCONF using an editor?"
		echo "If so, specify the editor you want to use. Otherwise,"
		echo "type 'exit'."
		EDITFND="not found"

                if [ -r /usr/bin/pico ]; then
                        EDITFND="pico"
                fi

                if [ -r /usr/bin/joe ]; then
                        EDITFND="joe"
                fi

		if [ -r /usr/bin/vi ]; then
			EDITFND="vi"
		fi

		if [ -r /usr/bin/vim ]; then
			EDITFND="vim"
		fi

		while [ "c" = "c" ]; do
			echo $n " [$EDITFND] -> $c"
			read EDITOR

			if [ -z "$EDITOR" ]; then
				EDITOR="$EDITFND"
			fi

			if [ "$EDITOR" = "exit" ]; then
				echo "Have a nice day."
				exit 1
			fi

			if [ -f $EDITOR ]; then
				$EDITOR $CONF
				exit 1
			fi

			if [ -f /usr/bin/$EDITOR ]; then
				/usr/bin/$EDITOR $CONF
				exit 1
			fi

			echo "   Editor not found. Specify a valid editor or"
			echo "type 'exit' to quit this script."
		done

	fi

	mv $CONF "$CONF.saved"
	rm $CONF
	echo "$IRCDCONF has been renamed to ircd.conf.saved"
else
	echo "$IRCDCONF is not found. (good)"
fi

clear
echo "|=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=|"
echo "| Welcome to the $IRCDCONF generator. |"
echo "|=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=|"
echo " "
echo "What will your server name be? (ie: $SERVNAME)"
echo $n " [$SERVNAME] -> $c"
read cc
if [ ! -z "$cc" ]; then
	SERVNAME="$cc"
fi

echo " "
echo "What is your IRC server's IP? (ie: $VIRTUAL)"
echo "Use * to bind to all interfaces"
echo $n " [$VIRTUAL] -> $c"
read cc
if [ ! -z "$cc" ]; then
        VIRTUAL="$cc"
fi

echo " "
echo "Server comment? (ie: Main Hub Server)"
echo $n " [$COMMENT] -> $c"
read cc
if [ ! -z "$cc" ]; then
	COMMENT="$cc"
fi

echo " "
echo "Main port? (ie: 6667)"
echo $n " [$PORT] -> $c"
read cc
if [ ! -z "$cc" ]; then
	PORT="$cc"
fi

echo " "
echo "What server numeric does the server have (range = 1..256)"
echo "Must NOT be the same as other servers you link with"
echo $n " [] -> $c"
read cc
if [ ! -z "$cc" ]; then
	NUMERIC="$cc"
fi

echo " "
echo "What is your real name? (ie: John Doe)"
echo $n " [$ADMIN] -> $c"
read cc
if [ ! -z "$cc" ]; then
	ADMIN="$cc"
fi

echo " "
echo "What is your IRC nickname? (ie: JD)"
echo $n " [$ADMNICK] -> $c"
read cc
if [ ! -z "$cc" ]; then
	ADMNICK="$cc"
fi

echo " "
echo "What is your e-mail address? (ie: john@doe.com)"
echo $n " [$ADMADDR] -> $c"
read cc
if [ ! -z "$cc" ]; then
	ADMADDR="$cc"
fi

echo " "
OPERNAME="$ADMNICK"
echo "What will your opername be for your O:line? (ie: johndoe)"
echo $n " [$OPERNAME] -> $c"
read cc
if [ ! -z "$cc" ]; then
	OPERNAME="$cc"
fi

echo " "
echo "What oper flags do you want to have? (ie: OAWZ)"
echo "Some flags to choose from:"
echo "  o = local oper"
echo "  O = global oper"
echo "  N = network administrator"
echo "  A = server administrator"
echo "  C = co administrator"
echo "  T = technical administrator"
echo "  read unrealircd.doc for more info about O:line flags)"
echo $n " [$FLAGS] -> $c"
read cc
if [ ! -z "$cc" ]; then
	FLAGS="$cc"
fi

        echo " "
        ENCRYPT=""
        ENCRYPT="bad"
        while [ "$ENCRYPT" = "bad" ]; do
                echo "Do you use encrypted oper passwords?"
                echo $n " [Yes] -> $c"
                read cc
                if [ -z "$cc" ]; then
                        cc="Yes"
                fi
                case "$cc" in
                        [Yy]*)
                                ENCRYPT="Yes"
                                ;;
                        [Nn]*)
                                ENCRYPT="No"
                                ;;
                        *)
                                echo "Please specify yes or no."
                                ENCRYPT="bad"
                                ;;
                esac
        done

if [ "$ENCRYPT" = "Yes" ] ; then
echo "Compile the file called mkpasswd.c in the 'crypt' directory"
echo "Then run it (./mkpasswd) and input the passwd to be encrypted"
echo "Then copy the output into the passwd slot in the O:line"
echo "NOTE: You can encrypt on irc via /mkpasswd <passwd>"
fi

echo " "
echo "What will your /oper password be? (ie: snoopy67)"
echo $n " [$OPERPASS] -> $c"
read cc
if [ ! -z "$cc" ]; then
	OPERPASS="$cc"
fi

echo " "
echo "What is your user@host mask on IRC? (ie: *@*.toronto.globalserve.net)"
echo $n " [$OPERADDR] -> $c"
read cc
if [ ! -z "$cc" ]; then
	OPERADDR="$cc"
fi

echo " "
echo "What do you want your die password to be?"
echo $n " [$DIEPASS] -> $c"
read cc
if [ ! -z "$cc" ]; then
        DIEPASS="$cc"
fi

echo " "
echo "What do you want your restart password to be?"
echo $n " [$RESTARTPASS] -> $c"
read cc
if [ ! -z "$cc" ]; then
        RESTARTPASS="$cc"
fi

echo " "
echo "If your running services, or linking to a network that has"
echo "services, please state the services servername."
echo "If you are running a non-services network, just type 'next'"
echo $n " [$SERVICES] -> $c"
read cc

#if [ ! -z "$cc" ]; then
#        SERVICES="$cc"
#fi

if [ "$cc" = "next" ]; then
        SERVICES="No.Services.Selected"
elif [ ! -z "$cc" ]; then
        SERVICES="$cc"
fi

cat > $CONF << __EOF__
########################
#
# Filename:  $IRCDCONF
#  Created:  $DATE - $TIME
#
########################

############ Server Info ############
M:$SERVNAME:$VIRTUAL:$COMMENT:$PORT:$NUMERIC
#####################################

############ Administrator Information #############
A:$ADMIN:$ADMNICK:$ADMADDR
####################################################

############ Y-lines #############
# Client Y:lines
Y:1:90:0:245:100000
# Server Y:lines
Y:50:300:600:1:1000000
##################################

############ I/Access Lines #############
I:*@*::*@*::1
#########################################

############ X:LINE Die/Restart Password ############
X:$DIEPASS:$RESTARTPASS
#####################################################

## O-line (O:hostmask:password:opername:flags:1) ##
O:$OPERADDR:$OPERPASS:$OPERNAME:$FLAGS:1
###################################################

__EOF__


echo " "
echo "   Would you like to configure any servers for linking?"
echo "Type the servername, or type 'done' when finished."
echo $n " [done] -> $c"
read cc

if [ -z "$cc" ]; then
	cc="done"
fi

while [ "$cc" != "done" ]; do
	THESERV="$cc"

	SHOST="none"
	while [ "$SHOST" = "none" ]; do
		echo " "
		echo "Hostname or ip for the other server? Must be specified."
		echo $n " [] -> $c"
		read SHOST
	done	

	echo " "
	echo "Password to send to the other server?"
	echo $n " [$LINKPASS] -> $c"
	read CLP
	if [ -z "$CLP" ]; then
		CLP="$LINKPASS"
	fi

	echo " "
	AUTOPORT=""
	AUTOCONN="bad"
	while [ "$AUTOCONN" = "bad" ]; do
		echo " "
		echo "Should we autconnect to this server?"
		echo $n " [Yes] -> $c"
		read cc
		if [ -z "$cc" ]; then
			cc="Yes"
		fi
		case "$cc" in
			[Yy]*)
				AUTOCONN="yes"
				echo "What port should we autoconnect to?"
				echo $n " [7029] -> $c"
				read AUTOPORT
				if [ -z "$AUTOPORT" ]; then
					AUTOPORT="7029"
				fi
				;;
			[Nn]*)
				AUTOCONN="no"
				;;
			*)
				echo "Please specify yes or no."
				AUTOCONN="bad"
				;;
		esac
	done

	echo " "
	HUB="bad"
	while [ "$HUB" = "bad" ]; do
		echo " "
		echo "Will this server be a hub?"
		echo $n " [Yes] -> $c"
		read cc
		if [ -z "$cc" ]; then
			cc="Yes"
		fi
		case "$cc" in
			[Yy]*)
				HUB="Yes"
				;;
			[Nn]*)
				HUB="no"
				;;
			*)
				echo "Please specify yes or no."
				HUB="bad"
				;;
		esac
	done

	if [ "$HUB" = "Yes" ]; then
		echo "############ HUB LINES #############" >> $CONF
		echo "H:*:*:$THESERV" >> $CONF
		echo "####################################" >> $CONF
		echo "" >> $CONF
	fi

        echo "############ C/N LINES #############" >> $CONF
        echo "C:$SHOST:$CLP:$THESERV:$AUTOPORT:50" >> $CONF
        echo "N:$SHOST:$CLP:$THESERV::50" >> $CONF
        echo "####################################" >> $CONF
	echo "" >> $CONF

	echo " "
	echo "Type the servername, or type 'done' when finished."
	echo $n " [done] -> $c"
	read cc
	if [ -z "$cc" ]; then
		cc="done"
	fi
done

cat >> $CONF << __EOF__
############ Uline for Services ############
U:$SERVICES:*:*
############################################

############ Q-Lined NickNames ############
Q::Reserved for services:*C*h*a*n*S*e*r*v*
Q::Reserved for services:*N*i*c*k*S*e*r*v*
Q::Reserved for services:*M*e*m*o*S*e*r*v*
Q::Reserved for services:*H*e*l*p*S*e*r*v*
Q::Reserved for services:*O*p*e*r*S*e*r*v*
Q::Reserved for services:*I*n*f*o*S*e*r*v*
Q::Reserved for Administrator:*Admin*
Q::Reserved for ircops:*IRC*op*
Q::Reserved for ircops:*Oper*
Q::Bug in mIRC:Status
###########################################

############ PORT LINES #############
__EOF__

cc="blank"
echo " "
echo " "
echo "   Would you like any extra ports, other than 6667? Enter them here."
echo "Type 'done' when you are finished. Type 'default' to use 6665-6669/7000."
while [ "$cc" != "done" ]; do
	echo $n " [$OPRT] -> $c"
	read cc
	if [ -z "$cc" ]; then
		cc="$OPRT"
	fi
	if [ "$cc" = "default" ]; then
		echo "P:$VIRTUAL:*:*:6660" >> $CONF
		echo "P:$VIRTUAL:*:*:6661" >> $CONF
		echo "P:$VIRTUAL:*:*:6662" >> $CONF
		echo "P:$VIRTUAL:*:*:6663" >> $CONF
		echo "P:$VIRTUAL:*:*:6664" >> $CONF
                echo "P:$VIRTUAL:*:*:6665" >> $CONF
                echo "P:$VIRTUAL:*:*:6666" >> $CONF
                echo "P:$VIRTUAL:*:*:6668" >> $CONF
                echo "P:$VIRTUAL:*:*:6669" >> $CONF
                echo "P:$VIRTUAL:*:*:7000" >> $CONF
		echo "P:$VIRTUAL:*:*:7029" >> $CONF
		echo "#####################################" >> $CONF
		cc="done"
	fi
	if [ "$cc" != "done" ]; then
		if [ $cc = "$PORT" ]; then
			echo "  No, $PORT is already defined in the M:line,"
			echo "please use a port other than $PORT."
		else
			echo "P:$VIRTUAL:*:*:$cc" >> $CONF
		fi
	fi
	case "$OPRT" in
		6660) OPRT="done"
			;;
		6661) OPRT="6660"
			;;
		6662) OPRT="6661"
			;;
		6663) OPRT="6662"
			;;
		6664) OPRT="6663"
			;;
		6665) OPRT="6664"
			;;
		6666) OPRT="6665"
			;;
		6669) OPRT="6666"
			;;
		6668) OPRT="6669"
			;;
		6667) OPRT="6668"
			;;
		*) OPRT="done"
			;;
	esac
done

# Make it so ONLY the one who created the conf can read or write.
chmod 700 $IRCDCONF

cat << __EOF__


    OK, $IRCDCONF has been generated according to what you specified.
Make sure you double check for errors in $IRCDCONF. We recommend that
you take a look at your $IRCDCONF right now. Thanks for using UnrealIRCd

__EOF__
		echo "Have a nice day."
		echo ""
		exit 1
