#!/bin/sh

##############################
##  Configurer for BitlBee  ##
##                          ##
##  Copyright 2004 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
flood=0
ssl=auto

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

--flood=0/1	Flood protection			$flood

--ssl=...	SSL library to use (gnutls, nss, openssl, bogus, auto)
							$ssl
EOF
		exit;
	fi
	eval "$e"
	shift;
done

# Expand $prefix and get rid of double slashes
bindir=`eval echo "$bindir/" | sed 's/\/\{1,\}/\//g'`
etcdir=`eval echo "$etcdir/" | sed 's/\/\{1,\}/\//g'`
mandir=`eval echo "$mandir/" | sed 's/\/\{1,\}/\//g'`
datadir=`eval echo "$datadir/" | sed 's/\/\{1,\}/\//g'`
config=`eval echo "$config/" | sed 's/\/\{1,\}/\//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
   
   Do *NOT* use any of these defines in your code without thinking twice, most
   of them can/will be overridden at run-time */

#define CONFIG "$config"
#define ETCDIR "$etcdir"
#define VARDIR "$datadir"
#define ARCH "$arch"
#define CPU "$cpu"
#define IPV6
EOF

if [ "$debug" = "1" ]; then
	echo 'CFLAGS=-g' >> Makefile.settings
	echo 'DEBUG=1' >> Makefile.settings
	echo '#define DEBUG' >> config.h
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 [ -n "$LD" ]; then
	echo "LD=$LD" >> Makefile.settings;
elif 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
	echo '#define GLIB2' >> config.h
elif type glib-config > /dev/null 2> /dev/null; then
	cat<<EOF>>Makefile.settings
EFLAGS+=`glib-config --libs`
CFLAGS+=`glib-config --cflags`
EOF
	echo '#define GLIB1' >> config.h
else
	echo 'Cannot find glib development libraries, aborting. (Install libglib-dev?)'
	exit 1;
fi

if [ -e /usr/include/iconv.h ]; then
	:;
elif [ -e /usr/local/include/iconv.h ]; then
	echo CFLAGS+=-I/usr/local/include >> Makefile.settings;
else
	echo
	echo 'Warning: Could not find iconv.h, you might have to install it and/or modify'
	echo 'Makefile.settings to tell where this file is.';
fi


detect_gnutls()
{
	if libgnutls-config --version > /dev/null 2> /dev/null; then
		cat <<EOF>>Makefile.settings
EFLAGS+=`libgnutls-config --libs`
CFLAGS+=`libgnutls-config --cflags`
EOF
		
		ssl=gnutls
		ret=1;
	else
		ret=0;
	fi;
}

detect_nss()
{
	if type pkg-config > /dev/null 2>/dev/null && pkg-config mozilla-nss; then
		cat<<EOF>>Makefile.settings
EFLAGS+=`pkg-config --libs mozilla-nss`
CFLAGS+=`pkg-config --cflags mozilla-nss`
EOF
		
		ssl=nss
		ret=1;
	else
		ret=0;
	fi;
}

if [ "$msn" = 1 -o "$jabber" = 1 ]; then
	if [ "$ssl" = "auto" ]; then
		detect_gnutls
		if [ "$ret" = "0" ]; then
			detect_nss
		fi;
	elif [ "$ssl" = "gnutls" ]; then
		detect_gnutls;
	elif [ "$ssl" = "nss" ]; then
		detect_nss;
	elif [ "$ssl" = "openssl" ]; then
		echo
		echo 'No detection code exists for OpenSSL. Make sure that you have a complete'
		echo 'install of OpenSSL (including devel/header files) before reporting'
		echo 'compilation problems.'
		echo
		echo 'Also, keep in mind that the OpenSSL is, according to some people, not'
		echo 'completely GPL-compatible. Using GnuTLS or NSS is recommended and better'
		echo 'supported by us. However, on many BSD machines, OpenSSL can be considered'
		echo 'part of the operating system, which makes it GPL-compatible.'
		echo
		echo 'For more info, see: http://www.openssl.org/support/faq.html#LEGAL2'
		echo '                    http://www.gnome.org/~markmc/openssl-and-the-gpl.html'
		echo
		echo 'Please note that distributing a BitlBee binary which links to OpenSSL is'
		echo 'probably illegal. If you want to create and distribute a binary BitlBee'
		echo 'package, you really should use GnuTLS or NSS instead.'
		echo
		echo 'Also, the OpenSSL license requires us to say this:'
		echo ' *    "This product includes software developed by the OpenSSL Project'
		echo ' *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"'
		
		echo 'EFLAGS+=-lssl -lcrypto' >> Makefile.settings
		
		ret=1;
	elif [ "$ssl" = "bogus" ]; then
		echo
		echo 'Using bogus SSL code. This will not make the MSN module work, but it will'
		echo 'allow you to use the Jabber module - although without working SSL support.'
		
		ret=1;
	else
		echo
		echo 'ERROR: Unknown SSL library specified.'
		exit 1;
	fi
	
	if [ "$ret" = "0" ]; then
		echo
		echo 'WARNING: Could not find a suitable SSL library (GnuTLS, libnss or OpenSSL).'
		echo '         This is necessary for MSN and full Jabber support. To continue,'
		echo '         install a suitable SSL library or disable MSN support (--msn=0).'
		echo '         If you want Jabber without SSL support you can try --ssl=bogus.'
		
		exit 1;
	fi;
	
	echo 'SSL_CLIENT=ssl_'$ssl'.o' >> Makefile.settings
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 [ -n "$STRIP" ]; then
		echo "STRIP=$STRIP" >> Makefile.settings;
	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 [ "$flood" = 1 ]; then
	echo '#define FLOOD_SEND' >> config.h
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'msnn.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 without problems on most BSD systems.'
	echo 'EFLAGS+=-liconv' >> Makefile.settings;
;;
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
	echo 'EFLAGS+=-liconv' >> Makefile.settings;
;;
Darwin )
	echo 'Darwin/Mac OS X. This should work well, please read the docs for more information.'
	echo 'EFLAGS+=-liconv' >> Makefile.settings;
;;
IRIX )
	echo 'IRIX should not be a problem. There were some positive testing reports.'
;;
CYGWIN* )
	echo 'Cygwin is tested and functioning, but not officially supported.'
;;
* )
	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 '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 [ "$msn" = "1" ]; then
	echo '  Using SSL library: '$ssl;
fi

if [ "$flood" = "0" ]; then
	echo '  Flood protection disabled.';
else
	echo '  Flood protection 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
