#!/bin/sh

set -e

set -- `getopt dam $*`
for i 
do
	case "$i"
	in
		-d)	set -x; shift
			;;
		-a)	AUTO=1; shift
			;;
		-m)	CACHEOPT="--cache-file=`pwd`/.config.cache"
	esac
done

if test -z "$AUTO"
then
	make -C scripts/lxdialog all
	scripts/Menuconfig scripts/config.in
fi
source .config


#
# Program selection 
#
SUBDIRS=
	
test -n "$CONFIG_DOC" && SUBDIRS="$SUBDIRS doc"
test -n "$CONFIG_FAQ"  && SUBDIRS="$SUBDIRS FAQ"
test -n "$CONFIG_ISDNCTRL"  && SUBDIRS="$SUBDIRS isdnctrl"
test -n "$CONFIG_IPROFD"  && SUBDIRS="$SUBDIRS iprofd"

test -n "$CONFIG_TELES"  && SUBDIRS="$SUBDIRS teles"
test -n "$CONFIG_ICN"  && SUBDIRS="$SUBDIRS icn"
test -n "$CONFIG_PCBIT"  && SUBDIRS="$SUBDIRS pcbit"
test -n "$CONFIG_AVMB1"  && SUBDIRS="$SUBDIRS avmb1"
test -n "$CONFIG_LOOP"  && SUBDIRS="$SUBDIRS loop"

test -n "$CONFIG_IMON"  && SUBDIRS="$SUBDIRS imon"
test -n "$CONFIG_IMONTTY"  && SUBDIRS="$SUBDIRS imontty"

test -n "$CONFIG_XISDNLOAD"  && SUBDIRS="$SUBDIRS xisdnload"
test -n "$CONFIG_XMONISDN"  && SUBDIRS="$SUBDIRS xmonisdn"

test -n "$CONFIG_IPPPD"  && SUBDIRS="$SUBDIRS ipppd"
test -n "$CONFIG_IPPPSTATS"  && SUBDIRS="$SUBDIRS ipppstats"
test -n "$CONFIG_ISDNLOG"  && SUBDIRS="$SUBDIRS lib isdnlog "
test -n "$CONFIG_VBOX"  && SUBDIRS="$SUBDIRS vbox"
test -n "$CONFIG_LIB_AREACODE" && SUBDIRS="areacode $SUBDIRS"

#
# Compile Options
#
CONFIGOPT=$CONFIGOPT

test -n "$CONFIG_DEBUG" && \
	CONFIGOPT="$CONFIGOPT --enable-debug"

test -n "$CONFIG_KERNEL" && \
	CONFIGOPT="$CONFIGOPT --with-kernel=$CONFIG_KERNEL"
test -n "$CONFIG_LOOPDIR" && \
	CONFIGOPT="$CONFIGOPT --with-loopdir=$CONFIG_LOOPDIR"

test -n "$CONFIG_UPCMD" && \
	CONFIGOPT="$CONFIGOPT --with-upcmd=$CONFIG_UPCMD"
test -n "$CONFIG_DOWNCMD" && \
	CONFIGOPT="$CONFIGOPT --with-downcmd=$CONFIG_DOWNCMD"

test -n "$CONFIG_MSCHAP" && \
	CONFIGOPT="$CONFIGOPT --with-mschap"

test -n "$CONFIG_TCLLIB" && \
	CONFIGOPT="$CONFIGOPT --with-tcl-libdir=$CONFIG_TCLLIB"
test -n "$CONFIG_TCLINC" && \
	CONFIGOPT="$CONFIGOPT --with-tcl-includedir=$CONFIG_TCLINC"
test -n "$CONFIG_NCLIB" && \
	CONFIGOPT="$CONFIGOPT --with-ncurses-libdir=$CONFIG_NCLIB"
test -n "$CONFIG_NCINC" && \
	CONFIGOPT="$CONFIGOPT --with-ncurses-includedir=$CONFIG_NCINC"

test -n "$CONFIG_POSTGRES" && \
	CONFIGOPT="$CONFIGOPT --enable-postgres"
test -n "$CONFIG_POSTGRESDIR" && \
	CONFIGOPT="$CONFIGOPT --with-postgres=$CONFIG_POSTGRESDIR"
test -n "$CONFIG_CHARGE_DE" && \
	CONFIGOPT="$CONFIGOPT --with-charge=DE"
test -n "$CONFIG_CHARGE_NL" && \
	CONFIGOPT="$CONFIGOPT --with-charge=NL"
test -n "$CONFIG_CHARGE_CH" && \
	CONFIGOPT="$CONFIGOPT --with-charge=CH"
test -n "$CONFIG_LIB_AREACODE" &&
	CONFIGOPT="$CONFIGOPT --with-alib=areacode"
test -n "$CONFIG_LIB_AVON" &&
	CONFIGOPT="$CONFIGOPT --with-alib=avon"

#
# Directory Layout
#
test -n "$CONFIG_I4LCONFDIR" && \
	CONFIGOPT="$CONFIGOPT --with-i4lconfdir=$CONFIG_I4LCONFDIR"
test -n "$CONFIG_PREFIX" && \
	CONFIGOPT="$CONFIGOPT --prefix=$CONFIG_PREFIX"
test -n "$CONFIG_BINDIR" && \
	CONFIGOPT="$CONFIGOPT --bindir=$CONFIG_BINDIR"
test -n "$CONFIG_SBINDIR" && \
	CONFIGOPT="$CONFIGOPT --sbindir=$CONFIG_SBINDIR"
test -n "$CONFIG_MANDIR" && \
	CONFIGOPT="$CONFIGOPT --mandir=$CONFIG_MANDIR"
test -n "$CONFIG_FAQDIR" && \
	CONFIGOPT="$CONFIGOPT --with-faqdir=$CONFIG_FAQDIR"
test -n "$CONFIG_ALIBDIR" && \
	CONFIGOPT="$CONFIGOPT --with-alibdir=$CONFIG_ALIBDIR"

#
# Create directory to include linux sources
# (you can't use -I/usr/src/linux : linux has a different net/ than # libc)
test -f include || rm -rf include
mkdir include
ln -s $CONFIG_KERNEL/include/linux include/linux
ln -s $CONFIG_KERNEL/include/asm include/asm
CONFIGOPT="$CONFIGOPT --with-include=`pwd`/include"

#
# Create the Makefile
#
sed -e "s#@SUBDIRS@#$SUBDIRS#" -e "s#@CONFIGOPT@#$CONFIGOPT#" \
	< Makefile.in > Makefile

#
# Run Configure in all subdirs
#
for A in $SUBDIRS; do
	pushd $A; ./configure $CONFIGOPT --srcdir=`pwd`; popd
done

#
# Done
#
cat << EOF
	Everything is configured. Now you can run "make" to compile
	and then "make install" (as root) to install isdn4k-utils.
EOF
