#!/bin/sh
#
# RageIRCd: an advanced Internet Relay Chat daemon (ircd).
# (C) 2000-2005 the RageIRCd Development Team, all rights reserved.
#
# This software is free, licensed under the General Public License.
# Please refer to doc/LICENSE and doc/README for further details.
#
# $Id: config,v 1.85.2.7 2005/02/21 02:32:47 amcwilliam Exp $
#

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

CACHEFILE="config.settings"
CURRENTDIR="`pwd`"
HOME="`echo $HOME`"

PREFIX="$HOME/rageircd"
ENGINE_CALL=""
ENGINE_NAME=""
FDSETSIZE="1024"
MAXBUFFER="24"
SENDQLENGTH="5050000"
ENABLE_DYNAMICMODULES="1"
ENABLE_OPENSSL="1"
OPENSSL_PATH=""
ENABLE_BLALLOC="1"
ENABLE_MMAP="1"
ENABLE_THROTTLE="1"
EXTRA="none"

# These tasks are not written to the cache file
MAKE_SELF_SIGNED=""
ENC_PRIV_KEY=""
QUICK=""
CLEAR=""

display_usage() {
	echo "Compile-Time Server Configuration Interface for RageIRCd v2.0"
	echo "(C) 2000-2005 the RageIRCd Development Team, all rights reserved."
	echo " "
	echo "Usage: config [option]"
	echo " "
	echo "Where optional argument [option] can be any of the following:"
	echo "    -h, --help         Displays this information"
	echo "    -a, --autoconf     Rebuild autoconf files"
	echo "    -q, --quick        Don't ask questions, just run configure"
	echo "    -c, --clear        Clear the $CACHEFILE file before configuring"
}

if test -n "$1"; then
	case "$1" in
		-h|*help)
			display_usage
			exit 0 ;;
		-a|*autoconf)
			echo -n "Rebuilding configure... "
			autoconf configure.ac >configure
			chmod +x configure
			echo "done!"

			echo -n "Rebuilding include/setup.h.in... "
			autoheader configure.ac >include/setup.h.in
			rm -f include/setup.h.in~
			echo "done!"

			rm -rf config.cache config.log autom4te.cache
			exit 0 ;;
		-q|*quick)
			QUICK="1" ;;
		-c|*clear)
			CLEAR="1" ;;
		*)
			display_usage
			echo " "
			echo "$1 is an unknown option."
			exit 0 ;;
	esac
fi
if test -n "$CLEAR" -a -f "$CACHEFILE"; then
	rm -f $CACHEFILE
	echo " "
	echo "Config cache file removed."
fi
if test -f "$CACHEFILE"; then
	. ./$CACHEFILE
fi
if test -n "$QUICK"; then
	if test ! -n "$CONFIGURE"; then
		echo " "
		echo "ERROR: cannot perform quick-configure, no cache file found."
		exit 0
	else
		echo " "
		echo "Cache file found, performing quick-configure."
		echo "$CONFIGURE"
		echo " "
		$CONFIGURE
		exit 0
	fi
fi

CONFIGURE="./configure"

cat <<__EOF__

RageIRCd v2.0 (bluemoon): Compile-Time Server Configuration
-----------------------------------------------------------

This script has been provided as an easy, user-friendly interface to the 
GNU autoconf script. All options will be saved to $CACHEFILE, and
will be loaded each time you re-run this script.

If you are not changing any options when re-running the script, you can
skip all the questions with the --quick (-q) parameter. If you wish to
clear the cache, you can use the --clear (-c) parameter.

__EOF__

VAR="$PREFIX"
echo "What directory would you like to install RageIRCd v2.0 to?"
echo $n "[$VAR] -> $c"
read cc
if test -z "$cc"; then
	PREFIX="$VAR"
else
	PREFIX="$cc"
fi
CONFIGURE="$CONFIGURE --prefix=$PREFIX"

# See if we can automatically detect the best available I/O interface to
# use as a default...

sys_type="`uname -s`"
sys_ver="`uname -r`"
sys_engine=""

case "$sys_type" in
	Linux)
		case "$sys_ver" in
			2.6.*|2.4.*)
				# Some distributions (i.e. Red Hat) have released linux 2.6.x
				# kernel packages that still doesn't include epoll
				if test -f /usr/include/sys/epoll.h; then
					sys_engine="1"
				else
					# rtsigio should be ok
					sys_engine="2"
				fi ;;
			*)
				# Not linux 2.6 or 2.4? Default to poll...
				sys_engine="5" ;;
		esac ;;
	FreeBSD)
		sys_engine="3" ;;
	SunOS)
		case "$sys_ver" in
			5.*)
				# Solaris should be ok?
				sys_engine="4" ;;
			*)
				# SunOS can poll
				sys_engine="5" ;;
		esac ;;
	CYGWIN*)
		sys_engine="5" ;;
	*)
		# Some other weirdness, just default to select to be safe
		sys_engine="6" ;;
