#! /bin/sh

# Initialize default variables
prefix="/usr/local"
incdir="/usr/include"
libdir="/usr/lib"
lmsbindir=""
lmslibdir=""
with_mysql=yes

# Create config.status file
echo "#!/bin/sh" > config.status.new
echo "" >> config.status.new
echo "./configure \\" >> config.status.new

# Parse options *****************************************************************
ac_prev=
for ac_option
do

  case "$ac_option" in
    	-*=*)
		ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'`
    	;;
    	*) 
		ac_optarg=
    	;;
  esac

  case "$ac_option" in
	
	-disable-* | --disable-*)
		ac_feature=`echo $ac_option|sed -e 's/-*disable-//'`
		# Reject names that are not valid shell variable names.
		if test -n "`echo $ac_feature| sed 's/[-a-zA-Z0-9_]//g'`"; then
			{ echo "configure: error: $ac_feature: invalid feature name" 1>&2; exit 1; }
		fi
		ac_feature=`echo $ac_feature| sed 's/-/_/g'`
		eval "enable_${ac_feature}=no"
	;;
	-enable-* | --enable-*)
		ac_feature=`echo $ac_option|sed -e 's/-*enable-//' -e 's/=.*//'`
		# Reject names that are not valid shell variable names.
		if test -n "`echo $ac_feature| sed 's/[-_a-zA-Z0-9]//g'`"; then
			{ echo "configure: error: $ac_feature: invalid feature name" 1>&2; exit 1; }
		fi
		ac_feature=`echo $ac_feature| sed 's/-/_/g'`
		case "$ac_option" in
			*=*) ;;
			*) ac_optarg=yes ;;
		esac
		eval "enable_${ac_feature}='$ac_optarg'"
	;;
	-prefix=* | --prefix=* )
		prefix="$ac_optarg"
	;;
	-lmsbindir=* | --lmsbindir=* )
		lmsbindir="$ac_optarg"
	;;
	-lmslibdir=* | --lmslibdir=* )
		lmslibdir="$ac_optarg"
	;;
	-with-* | --with-*)
		ac_package=`echo $ac_option|sed -e 's/-*with-//' -e 's/=.*//'`
		# Reject names that are not valid shell variable names.
		if test -n "`echo $ac_package| sed 's/[-_a-zA-Z0-9]//g'`"; then
			{ echo "configure: error: $ac_package: invalid package name" 1>&2; exit 1; }
		fi
		ac_package=`echo $ac_package| sed 's/-/_/g'`
		case "$ac_option" in
			*=*) ;;
			*) ac_optarg=yes ;;
		esac
		eval "with_${ac_package}='$ac_optarg'"
	;;
	-lib=* | --lib=* | -libdir=* | --libdir=* )
		libdir="$ac_optarg"
	;;
	-inc=* | --inc=* | -incdir=* | --incdir=* )
		incdir="$ac_optarg"
	;;
	-ini=* | -ini=* | -inifile=* | --inifile=* )
		inifile="$ac_optarg"
	;;
	-help | --help )
    		# Omit some internal or obsolete options to make the list less imposing.
    		# This message is too long to be a string in the A/UX 3.1 sh.
    		cat << EOF
Usage: ./configure [options]
Options: [defaults in brackets after descriptions]
  --help		print this message
  --enable-debug0	SQL query logging (disabled)
  --enable-debug1	events logging (disabled)
  --with-pgsql		enables use of PostgreSQL database (disabled)
  --with-mysql		enables use of MySQL database (enabled)
  --prefix=PREFIX	sets program and modules install location
			(/usr/local), modules goes to PREFIX/lms/lib and main
			program to PREFIX/lms/bin
  --lmsbindir=DIR	sets location of target LMS binaries (PREFIX/lms/bin)
  --lmslibdir=DIR	sets location of target LMS modules (PREFIX/lms/lib)
  --libdir=DIR		sets location of database library (/usr/lib)
  --incdir=DIR		sets location of database include files (/usr/include)
  --inifile=FILE        configuration file - disables online configuration
EOF
		rm config.status.new
    		exit 0
	;;
	*)
		{ echo "configure error: '$ac_option' invalid option; use --help to show usage" 1>&2; exit 1; }
    	;;
  esac
  
  echo "\"$ac_option\" \\" >> config.status.new
done

mv config.status.new config.status
chmod a+x config.status
#***************************************************************************************************

LIBS=
LDFLAGS=
CFLAGS=
FLAGS=
OS=
LMSDBIN="lmsd"

