#!/bin/sh
#
#  Auto-configurator for IRCE - Copyright (C) 1998 Ari Heikkinen
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 1, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
check_compiler()
{
	RET=1
	[ "$1" = "" -o "$2" = "" ] && return $RET
	printf "%s" "Checking for $1 ..."
	src=setup-src$$.c
	out=setup-out$$
	rm -f $src $out
	echo "int main(void) { return 0; }" > $src
	stdout=`$2 $3 $src -o $out $4 2>&1`
	if [ -z "$stdout" -a -x "$out" ]; then
		echo ' yes'
		RET=0
	else
		echo ' no'
		RET=1
	fi
	rm -f $src $out
	return $RET
}

check_include()
{
	RET=1
	[ "$1" = "" -o "$2" = "" ] && return $RET
	printf "%s" "Checking for $1 ..."
	src=setup-src$$.c
	out=setup-out$$
	rm -f $src $out
	if [ "$3" = "" ]; then
		echo "#include <$1>" > $src
	else
		echo "#include <$3>" > $src
		echo "#include <$1>" >> $src
	fi
	echo "int main(void) { return 0; }" >> $src
	$CC $CFLAGS $src -o $out $LDFLAGS > /dev/null 2> /dev/null
	if [ -x "$out" ]; then
		echo ' yes'
		echo "#define	$2" >> $SETUP
		RET=0
	else
		echo ' no'
		echo "#undef	$2" >> $SETUP
		RET=1
	fi
	rm -f $src $out
	return $RET
}

check_function()
{
	RET=1
	[ "$1" = "" -o "$2" = "" ] && return $RET
	printf "%s" "Checking for $1 ..."
	src=setup-src$$.c
	out=setup-out$$
	rm -f $src $out
	echo "void $1(void);" > $src
	echo "int main(void) { $1(); return 0; }" >> $src
	$CC $CFLAGS $src -o $out $LDFLAGS > /dev/null 2> /dev/null
	if [ -x "$out" ]; then
		echo ' yes'
		echo "#define	$2" >> $SETUP
		RET=0
	else
		echo ' no'
		echo "#undef	$2" >> $SETUP
		RET=1
	fi
	rm -f $src $out
	return $RET
}

cat << EOF
*********************************************************
* Auto-configurator for IRCE - Written by Ari Heikkinen *
*********************************************************
This may take several minutes, be patient...
EOF
umask 22 || exit 1
if [ ! -x "./config-irce" ]; then
	echo "Sorry, must be in the same directory than config-irce script!"
	exit 1
fi
if [ "`echo 'foo bar' | awk '{ print $2 }'`" != "bar" ]; then
	echo 'Sorry, you need a working awk in your path!'
	exit 1
fi
if [ "`echo 'foo foo' | sed 's/foo/bar/g'`" != "bar bar" ]; then
	echo 'Sorry, you need a working sed in your path!'
	exit 1
fi
NAM=`uname -n`
SYS=`uname -s`
REV=`uname -r`
if [ -z "$NAM" -o -z "$SYS" -o -z "$REV" ]; then
	echo 'Sorry, you need a working uname command in your path!'
	exit 1
fi
USER=`whoami`
if [ -z "$USER" ]; then
	echo 'Sorry, unable to determine your username!'
	exit 1
fi
HOST=`nslookup $NAM 2> /dev/null | awk '/Name:/ { print $2 ; exit }'`
if [ -z "$HOST" ]; then
	HOST=`awk '/domain/ { print $2 ; exit }' /etc/resolv.conf`
	if [ -z "$HOST" ]; then
		HOST=`domainname`
		if [ -z "$HOST" ]; then
			echo 'Unable to determine your domain name!'
			exit 1
		fi
	fi
	HOST="$NAM"."$HOST"
fi
MAJREV=`echo "$REV" | cut -d. -f1`
if [ -z "$MAJREV" ]; then
	echo 'Sorry, you need a working cut command!'
	exit 1
fi
OBJDIR=obj-$SYS-$REV
SETUP=setup.h
CC=""
CFLAGS=""
LDFLAGS=""

mkdir $OBJDIR || exit 1
cd $OBJDIR || exit 1

if check_compiler "compiler gcc" gcc "" ""; then
	CC=gcc
elif check_compiler "compiler cc" cc "" ""; then
	CC=cc
elif check_compiler "compiler cc (HPUX)" cc "-Ae" ""; then
	CC=cc
	CFLAGS="-Ae -DHPUX_BROKEN_CC"
	export CCOPTS=-Ae
else
	echo "You need a working C compiler in your path!!"
	echo "First one found either gcc or cc will be picked."
	exit 1
fi

printf "%s" "Checking for Solaris 2.x ..."
if [ "$SYS" = "SunOS" ]; then
	if [ "$MAJREV" -ge "5" ]; then
		CFLAGS="-DSOL20"
		echo ' yes'
	else
		echo ' no'
	fi
else
	echo ' no'
fi

printf "%s" "Checking for AIX ..."
if [ "$SYS" = "AIX" ]; then
	CFLAGS="-DAIX"
	echo ' yes'