esac

# If ENGINE_CALL is blank, use the recommended module
if test "x$ENGINE_CALL" = "x"; then
	ENGINE_CALL="$sys_engine"
fi

VAR=""
while [ -z "$VAR" ]; do
	VAR="$ENGINE_CALL"
	echo " "
	echo "What socket engine module do you want to use? Superior modules should"
	echo "be used over standard POSIX system calls where possible."
	echo " "
	echo "    Name       Available On               Details"
	echo " --------------------------------------------------------------"
	echo " 1. epoll      Linux 2.6 (patched 2.4)    Superior to rtsigio"
	echo " 2. rtsigio    Linux 2.4, 2.6             Superior to poll"
	echo " 3. kqueue     FreeBSD 4.1 and up         Superior to poll"
	echo " 4. devpoll    Solaris 7 and up           Superior to poll"
	echo " 5. poll       POSIX system call          Superior to select"
	echo " 6. select     POSIX system call          Default"
	echo " "
	echo "Please refer to doc/io-engine.txt for further details."
	echo " "
	echo "Your system is:      $sys_type $sys_ver"
	echo "Recommended engine:  $sys_engine"
	# Show them the current module, if there is one
	if test "x$ENGINE_CALL" != "x$sys_engine"; then
		echo "Selected engine:     $ENGINE_CALL"
	fi
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		cc="$VAR"
	fi
	case "$cc" in
		1) ENGINE_CALL_NAME="epoll" ;;
		2) ENGINE_CALL_NAME="rtsigio" ;;
		3) ENGINE_CALL_NAME="kqueue" ;;
		4) ENGINE_CALL_NAME="devpoll" ;;
		5) ENGINE_CALL_NAME="poll" ;;
		6) ENGINE_CALL_NAME="select" ;;
		*)
			echo " "
			echo "You must enter a valid number here."
			VAR="" ;;
	esac
	ENGINE_CALL="$cc"
done
CONFIGURE="$CONFIGURE --with-engine=$ENGINE_CALL_NAME"

VAR=""
while [ -z "$VAR" ]; do
	VAR="$FDSETSIZE"
	echo " "
	echo "What is the maximum amount of file descriptors the server can use? On"
	echo "most modern systems, the default value of 1024 should suffice, although"
	echo "you can check your systems exact fd limit by running either the \"limit\""
	echo "command or the \"ulimit -a\" command. The fd limit is labelled as"
	echo "\"descriptors\" on the first, and \"open files\" on the latter."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		FDSETSIZE="$VAR"
		break;
	fi
	case "$cc" in
		[1-9]*)
			FDSETSIZE="$cc" ;;
		*)
			echo " "
			echo "You must enter a number here."
			VAR="" ;;
	esac
done
CONFIGURE="$CONFIGURE --with-fd-setsize=$FDSETSIZE"

VAR=""
while [ -z "$VAR" ]; do
	VAR="$SENDQLENGTH"
	echo " "
	echo "What would you like the maximum send queue length to be? This is the"
	echo "maximum amount of data a connection can send to the server before the"
	echo "connection is dropped."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		SENDQLENGTH="$VAR"
		break;
	fi
	case "$cc" in
		[1-9]*)
			SENDQLENGTH="$cc" ;;
		*)
			echo " "
			echo "You must enter a number here."
			VAR="" ;;
	esac
done
CONFIGURE="$CONFIGURE --with-sendq-length=$SENDQLENGTH"

VAR=""
while [ -z "$VAR" ]; do
	VAR="$MAXBUFFER"
	echo " "
	echo "What would you like the maximum size of the connect buffer to be? This"
	echo "must not exceed the maximum fd's subtract maximum clients."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		MAXBUFFER="$VAR"
		break;
	fi
	case "$cc" in
		[1-9]*)
			MAXBUFFER="$cc" ;;
		*)
			echo " "
			echo "You must enter a number here."
			VAR="" ;;
	esac
done
CONFIGURE="$CONFIGURE --with-maxbuffer=$MAXBUFFER"

VAR=""
while [ -z "$VAR" ]; do
	if test -n "$ENABLE_DYNAMICMODULES"; then
		VAR="Yes"
	else
		VAR="No"
	fi
	echo " "
	echo "Would you like to compile with dynamic modules support? See"
	echo "doc/dynamic-modules.txt for more information."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		cc="$VAR"
	fi
	case "$cc" in
		[Yy]*)
			ENABLE_DYNAMICMODULES="1"
			CONFIGURE="$CONFIGURE --enable-dynamic-modules" ;;
		[Nn]*)
			ENABLE_DYNAMICMODULES=""
			CONFIGURE="$CONFIGURE --disable-dynamic-modules" ;;
		*)
			echo " "
			echo "You must enter either Yes or No here."
			VAR="" ;;
	esac