echo -n "OS Type... "
case `(uname -s || echo unknown) 2>/dev/null` in
  Linux | linux | GNU | GNU/*) 
	OS="linux"
	FLAGS="-DGNU"
	CFLAGS="-fPIC"
	echo "Linux";;
  CYGWIN* | Cygwin* | cygwin* )
	OS="cygwin"
	LIBS="-lz"
	LMSDBIN="lmsd.exe"
	EXCLUDE="pinger"
	echo "Cygwin";;

  *)
	CFLAGS="-fPIC"
	EXCLUDE="pinger"
	echo "Other";;
esac

# Set up database
echo -n "Database type..." 
if test "${with_pgsql+set}" = set
then
	LIBS="-lpq -ldl "$LIBS
	FLAGS="$FLAGS -DUSE_PGSQL"
	DBDRIVER=pgsql
	echo " PostgreSQL"
else
	LIBS="-lmysqlclient -ldl "$LIBS
	FLAGS="$FLAGS -DUSE_MYSQL"
	DBDRIVER=mysql
	echo " MySQL"
fi

# Set up lib/includes location
echo -n "Library location..."
LDFLAGS="-L$libdir"
echo " $libdir"

echo -n "Includes location..."
CFLAGS="-I$incdir -Wall -O2 "$CFLAGS
echo " $incdir"

# Set up debug level
echo -n "SQL queries debugging..."
if test "${enable_debug0+set}" = set
then
	FLAGS="$FLAGS -DDEBUG0"
	echo " yes"
else 
	echo " no"
fi

echo -n "Events debugging..."
if test "${enable_debug1+set}" = set
then
	FLAGS="$FLAGS -DDEBUG1"
	echo " yes"
else
	echo " no"
fi

echo -n "Configuration file..."
if test "${inifile+set}" = set
then
	FLAGS="$FLAGS -DCONFIGFILE=\\\"$inifile\\\""
	echo " $inifile"
else
	echo " no"
fi

# Set install directories
echo -n "Daemon install directory..."
start=`pwd`
cd $prefix
if [ $? -eq 1 ] # exit if directory not exists 
then 
    exit 0 
fi
prefix=`pwd`
cd $start
if [ ! -z "$lmsbindir" ]
then
    LMS_BIN_DIR=$lmsbindir
else
    LMS_BIN_DIR="$prefix/lms/bin"
fi
echo " $LMS_BIN_DIR"

echo -n "Modules install directory..."
start=`pwd`
cd $prefix
if [ $? -eq 1 ] # exit if directory not exists 
then 
    exit 0 
fi
prefix=`pwd`
cd $start
if [ ! -z "$lmslibdir" ]
then
    LMS_LIB_DIR=$lmslibdir
else
    LMS_LIB_DIR="$prefix/lms/lib"
fi
echo " $LMS_LIB_DIR"

# Create makefiles
echo -n "Create Makefile for main program..."
echo "
INSTALLDIR = $LMS_BIN_DIR
MODULESDIR = $LMS_LIB_DIR
LIBS = $LIBS
LDFLAGS = $LDFLAGS
CFLAGS = $CFLAGS $FLAGS -DLMS_LIB_DIR=\\\"\$(MODULESDIR)\\\"
DBDRIVER = $DBDRIVER
OUT = $LMSDBIN
OS = $OS
" > Makefile \
&& cat Makefile.in >> Makefile
echo " done"

# Make modules Makefile's
echo -n "MODULES = " > ./modules/Makefile

cd ./modules
for x in `ls | grep -v CVS`
do
    if [ -d ./${x} ]
    then
	if [ ! `echo $EXCLUDE | grep ${x}` ]
	then
	    echo -n "${x} " >> ./Makefile
	fi
    	if [ -e ./${x}/Makefile.in ]
	then
	    echo -n "Create Makefile for mod_${x}..."
	    echo "
MODULESDIR = $LMS_LIB_DIR
LIBS = $LIBS 
CFLAGS = $CFLAGS $FLAGS -I../..
LDFLAGS = $LDFLAGS
" > ./${x}/Makefile && cat ./${x}/Makefile.in >> ./${x}/Makefile
	    echo " done"
	fi
    fi
done 

cat ./Makefile.in >> ./Makefile

cd parser/extensions
if [ -e ./Makefile.in ]
then
    echo -n "Create Makefile for mod_parser extensions..."
    echo "
DBDRIVER = $DBDRIVER
CFLAGS = $CFLAGS $FLAGS -I../..
" > ./Makefile && cat ./Makefile.in >> ./Makefile
    echo " done"
fi

echo "***************************************************************"
echo "Configuration finished. Now You can type 'make && make install'"
