#!/bin/sh
#
OPTIONS_H=include/options.h
OPTIONS=".options"

SERVICES_NAME="services.darkfire.net"
KLINE_ADDRESS=""
PATHNOW=`pwd`
DPATH="$PATHNOW"
SPATH="$PATHNOW/ircd"
HUB=""
CRYPT_OPER_PASSWORD=""
CRYPT_LINK_PASSWORD=""
LISTEN_SIZE="5"
MAXSENDQLENGTH="3000000"
BUFFERPOOL="(9 * MAXSENDQLENGTH)"
NICKNAMEHISTORYLENGTH="2000"
MAXCONNECTIONS="1024"
SHOWOPERS="Yes"
REMOTE_KLINE="1"
REMOTE_REHASH="1"
RES=""

TMP=/tmp/.Configtmp$$.c
EXEC=/tmp/.Configtmp$$
PLATE=/tmp/.ConPlate$$
c=''
n=''

2>/dev/null
if [ "`eval echo -n 'a'`" = "-n a" ] ; then
        c='\c'
else
        n='-n'
fi

if [ -r "$OPTIONS" ] ; then
    . $OPTIONS
fi

if [ ! -f include/setup.h ] ; then
     echo "You must run configure first!"
     exit
fi

COMP=`egrep "^CC=" src/Makefile 2>/dev/null | sed -e 's/^[^=]*[      ]*=\(.*\)/\1/'`

echo
echo "Welcome to the config for Darkfire IRCD, Version df-1.2.0."

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$DPATH"
    echo ""
    echo "What directory are all the server configuration files in?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
        read cc
    else
        cc=""
        runonce=Yes
    fi
    if [ -z "$cc" ] ; then
        cc=$FOO
    fi
done
DPATH=$cc

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$SPATH"
    echo ""
    echo "What is the explicit path to where the ircd binary will be"
    echo "installed? This should point to a file, not a directory."
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
        read cc
    else
        cc=""
        runonce=Yes
    fi
    if [ -z "$cc" ] ; then
        cc=$FOO
    fi
done
SPATH=$cc

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$KLINE_ADDRESS"
    echo " "
    echo "What address should users contact in regards to be K:lined from"
    echo "this server. This should be set to the admins email address."
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" -o -z "$FOO" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Kk][Ll][Ii][Nn][Ee]@[[Aa][Rr][Ee][Aa][Oo][Nn][Ee].[Oo][Rr][Gg])
	    KLINE_ADDRESS=$cc
	    FOO=""
	    echo " "
	    while [ -z "$FOO" ] ; do
		FOO="No"
		echo "Kline@darkfire.net does not handle server-specific K:lines."
		echo "Are you sure you want to set the address to kline@darkfire.net?"
		echo $n "[$FOO] -> $c"
		if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
		    read cc
		else
		    cc=""
		    runonce=Yes
		fi
		if [ -z "$cc" ] ; then
		    cc=$FOO
		fi
		case "$cc" in
		    [Yy]*)
			;;
		    [Nn]*)
			KLINE_ADDRESS=""
			;;
		    *)
			echo ""
			echo "You need to enter either Yes or No here..."
			echo ""
			FOO=""
			;;
		esac
	    done
	    FOO="$KLINE_ADDRESS"
	    ;;
	*@*.*)
	    KLINE_ADDRESS=$cc
	    FOO="$KLINE_ADDRESS"
	    ;;
	*)
	    echo " "
	    echo "Read the instructions and try again...  You did not enter a"
	    echo "proper email address (user@host.domain)."
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$SERVICES_NAME"
    echo " "
    echo "If you have network services and would like to make use of "
    echo "the built in secure services aliasing features, type in the "
    echo "name of your network services server here. If you are a "
    echo "Darkfire admin, you would type in \"services.darkfire.net\" below."
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" -o -z "$SERVICES_NAME" ] ; then
	read cc
	runonce=Yes
    else
	cc=""
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
done
SERVICES_NAME=$cc

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$SHOWOPERS" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Would you like non-opers to be able to see your O:lines?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
	runonce=Yes
    else
	cc=""
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    SHOWOPERS="YES"
	    ;;
	[Nn]*)
	    SHOWOPERS=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$CRYPT_OPER_PASSWORD" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Should operator passwords in ircd.conf be encrypted?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    CRYPT_OPER_PASSWORD="1"
	    ;;
	[Nn]*)
	    CRYPT_OPER_PASSWORD=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$CRYPT_LINK_PASSWORD" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Should incoming link passwords be encrypted in ircd.conf?"
    echo "Note, this applies to N lines only, C lines must remain unencrypted."
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    CRYPT_LINK_PASSWORD="1"
	    ;;
	[Nn]*)
	    CRYPT_LINK_PASSWORD=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    if [ -n "$USE_KLINE_LOGGING" ] ; then
	FOO="Yes"
    else
	FOO="No"
    fi
    echo ""
    echo "Would you like to log Klines and Zlines added/removed by operators?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[Yy]*)
	    USE_KLINE_LOGGING="1"
	    ;;
	[Nn]*)
	    USE_KLINE_LOGGING=""
	    ;;
	*)
	    echo ""
	    echo "You need to enter either Yes or No here..."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$LISTEN_SIZE"
    echo ""
    echo "What listen() backlog value do you wish to use? Some servers"
    echo "have problems with more than 5, others work fine much more."
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[1-9]*)
	    LISTEN_SIZE="$cc"
	    ;;
	*)
	    echo ""
	    echo "You need to enter a number here."
	    echo ""
	    FOO=""
	    ;;
    esac