else
	echo ' no'
fi

if check_compiler "compiler flag -O2" $CC "-O2" ""; then
	CFLAGS="$CFLAGS -O2"
elif check_compiler "compiler flag -O" $CC "-O" ""; then
	CFLAGS="$CFLAGS -O"
elif check_compiler "compiler flag +O3" $CC "+O3" ""; then
	CFLAGS="$CFLAGS +O3"
else
	echo "No working optimization flags found..."
fi

for f in -Xo -cckr -bsd
do
	if check_compiler "compiler flag(s) $f" $CC "$f" ""; then
		CFLAGS="$CFLAGS $f"
	fi
done

for f in -lsocket -lnsl -lm -lsys_s -lbsd -linet -lseq -lns -lucb
do
	if check_compiler "linker flag $f" $CC "" "$f"; then
		LDFLAGS="$LDFLAGS $f"
	fi
done

cat > $SETUP << !EOF!
/*
** setup.h - automatically generated by configure
*/
#ifndef	__setup_include__
#define	__setup_include__

!EOF!

check_include stddef.h HAVE_STDDEFH
check_include string.h HAVE_STRINGH
check_include strings.h HAVE_STRINGSH
check_include errno.h HAVE_ERRNOH
check_include unistd.h HAVE_UNISTDH
check_include time.h HAVE_TIMEH
check_include fcntl.h HAVE_FCNTLH
check_include math.h HAVE_MATHH
check_include stropts.h HAVE_STROPTSH
check_include poll.h HAVE_POLLH
if check_include sys/time.h HAVE_SYS_TIMEH ; then
	check_include sys/resource.h HAVE_SYS_RESOURCEH sys/time.h
else
	check_include sys/resource.h HAVE_SYS_RESOURCEH
fi
check_include sys/param.h HAVE_SYS_PARAMH
check_include sys/syslog.h HAVE_SYS_SYSLOGH
check_include sys/rusage.h HAVE_SYS_RUSAGEH
check_include sys/times.h HAVE_SYS_TIMESH
check_include sys/stat.h HAVE_SYS_STATH
check_include sys/file.h HAVE_SYS_FILEH
check_include sys/filio.h HAVE_SYS_FILIOH
check_include sys/un.h HAVE_SYS_UNH
check_include sys/bitypes.h HAVE_SYS_BITYPESH
check_function poll HAVE_POLL
check_function bzero HAVE_BZERO
check_function bcopy HAVE_BCOPY
check_function bcmp HAVE_BCMP
check_function setsid HAVE_SETSID
check_function index HAVE_INDEX
check_function strerror HAVE_STRERROR
check_function strtoken HAVE_STRTOKEN
check_function strtok HAVE_STRTOK
check_function inet_addr HAVE_INET_ADDR
check_function inet_ntoa HAVE_INET_NTOA
check_function inet_aton HAVE_INET_ATON
check_function inet_netof HAVE_INET_NETOF
check_function lrand48 HAVE_LRAND48
check_function getrusage HAVE_GETRUSAGE
check_function times HAVE_TIMES
if check_function sigaction POSIX_SIGNALS ; then
	:
elif check_function signal BSD_RELIABLE_SIGNALS ; then
	:
else
	echo '#define	SYSV_UNRELIABLE_SIGNALS' >> $SETUP
fi

printf "%s" "Generating binary types..."
rm -f bitypes$$
$CC $CFLAGS ../setup/bitypes.c -o bitypes$$ > /dev/null 2> /dev/null
if [ -x ./bitypes$$ ]; then
	echo >> $SETUP
	./bitypes$$ >> $SETUP
	rm -f bitypes$$
	echo ' ok'
else
	echo ' error, unable to compile bitypes.c'
	exit 1
fi

printf "%s" "Determining the best dbuf block size..."
rm -f memory$$
$CC $CFLAGS ../setup/memory.c -o memory$$ > /dev/null 2> /dev/null
if [ -x ./memory$$ ]; then
	echo >> $SETUP
	./memory$$ >> $SETUP
	rm -f memory$$
	echo ' ok'
else
	echo ' error, unable to compile memory.c'
	exit 1
fi

cat >> $SETUP << !EOF!

#endif	/* __setup_include__ */
!EOF!

ss='s/\([\\/\ ]\)/\\\1/g'
sed "s/@DEST@/"`echo "$HOME" | sed "$ss"`"\\/ircd/" < ../setup/Makefile.in \
	| sed "s/@FLAGS@/$CFLAGS/" \
	| sed "s/@LIBS@/$LDFLAGS/" \
	| sed "s/@COMPILER@/$CC/" > Makefile || exit 1
cp ../setup/config.h.in config.h || exit 1

sed "s/@HOSTNAME@/"`echo $HOST | sed "$ss"`/g < ../setup/ircd.conf.in \
	| sed "s/@USERNAME@/"`echo $USER | sed "$ss"`/g > ircd.conf

echo "Now cd to $OBJDIR, then edit Makefile and config.h"
echo "Finally run make to build irce. Good luck!"
exit 0