done

VAR=""
while [ -z "$VAR" ]; do
	if test -n "$ENABLE_OPENSSL"; then
		if test "x$OPENSSL_PATH" = "x"; then
			VAR="Yes"
		else
			VAR="$OPENSSL_PATH"
		fi
	else
		VAR="No"
	fi
	echo " "
	echo "Would you like to compile with OpenSSL support? This will allow you to"
	echo "listen for secure clients, and setup RC4 server links. If configure fails"
	echo "to find an OpenSSL installation, you can specify a path to search here."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		cc="$VAR"
	fi
	case "$cc" in
		[Yy]*)
			ENABLE_OPENSSL="1"
			CONFIGURE="$CONFIGURE --enable-openssl" ;;
		[Nn]*)
			ENABLE_OPENSSL=""
			CONFIGURE="$CONFIGURE --disable-openssl" ;;
		*)
			ENABLE_OPENSSL="1"
			OPENSSL_PATH="$cc"
			CONFIGURE="$CONFIGURE --enable-openssl=$OPENSSL_PATH" ;;
	esac
done

if test -n "$ENABLE_OPENSSL"; then
	VAR=""
	while [ -z "$VAR" ]; do
		if test -f "cert.pem" -a -f "key.pem"; then
			VAR="No"
		else
			VAR="Yes"
		fi
		echo " "
		echo "Would you like to generate a self-signed SSL certificate?"
		if test -f "cert.pem" -a -f "key.pem"; then
			echo " "
			echo "WARNING! cert.pem and key.pem already exist. Saying Yes here will"
			echo "overwrite them with a new certificate and private key."
		fi
		echo $n "[$VAR] -> $c"
		read cc
		if test -z "$cc"; then
			cc="$VAR"
		fi
		case "$cc" in
			[Yy]*)
				MAKE_SELF_SIGNED="1" ;;
			[Nn]*)
				MAKE_SELF_SIGNED="" ;;
			*)
				echo " "
				echo "You must enter either Yes or No here."
				VAR="" ;;
		esac
	done

	if test -n "$MAKE_SELF_SIGNED"; then
		VAR=""
		while [ -z "$VAR" ]; do
			if test -n "$ENC_PRIV_KEY"; then
				VAR="Yes"
			else
				VAR="No"
			fi
			echo " "
			echo "Would you like to encrypt your SSL private key?"
			echo $n "[$VAR] -> $c"
			read cc
			if test -z "$cc"; then
				cc="$VAR"
			fi
			case "$cc" in
				[Yy]*)
					ENC_PRIV_KEY="1" ;;
				[Nn]*)
					ENC_PRIV_KEY="" ;;
				*)
					echo " "
					echo "You must enter either Yes or No here."
					VAR="" ;;
			esac
		done
	fi
fi

VAR=""
while [ -z "$VAR" ]; do
	if test -n "$ENABLE_BLALLOC"; then
		VAR="Yes"
	else
		VAR="No"
	fi
	echo " "
	echo "Would you like to compile with memory block allocator support? This allows"
	echo "quicker memory allocation of the most commonly used structures (eg, client"
	echo "structures), allowing increased server performance under high client loads."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		cc="$VAR"
	fi
	case "$cc" in
		[Yy]*)
			ENABLE_BLALLOC="1"
			ENABLE_MMAP="1"
			CONFIGURE="$CONFIGURE --enable-blalloc" ;;
		[Nn]*)
			ENABLE_BLALLOC=""
			ENABLE_MMAP=""
			CONFIGURE="$CONFIGURE --disable-blalloc" ;;
		*)
			echo " "
			echo "You must enter either Yes or No here."
			VAR="" ;;
	esac
done

if test -n "$ENABLE_BLALLOC"; then
	VAR=""
	while [ -z "$VAR" ]; do
		if test -n "$ENABLE_MMAP"; then
			VAR="Yes"
		else
			VAR="No"
		fi
		echo " "
		echo "Would you like to enable mmap memory allocation support? If enabled, the block"
		echo "allocator will call the mmap system call instead of malloc when allocating"
		echo "blocks. This is more economical in the long run, as when this memory is freed"
		echo "it will be returned to the page file instead of being stored and unused under"
		echo "the process."
		echo $n "[$VAR] -> $c"
		read cc
		if test -z "$cc"; then
			cc="$VAR"
		fi
		case "$cc" in
			[Yy]*)
				ENABLE_MMAP="1"
				CONFIGURE="$CONFIGURE --enable-mmap" ;;
			[Nn]*)
				ENABLE_MMAP=""
				CONFIGURE="$CONFIGURE --disable-mmap" ;;
			*)
				echo " "
				echo "You must enter either Yes or No here."
				VAR="" ;;
		esac
	done