done


FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$NICKNAMEHISTORYLENGTH"
    echo ""
    echo "How far back do you want to keep the nickname history?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[1-9]*)
	    NICKNAMEHISTORYLENGTH="$cc"
	    ;;
	*)
	    echo ""
	    echo "You need to enter a number here."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$MAXSENDQLENGTH"
    echo ""
    echo "What sendq length do you wish to have?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[1-9]*)
	    MAXSENDQLENGTH="$cc"
	    ;;
	*)
	    echo ""
	    echo "You need to enter a number here."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$BUFFERPOOL"
    echo ""
    echo "What size of a bufferpool (total of ALL sendq's in use) do you"
    echo "do you wish to have?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	\([1-9]*\))
	    BUFFERPOOL="$cc"
	    ;;
	*)
	    echo ""
	    echo "You need to enter a number here, either based on"
	    echo "MAXSENDQLENGTH or a literal value.  Also, this value"
	    echo "MUST be enclosed in parentheses -- (9*MAXSENDQLENGTH), for example."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
    FOO="$MAXCONNECTIONS"
    echo ""
    echo "How many file descriptors (or sockets) can the irc server use?"
    echo $n "[$FOO] -> $c"
    if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
	read cc
    else
	cc=""
	runonce=Yes
    fi
    if [ -z "$cc" ] ; then
	cc=$FOO
    fi
    case "$cc" in
	[1-9][0-9][0-9]*)
	    MAXCONNECTIONS="$cc"
	    ;;
	*)
	    echo ""
	    echo "You need to enter a number here, greater or equal to 100."
	    echo ""
	    FOO=""
	    ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
        if [ -n "$HUB" ] ; then
                FOO="Yes"
        else
                FOO="No"
        fi
        echo ""
	echo "Should this server be configured to accept more than one link, that is,"
	echo "configured as a hub? Only certain servers on DarkFire should set this"
	echo "option, if unsure please contact servapp@darkfire.net"
        echo $n "[$FOO] -> $c"
        if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
                read cc
        else
                cc=""
                runonce=Yes
        fi
        if [ -z "$cc" ] ; then
                cc=$FOO
        fi   
    case "$cc" in     
        [Yy]*)
                HUB="1"   
            ;;
        [Nn]*)
                HUB=""
            ;;
        *) 
            echo ""
            echo "You need to enter either Yes or No here..."
            echo ""
            FOO=""
            ;;
    esac
done

FOO=""
runonce=""
while [ -z "$FOO" ] ; do
        if [ -n "$RES" ] ; then
                FOO="Yes"
        else
                FOO="No"
        fi
        echo ""
	echo "Does this server require the extra res sources?"
	echo "Say yes if you get compile errors about res_init."
        echo $n "[$FOO] -> $c"
        if [ -z "$AUTO_CONFIG" -o -n "$runonce" ] ; then
                read cc
        else
                cc=""
                runonce=Yes
        fi
        if [ -z "$cc" ] ; then
                cc=$FOO
        fi
    case "$cc" in
        [Yy]*)
		echo
		echo "Editing src/Makefile to include extra RES_SRC files"
		cat src/Makefile | sed s/'\#RES_SRC'/RES_SRC/ > src/Makefile
            ;;
        [Nn]*)
		RES="0"
            ;;
        *)
            echo ""
            echo "You need to enter either Yes or No here..."
            echo ""
            FOO=""
            ;;
    esac
done

#######################################################################
# Ask for secret.h stuff here.

echo ""
echo "Disguise algorithm (secret.h):"
echo "To improve security of disguising hostnames with +d, there is the"
echo "option of having a separate file secret.h to contain the algorithm."
echo "Not having one means using the default disguise algorithm.  If you"
echo "have a secret.h file, enter the pathname for it, otherwise, enter"
echo "a blank string.  If you think you may need this file, ask about it."
echo ""
if [ -r "include/secret.h" ]; then
    echo "You currently have a secret.h file in place.  If you just hit"
    echo "ENTER, the one already there will be used."
    echo "Enter the pathname for a new secret.h file to override the"
    echo "old one."
    FOO="include/secret.h"
else
    echo "You are lacking a secret.h file.  If you just hit ENTER, then"
    echo "it will be assumed that you will not be using one."
    echo "If you would like to use one, specify where the path to the"
    echo "current location of the file to be secret.h."
    FOO=""
fi
echo $n "[$FOO] -> $c"
if [ -z "$AUTO_CONFIG" ] ; then
        read cc
else
        cc=""
fi
if [ -z "$cc" ]; then
        cc=$FOO
fi
if [ -z "$cc" ]; then
        rm -f include/secret.h
