#!/bin/sh

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

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

msn=1
jabber=1
oscar=1
yahoo=1

debug=0
strip=1
tcpd=0
iconv=1

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

echo BitlBee configure

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

Usage: $0 [OPTIONS]

Option		Description				Default

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

--msn=0/1	Disable/enable MSN part			$msn
--jabber=0/1	Disable/enable Jabber part		$jabber
--oscar=0/1	Disable/enable Oscar part (ICQ, AIM)	$oscar
--yahoo=0/1	Disable/enable Yahoo part		$yahoo

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

--iconv=0/1	Character set conversion		$iconv
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=
EFLAGS=
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 [ -n "$CC" ]; then
	echo "CC=$CC" >> Makefile.settings;
elif 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 pkg-config > /dev/null 2>/dev/null && pkg-config glib-2.0; then
	cat<<EOF>>Makefile.settings
EFLAGS+=`pkg-config --libs glib-2.0`
CFLAGS+=`pkg-config --cflags glib-2.0`
EOF
elif type glib-config > /dev/null 2> /dev/null; then
	if [ "$msn" = 1 ]; then
		echo
		echo 'WARNING: GLIB <2.0 found, this is probably not going to work with the MSN'
		echo 'module. We strongly suggest you to upgrade glib.';
	fi
	cat<<EOF>>Makefile.settings
EFLAGS+=`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
EFLAGS+=-lwrap
EOF
elif [ -e /usr/local/include/tcpd.h -a "$tcpd" = "1" ]; then
	cat<<EOF>>Makefile.settings
EFLAGS+=-L/usr/local/lib -lwrap
CFLAGS+=-I/usr/local/include
EOF
else
	if ! [ "$tcpd" = 0 ]; then
		tcpd=0
		echo
		echo 'Compiling without tcpd support, /etc/hosts.{allow,deny} will be ignored!';
	fi
	echo '#define NO_TCPD' >> config.h
fi

echo '#define ICONV' >> config.h
if [ -e /usr/include/iconv.h -a "$iconv" = "1" ]; then
	:;
	## no-op
elif [ -e /usr/local/include/iconv.h -a "$iconv" = "1" ]; then
	cat<<EOF>>Makefile.settings
EFLAGS+=-L/usr/local/lib
CFLAGS+=-I/usr/local/include
EOF
else
	if ! [ "$iconv" = 0 ]; then
		iconv=0
		echo
		echo 'Compiling without iconv support, could not find include file.';
	fi
	echo '#undef ICONV' >> config.h
fi

if [ "$msn" = 1 ]; then
	if pkg-config --exists soup-2.0 2> /dev/null; then
		cat<<EOF>>Makefile.settings
EFLAGS+=`pkg-config soup-2.0 --libs`
CFLAGS+=`pkg-config soup-2.0 --cflags`
EOF
	else
		echo
		echo 'Could not find libsoup (v1.99 or higher), we need that for MSN. MSN disabled.'
		msn=0;
	fi;
fi

if [ "$strip" = 0 ]; then
	echo "STRIP=\# skip strip" >> Makefile.settings;
else
	if [ "$debug" = 1 ]; then
		echo
		echo 'Stripping binaries does not make sense when debugging. Stripping disabled.'
		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
		echo 'No strip utility found, cannot remove unnecessary parts from executable.'
		echo 'STRIP=\# skip strip' >> Makefile.settings
		strip=0;
	fi;
fi

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

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

protocols=''
protoobjs=''

if [ "$msn" = 0 ]; then
	echo '#undef WITH_MSN' >> config.h
else
	echo '#define WITH_MSN' >> config.h
	protocols=$protocols'msn '
	protoobjs=$protoobjs'msn.o '
fi

if [ "$jabber" = 0 ]; then
	echo '#undef WITH_JABBER' >> config.h
else
	echo '#define WITH_JABBER' >> config.h
	protocols=$protocols'jabber '
	protoobjs=$protoobjs'jabberr.o '
fi

if [ "$oscar" = 0 ]; then
	echo '#undef WITH_OSCAR' >> config.h
else
	echo '#define WITH_OSCAR' >> config.h
	protocols=$protocols'oscar '
	protoobjs=$protoobjs'oscarr.o '
fi

if [ "$yahoo" = 0 ]; then
	echo '#undef WITH_YAHOO' >> config.h
else
	echo '#define WITH_YAHOO' >> config.h
	protocols=$protocols'yahoo '
	protoobjs=$protoobjs'yahooo.o '
fi

if [ "$protocols" = "PROTOCOLS = " ]; then
	echo
	echo "WARNING: You haven't selected any communication protocol to compile!"
	echo "         Bitlbee will run, but you will be unable to connect to IM servers!"
fi

echo "PROTOCOLS = $protocols" >> Makefile.settings
echo "PROTOOBJS = $protoobjs" >> Makefile.settings

echo
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.'
	if [ "$iconv" = 1 ]; then
		echo 'EFLAGS+=-liconv' >> Makefile.settings;
	fi
;;
SunOS )
	echo 'Solaris does not work with stripping enabled, so we will disable that.'
	echo 'EFLAGS+=-lresolv -lnsl -lsocket' >> Makefile.settings
	echo 'STRIP=\# skip strip' >> Makefile.settings
	if [ "$iconv" = "1" ]; then
		echo 'EFLAGS+=-liconv' >> Makefile.settings;
	fi
;;
Darwin )
	echo 'Darwin/Mac OS X. This should work well, please read the docs for more information.'
;;
IRIX )
	echo 'IRIX should not be a problem. I got some positive testing reports.'
;;
CYGWIN* )
	echo 'Cygwin was tested and functioning. However, you'\''ll need glib. Please read the docs on how to obtain it.'
;;
* )
	echo 'We haven'\''t tested BitlBee on many platforms yet, yours is untested. YMMV. Please report any problems to <wilmer@gaast.net>.'
;;
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 'We got a SPARC around to test on from time to time. It usually works perfectly.'
;;
"Power Macintosh" )
	echo 'We test on an iBook regularly. Everything should work fine.'
;;
* )
	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.'
	echo '    -- Please do not report any bugs when using tcpd, we disabled this feature'
	echo '       long ago, for a very good reason: It usually does not work and does not'
	echo '       really make sense in inetd services anyway.';
fi

if [ "$iconv" = "0" ]; then
	echo '  iconv support disabled.';
else
	echo '  iconv support enabled.';
fi

if [ -n "$protocols" ]; then
	echo '  Building with these protocols:' $protocols;
else
	echo '  Building without IM-protocol support. We wish you a lot of fun...';
fi
