#!/bin/sh

##############################
##  Configurer for BitlBee  ##
##                          ##
##  Copyright 2002 Lintux   ##
##  Copyright 2002 Lucumo   ##
##############################

prefix='/usr/local/'
bindir='$prefix/sbin/'
etcdir='$prefix/etc/'
mandir='$prefix/share/man/'
datadir='$prefix/share/bitlbee/'
config='/var/lib/bitlbee/'

debug=0
strip=1
tcpd=0

arch=`uname -s`
cpu=`uname -m`

while [ -n "$1" ]; do
	e="`expr "$1" : '--\(.*=.*\)'`"
	if [ -z "$e" ]; then
		cat<<EOF
BitlBee configure

Usage: $0 [OPTIONS]

Option		Description				Default

--prefix=...	Directories to put files in		$prefix
--bindir=...						$bindir
--etcdir=...						$etcdir
--mandir=...						$mandir
--datadir=...						$datadir
--config=...						$config

--debug=0/1	Disable/enable debugging		$debug
--strip=0/1	Disable/enable binary stripping		$strip

--tcpd=0/1	/etc/hosts.{allow,deny} support		$tcpd
EOF
		exit;
	fi
	eval "$e"
	shift;
done

# Expand $prefix
bindir=`eval echo $bindir/ | sed 's/\/\+/\//g'`
etcdir=`eval echo $etcdir/ | sed 's/\/\+/\//g'`
mandir=`eval echo $mandir/ | sed 's/\/\+/\//g'`
datadir=`eval echo $datadir/ | sed 's/\/\+/\//g'`
config=`eval echo $config/ | sed 's/\/\+/\//g'`

cat<<EOF>Makefile.settings
## BitlBee settings, generated by configure
PREFIX=$prefix
BINDIR=$bindir
ETCDIR=$etcdir
MANDIR=$mandir
DATADIR=$datadir
CONFIG=$config

ARCH=$arch
CPU=$cpu
OUTFILE=bitlbee

DESTDIR=
LFLAGS=

EOF

cat<<EOF>config.h
/* BitlBee settings, generated by configure */
#define CONFIG "$config"
#define ETCDIR "$etcdir"
#define DATADIR "$datadir"
#define ARCH "$arch"
#define CPU "$cpu"
EOF

if [ "$debug" = "1" ]; then
	echo 'CFLAGS=-g' >> Makefile.settings
	echo 'DEBUG=1' >> Makefile.settings
else
	echo 'CFLAGS=-O3' >> Makefile.settings;
fi

echo CFLAGS+=-I`pwd` -I`pwd`/protocols -I. >> Makefile.settings

if type gcc > /dev/null 2> /dev/null; then
	echo "CC=gcc" >> Makefile.settings;
elif type cc > /dev/null 2> /dev/null; then
	echo "CC=cc" >> Makefile.settings;
else
	echo 'Cannot find a C compiler, aborting.'
	exit 1;
fi

if type ld > /dev/null 2> /dev/null; then
	echo "LD=ld" >> Makefile.settings;
else
	echo 'Cannot find ld, aborting.'
	exit 1;
fi

if type glib-config > /dev/null 2> /dev/null; then
	cat<<EOF>>Makefile.settings
LFLAGS+=`glib-config --libs`
CFLAGS+=`glib-config --cflags`
EOF
else
	echo 'Cannot find glib development libraries, aborting. (Install libglib-dev?)'
	exit 1;
fi

if [ -e /usr/include/tcpd.h -a "$tcpd" = "1" ]; then
	cat<<EOF>>Makefile.settings
LFLAGS+=-lwrap
EOF
elif [ -e /usr/local/include/tcpd.h -a "$tcpd" = "1" ]; then
	cat<<EOF>>Makefile.settings
LFLAGS+=-L/usr/local/lib
CFLAGS+=-I/usr/local/include
EOF
else
	if ! [ "$tcpd" = 0 ]; then
		tcpd=0
		echo 'Compiling without tcpd support, /etc/hosts.{allow,deny} will be ignored!'
		echo;
	fi
	echo '#define NO_TCPD' >> config.h
fi

if [ "$strip" = 0 ]; then
	echo "STRIP=\# skip strip" >> Makefile.settings;
else
	if [ "$debug" = 1 ]; then
		echo 'Stripping binaries does not make sense when debugging. Stripping disabled.'
		echo
		echo 'STRIP=\# skip strip' >> Makefile.settings
		strip=0;
	elif type strip > /dev/null 2> /dev/null; then
		echo "STRIP=strip" >> Makefile.settings;
	elif /bin/test -x /usr/ccs/bin/strip; then
		echo "STRIP=/usr/ccs/bin/strip" >> Makefile.settings;
	else
		echo 'No strip utility found, cannot remove unnecessary parts from executable.'
		echo ''
		echo 'STRIP=\# skip strip' >> Makefile.settings
		strip=0;
	fi;
fi

if [ "$strip" = 1 ]; then
	echo 'The strip option is enabled. This should not be a problem usually, but on some'
	echo 'systems it breaks stuff.'
	echo
fi

if [ -n "$BITLBEE_VERSION" ]; then
	echo 'Spoofing version number: '$BITLBEE_VERSION
	echo
	echo '#undef BITLBEE_VERSION' >> config.h
	echo '#define BITLBEE_VERSION '$BITLBEE_VERSION >> config.h;
fi

## Yes, I know these are a bit bogus. If necessary, flags can be added
## for specific architectures. That's what these lines are for...

## For Solaris we'll need some extra thingies here, for now we don't
## know what so it's not supported...

echo Architecture: $arch
case "$arch" in
Linux )
	echo 'Linux is our primary development platform, BitlBee should work well.'
;;
*BSD )
	echo 'Although our primary development system is Linux, BitlBee should run quite well on most BSD systems.'
;;
SunOS )
	echo 'Solaris does not yet work. This will be fixed in future versions.'
;;
* )
	echo 'We haven'\''t tested BitlBee on many platforms yet, yours is untested. YMMV.'
;;
esac
echo

echo Machine: $cpu
case "$cpu" in
i[0-9]86 )
	echo 'x86 is our primary development platform, BitlBee should work well.'
;;
sparc )
	echo 'SPARC is our secondary development platform. BitlBee usually works quite well.'
;;
* )
	echo 'This platform is untested. Please report any problems to <wilmer@gaast.net>.'
;;
esac
echo

echo 'Configuration done:'
if [ "$debug" = "1" ]; then
	echo '  Debugging enabled.';
else
	echo '  Debugging disabled.';
fi
if [ "$strip" = "1" ]; then
	echo '  Binary stripping enabled.';
else
	echo '  Binary stripping disabled.';
fi
if [ "$tcpd" = "0" ]; then
	echo '  Tcpd support disabled.';
else
	echo '  Tcpd support enabled.';
fi