fi
if [ -n "$cc" -a "$cc" != "include/secret.h" ] ; then
    if [ -r "$cc" ]; then
        cp $cc include/secret.h
        echo "Found your choice... making this file unviewable to others."
        chmod 600 $cc
    else
        echo " *** Specified file $cc does not exist!"
    fi
fi
if [ ! -r "include/secret.h" ]; then
echo " *** Using default (blank) secret.h."
cat >include/secret.h <<__EOF__
/* null secret.h file -- use defaults */
__EOF__
fi
chmod 600 include/secret.h

######################################################################

#
# create the $OPTIONS_H file
#
rm -f $OPTIONS_H
cat > $OPTIONS_H << __EOF__
/*
 * Darkfire Compile Configuration File
 * VERSION: $IRCD_VERSION
 * DATE:    $CONF_DATE
 *
 * This file is automatically generated and will be updated each time you
 * run Config.  YOU SHOULD NOT EDIT THIS FILE.  Instead, edit "$OPTIONS" in
 * the same directory as Config, and rerun to regenerate this file.
 */

#define DPATH "$DPATH"
#define SPATH "$SPATH"
#define LISTEN_SIZE $LISTEN_SIZE
#define MAXSENDQLENGTH $MAXSENDQLENGTH
#define BUFFERPOOL $BUFFERPOOL
#define MAXCONNECTIONS $MAXCONNECTIONS
#define NICKNAMEHISTORYLENGTH $NICKNAMEHISTORYLENGTH
__EOF__
if [ -n "$SHOWOPERS" ] ; then
    echo "#define SHOWOPERS" >> $OPTIONS_H
else
    echo "#undef SHOWOPERS" >> $OPTIONS_H
fi
if [ -n "$HUB" ] ; then
    echo "#define HUB 1" >> $OPTIONS_H
else
    echo "#undef HUB" >> $OPTIONS_H
fi
if [ -n "$CRYPT_OPER_PASSWORD" ] ; then
    echo "#define CRYPT_OPER_PASSWORD 1" >> $OPTIONS_H
else
    echo "#undef CRYPT_OPER_PASSWORD" >> $OPTIONS_H
fi
if [ -n "$CRYPT_LINK_PASSWORD" ] ; then
    echo "#define CRYPT_LINK_PASSWORD 1" >> $OPTIONS_H
else
    echo "#undef CRYPT_LINK_PASSWORD" >> $OPTIONS_H
fi
if [ -n "$REMOTE_KLINE" ] ; then
    echo "#undef NO_REMOTE_KLINE" >> $OPTIONS_H
else
    echo "#define NO_REMOTE_KLINE 1" >> $OPTIONS_H
fi
if [ -n "$REMOTE_REHASH" ] ; then
    echo "#undef NO_REMOTE_REHASH" >> $OPTIONS_H
else
    echo "#define NO_REMOTE_REHASH 1" >> $OPTIONS_H
fi
if [ -n "$USE_KLINE_LOGGING" ] ; then
    echo "#define USE_KLINE_LOGGING 1" >> $OPTIONS_H
else
    echo "#undef USE_KLINE_LOGGING" >> $OPTIONS_H
fi
if [ -n "$SERVICES_NAME" ] ; then
    echo "#define SERVICES_NAME \"$SERVICES_NAME\"" >> $OPTIONS_H
fi
if [ -n "$KLINE_ADDRESS" ] ; then
    echo "#define KLINE_ADDRESS \"$KLINE_ADDRESS\"" >> $OPTIONS_H
fi
#
# create the persistant file
#
rm -f $OPTIONS
cat > $OPTIONS << __EOF__
#
# Darkfire Compile Configuration File
# VERSION: $IRCD_VERSION
# DATE:    $CONF_DATE
#
# This file is automatically generated and will be updated each time you
# run Config.  You should retain a copy of this to help migrate to future
# versions of Darkfire with ease.
#
SERVICES_NAME="$SERVICES_NAME"
KLINE_ADDRESS="$KLINE_ADDRESS"
DPATH="$DPATH"
SPATH="$SPATH"
HUB="$HUB"
CRYPT_OPER_PASSWORD="$CRYPT_OPER_PASSWORD"
CRYPT_LINK_PASSWORD="$CRYPT_LINK_PASSWORD"
REMOTE_KLINE="$REMOTE_KLINE"
REMOTE_REHASH="$REMOTE_REHASH"
LISTEN_SIZE="$LISTEN_SIZE"
MAXSENDQLENGTH="$MAXSENDQLENGTH"
BUFFERPOOL="$BUFFERPOOL"
MAXCONNECTIONS="$MAXCONNECTIONS"
NICKNAMEHISTORYLENGTH="$NICKNAMEHISTORYLENGTH"
SHOWOPERS="$SHOWOPERS"
__EOF__

#cat Makefile | sed s/'FDLIMIT=.*'/FDLIMIT=-DFD_SETSIZE=${MAXCONNECTIONS}/ > Makefile

echo
echo "Configuration is now complete. Please edit include/config.h"
echo "for more specific options. To compile your server, type 'make'."
