#!/bin/sh
#
# Defaults
#
SERVNAME="Server.DarkFire.NET"
SERVIP="*"
COMMENT="A DarkFire Server"
ADMIN="John Doe"
ADMNICK="john_doe"
ADMADDR="jdoe@darkfire.net"
OPERNAME="jdoe"
OPERADDR="jdoe@*.blah.com"
OPERPASS="EnterAPass"
PORT="5550"
OPRT="6667"
LINKPASS="pickforyourself"

# 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" ircd.conf > mkconf.tmp
#cp mkconf.tmp ircd.conf
#rm mkconf.tmp
#__EOF__

# 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 Options exist, load the info, otherwise tell them to run Config.

if [ -r .options ]; then
	. .options
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=$DPATH/ircd.conf

if [ -r "$CONF" ]; then
	echo "ircd.conf 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 ircd.conf using an editor?"
		echo "If so, specify the editor you want to use. Otherwise,"
		echo "type 'exit'."
		EDITFND="not found"

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

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

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

		if [ -r /usr/bin/joe ]; then
			EDITFND="joe"
		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

	cp $CONF $DPATH/ircd.conf.bak
	rm $CONF
	echo "ircd.conf has been renamed to ircd.conf.bak"
else
	echo "ircd.conf not found, good!"
fi

echo " "
echo "What will your server name be?"
echo $n " [$SERVNAME] -> $c"
read cc
if [ ! -z "$cc" ]; then
	SERVNAME="$cc"
fi

echo " "
echo "Server comment in /links?"
echo $n " [$COMMENT] -> $c"
read cc
if [ ! -z "$cc" ]; then
	COMMENT="$cc"
fi

echo " "
echo "Server IP if using virtual IP"
echo "If this is the only IRCD on this machine to use the ports"
echo "you specify, you may just hit ENTER."
echo $n " [$SERVIP] -> $c"
read cc
if [ ! -z "$cc" ]; then
	SERVIP="$cc"
fi

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

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

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

echo " "
echo "What is your e-mail address?"
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?"
echo $n " [$OPERNAME] -> $c"
read cc
if [ ! -z "$cc" ]; then
	OPERNAME="$cc"
fi

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

echo " "
echo "What is your user@host mask that will be used for your O:line?"
echo $n " [$OPERADDR] -> $c"
read cc
if [ ! -z "$cc" ]; then
	OPERADDR="$cc"
fi

cat > $CONF << __EOF__
#
# ircd.conf file - generated by binary's ircd.conf creator on $DATE
#
# M:line - general server info (M:server.name:<ip|*>:server comment:port)
M:$SERVNAME:$SERVIP:$COMMENT:$PORT
#
# A:line - /admin info (A:real name:nickname:email addy)
A:$ADMIN:$ADMNICK:$ADMADDR
#
# Y:lines - denoting classes (Y:class:pingfreq:connfreq:maxlinks:maxsendq)
# Client Y:line(s)
Y:1:90:0:$MAXCONNECTIONS:100000
# Oper Y:line
Y:10:90:0:10:100000
# Server Y:lines
Y:30:300:300:1:1000000
#
# I:lines - allows clients to connect
#  (I:addymask:[passwd]:addymask:[port]:class
# NOTE: TO make a special oper port, make an I:line for each of your other
#  ports, using class 1, and then a separate one for your oper port and make
#  it class 10
I:*@*::*@*::1
#
# O:lines - IRCop status (O:user@host.mask:passwd:opername:operflags:2)
#  The operflags field has many possible flags, but these are the main ones:
# o - local oper	O - global oper		A - server admin
# a - services admin	R - /restart access	D - /die access
# z - zline		s - remote kline/rehash	* - all but Aa
O:$OPERADDR:$OPERPASS:$OPERNAME:Aa*:10
#
# K:lines - kill lines (K:hostmask:reason:identmask:usermask)
# C/N[/H] lines
#   C/N must be in pairs
#   H allows a server to have other servers downstream
# Syntax:
#   C:hostmask:sendpassword:server.name:[port if autoconnect]:class
#   N:hostmask:receivepassword:server.name:*@*:class
# If you want the server to be able to have downlinks,
#   H:leafmask::hub.server
#
__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 "Hostname or ip for the other server? Must be specified."
		echo $n " [] -> "
		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 " "
	echo "Password to expect from the other server?"
	echo $n " [$CLP] -> $c"
	read NLP
	if [ -z "$NLP" ]; then
		NLP="$CLP"
	fi

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

	echo " "
	HUB="bad"
	while [ "$HUB" = "bad" ]; do
		echo "Will this server be a hub (have downlinks)?"
		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

	echo "# Server $THESERV (autoconnect=$AUTOCONN hub=$HUB)" >> $CONF
	echo "C:$SHOST:$CLP:$THESERV:$AUTOPORT:30" >> $CONF
	echo "N:$SHOST:$NLP:$THESERV:*@*:30" >> $CONF
	if [ "$HUB" = "yes" ]; then
		echo "H:*::$THESERV" >> $CONF
	fi

	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__