fi

VAR=""
while [ -z "$VAR" ]; do
	if test -n "$ENABLE_THROTTLE"; then
		VAR="Yes"
	else
		VAR="No"
	fi
	echo " "
	echo "Would you like to compile with connection throttling support? This protects the"
	echo "server against connection flooding."
	echo $n "[$VAR] -> $c"
	read cc
	if test -z "$cc"; then
		cc="$VAR"
	fi
	case "$cc" in
		[Yy]*)
			ENABLE_THROTTLE="1"
			CONFIGURE="$CONFIGURE --enable-throttle" ;;
		[Nn]*)
			ENABLE_THROTTLE=""
			CONFIGURE="$CONFIGURE --disable-throttle" ;;
		*)
			echo " "
			echo "You must enter either Yes or No here."
			VAR="" ;;
	esac
done

VAR="$EXTRA"
echo " "
echo "Please enter any additional parameters to pass to configure."
echo "Entering \"none\" will effect a null entry."
echo $n "[$VAR] -> $c"
read cc
if test -z "$cc"; then
	EXTRA="$VAR"
else
	EXTRA="$cc"
fi
if test "x$EXTRA" != "xnone" -a "x$EXTRA" != "x"; then
	CONFIGURE="$CONFIGURE $EXTRA"
fi

if test -f $CACHEFILE; then
	rm -f $CACHEFILE
fi

cat >>$CACHEFILE<<__EOF__
#
# RageIRCd v2.0 (bluemoon): Compile-Time Server Configuration Cache File
# ----------------------------------------------------------------------
#
# This file has been automatically generated using the RageIRCd v2 server
# configuration interface, and will be rewritten each time the interface
# is run.
#

PREFIX="$PREFIX"
ENGINE_CALL="$ENGINE_CALL"
ENGINE_CALL_NAME="$ENGINE_CALL_NAME"
FDSETSIZE="$FDSETSIZE"
SENDQLENGTH="$SENDQLENGTH"
BUFFERPOOL="$BUFFERPOOL"
MAXBUFFER="$MAXBUFFER"
ENABLE_DYNAMICMODULES="$ENABLE_DYNAMICMODULES"
ENABLE_OPENSSL="$ENABLE_OPENSSL"
OPENSSL_PATH="$OPENSSL_PATH"
ENABLE_BLALLOC="$ENABLE_BLALLOC"
ENABLE_MMAP="$ENABLE_MMAP"
ENABLE_THROTTLE="$ENABLE_THROTTLE"
EXTRA="$EXTRA"

CONFIGURE="./configure --prefix=\$PREFIX --with-engine=\$ENGINE_CALL_NAME --with-fd-setsize=\$FDSETSIZE"
CONFIGURE="\$CONFIGURE --with-sendq-length=\$SENDQLENGTH --with-maxbuffer=\$MAXBUFFER"
if test "x\$ENABLE_DYNAMICMODULES" = "x1"; then
	CONFIGURE="\$CONFIGURE --enable-dynamic-modules"
else
	CONFIGURE="\$CONFIGURE --disable-dynamic-modules"
fi
if test "x\$ENABLE_OPENSSL" = "x1"; then
	if test "x\$OPENSSL_PATH" = "x"; then
		CONFIGURE="\$CONFIGURE --enable-openssl"
	else
		CONFIGURE="\$CONFIGURE --enable-openssl=\$OPENSSL_PATH"
	fi
else
	CONFIGURE="\$CONFIGURE --disable-openssl"
fi
if test "x\$ENABLE_BLALLOC" = "x1"; then
	CONFIGURE="\$CONFIGURE --enable-blalloc"
else
	CONFIGURE="\$CONFIGURE --disable-blalloc"
fi
if test "x\$ENABLE_MMAP" = "x1"; then
	CONFIGURE="\$CONFIGURE --enable-mmap"
else
	CONFIGURE="\$CONFIGURE --disable-mmap"
fi
if test "x\$ENABLE_THROTTLE" = "x1"; then
	CONFIGURE="\$CONFIGURE --enable-throttle"
else
	CONFIGURE="\$CONFIGURE --disable-throttle"
fi
if test "x\$EXTRA" != "xnone" -a "x\$EXTRA" != "x"; then
	CONFIGURE="\$CONFIGURE \$EXTRA"
fi

# End of File
__EOF__

if test -n "$ENABLE_OPENSSL" -a -n "$MAKE_SELF_SIGNED"; then
	CONFIGURE="$CONFIGURE --with-self-signed"

	if test -n "$ENC_PRIV_KEY"; then
		CONFIGURE="$CONFIGURE --with-encrypted-key"
	fi
fi

echo " "
echo "$CONFIGURE"
echo " "

$CONFIGURE