#
# U:lines - give this to fake servers that need extra power, such as services
#
U:services.darkfire.net:*:*
U:stats.darkfire.net:*:*
#
# Q:lines - restricted nick/server names (Q::reason:mask)
Q::Reserved for services:*C*h*n*S*r*v*
Q::Reserved for services:*N*i*k*S*r*v*
Q::Reserved for services:*M*m*o*S*r*v*
Q::Reserved for services:*H*l*p*S*r*v*
Q::Reserved for services:*O*p*r*S*r*v*
Q::Reserved for operators:*IRC*op*
Q::Reserved for operators:*IRC*0p*
Q::Reserved for operators:*Sys*op*
Q::Reserved for operators:*Sys*0p*
Q::Reserved for operators:Management
Q::Reserved for operators:Suggestions
Q::Reserved for operators:*CS*op*
Q::Reserved for operators:Global
#
# P:lines - extra ports (P:<ip|*>:*:*:port)
__EOF__

cc="blank"
echo " "
echo " "
echo "   Would you like any extra ports, other than $PORT? Enter them here."
echo "Type 'done' when you are finished. Type 'default' to use 6667 and 7000."
echo "Keep in mind that each port you declare will tie up ONE socket and"
echo "thus keep one client from connecting, if you are concerned about"
echo "filling all your connections."

while [ "$cc" != "done" ]; do
	echo $n " [$OPRT] -> $c"
	read cc
	if [ -z "$cc" ]; then
		cc="$OPRT"
	fi
	if [ "$cc" = "default" ]; then
		echo "P:*:*:*:6667" >> $CONF
		echo "P:*:*:*:7000" >> $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:$SERVIP:*:*:$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="7000"
			;;
		7000) OPRT="6668"
			;;
		*) OPRT="done"
			;;
	esac
done

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

cat << __EOF__


    OK, ircd.conf has been generated according to what you specified.
Would you like to view and/or edit it? If so, specify the editor you use (or
hit enter to use the one chosen for you), otherwise type 'exit' to quit.
__EOF__

EDITFND="not found"

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

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

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

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

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

while [ "c" = "c" ]; do
	echo $n " [$EDITFND] -> "
	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
cat << __EOF__
    From now on, to edit your server configuration, type
		$EDITOR $CONF
    and once you save it, just /rehash from your IRC connection.
__EOF__
		echo $n " [ENTER] to continue -> $c"
		read $DUMMY
		$EDITOR $CONF
		exit 1
	fi

	if [ -f /usr/bin/$EDITOR ]; then
cat << __EOF__
    From now on, to edit your server configuration, type
		$EDITOR $CONF
    and once you save it, just /rehash from your IRC connection.
__EOF__
		echo $n " [ENTER] to continue -> $c"
		read $DUMMY
		/usr/bin/$EDITOR $CONF
		exit 1
	fi

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

