#####################################################################
#
#		BEEWEEB SERVER CONFIGURE FILE
#
#



#########################################################
#
#  We have to choose file for output e for pid
#  You can choose an absolute path, otherwise it must be
#  a file name (it will be created in BWBROOTDIR/log/)
#

	OUTPUTFILE=beeweeb.output
	PIDFILE=beeweeb.pid


#########################################################
#
#  You can set which modules to install (of course you
#  must have the module in the downloaded version).
#  If you don't want to install one you can set value 0.
#

	MOD_SYS=1
	MOD_VRT=1
	MOD_LOG=1
	MOD_ADV=1
	MOD_RDR=1
	MOD_DFT=1

	
	
#==========================================================#

	
# We have to check for some system information

MACHINE=`(uname -m) 2>/dev/null` || MACHINE="unknown"
RELEASE=`(uname -r) 2>/dev/null` || RELEASE="unknown"
SYSTEM=`(uname -s) 2>/dev/null`  || SYSTEM="unknown"
VERSION=`(uname -v) 2>/dev/null` || VERSION="unknown"


# prefix section

ARGV="$@"
error=

CUT=cut
RM=rm
CP=cp


if [ "x$ARGV" = "x" ] ; then
    echo " - using default installation directory /usr/local/beeweeb."
    prefix=/usr/local/beeweeb
else
    aux=`echo $ARGV | $CUT -d\=  -f1`
    origprefix=`echo $ARGV | $CUT -d\=  -f2`
    prefix=${origprefix%/}
    aux2=`echo $ARGV | $CUT -d\   -f2`
    if [ "x$prefix" = "x" ] || [ "$aux" != "--prefix" ] ; then
	error=YES
    else
        if  [ "$aux2"  != "--prefix=$origprefix" ] ; then
            error=YES
        else 
            echo " -using installation directory $prefix."
        fi
    fi
fi

if [ "$error" = "YES" ] ; then
    echo usage: configure [--prefix=BEEWEEB_ROOT_DIR]; \
    echo "       please, read README document."
    exit 1
fi


echo Configuring Beeweeb server installation for $SYSTEM system.


# Os definition

case $SYSTEM in
	Linux)
			OSTYPEDF=-DLINUX ;
		;;
	SunOS)
			OSTYPEDF=-DSOLARIS ;
		;;
	AIX)
			OSTYPEDF=-DAIX ;
		;;
esac

# Compiler definitions

	COMPILER=
	for compilers in "gcc" "cc"
	do
		i=1;
		path=`echo $PATH | cut -d: -f$i`
		while [ ok ] ;	do
			path=`echo $PATH | cut -d: -f$i`
			if [ "x$path" = "x" ] ; then
				break;
			fi
			i=$(($i+1))
	    	if [ -x $path/$compilers ] && \
	           [ ! -d $path/$compilers ]; then
	    		COMPILER="$compilers"
	   			break
			fi
		done
		if [ "x$COMPILER" != "x" ] ; then
			break
	    fi
	done

    if [ "x$COMPILER" = "x" ]; then
    	echo "Error: could not find any of these C compilers"
        echo "       anywhere in your PATH: $COMPILERS."
        exit 1
    fi
    CC=$COMPILER
	
	case $CC in
		gcc)
			CCFLAG="-c -g -Wall"
			COPT="-O3"
			SHARED=-shared
			PIC=-fPIC
			;;
		cc)
			case $SYSTEM in
				SunOS)
					CCFLAG="-c -xCC"
					COPT=-xO5
					SHARED=-G
					PIC=-KPIC
					;;
				AIX)
					CCFLAG="-c -qcpluscmt -qnogenpcomp -qnousepcomp "
					COPT="-O3 -qstrict"
					SHARED=-G
					PIC=
					;;
				esac
			;;
	esac
	echo "using $CC compiler"

#  Variables

echo "Setting variables..."

SHADOWFILE=/etc/shadow
BEEWEEBLIB=beeweeb.so
CFLAG=-o
CRYPTLIB=-lcrypt
SOCK_LIB_SUN="-lsocket -lnsl"
SOCK_LIB_LINUX=
SOCK_LIB_AIX=
CLIB6=libc.so.6
CLIB5=libc.so.5
TESTFILE=test.c
TESTX=test

##############################################
#
#	System info
#

# shadow section

echo "Searching for shadow file..."

if [ -f $SHADOWFILE ] ; then
   echo " -shadow file found."
   useshadow=-DSHADOW_MOD
else
   echo " -shadow file not found."
   useshadow=
fi

# 	creating test file

echo "Creating test file"

cat <<EOF > $TESTFILE
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("This is a test file\n");
    fflush(stdout);

    return 0;
}
EOF


# compiling to know libc version  for Linux

case $SYSTEM in
	Linux)
		echo "Compiling test file."

		$CC $CFLAG $TESTX $TESTFILE

		echo "Checking libc version."
		ldd test | grep $CLIB6 > aux
	
		ans=`cat aux`

		if [ "x$ans" = "x" ] ; then
			echo " -libc version $CLIB5"
    	    bwblib=$BEEWEEBLIB.5.linux
  		else
			echo " -libc version $CLIB6"
        	bwblib=$BEEWEEBLIB.6.linux
  		fi

		sock_lib=$SOCK_LIB_LINUX
		rm aux
		rm $TESTX
		;;
	SunOS)
		bwblib=$BEEWEEBLIB.solaris7sparc	
		sock_lib=$SOCK_LIB_SUN
		;;
	AIX)
		bwblib=$BEEWEEBLIB.aix	
		sock_lib=$SOCK_LIB_AIX
		;;
esac


# compiling to know if libcrypt is used

  echo "Compiling test file."
  echo "Checking how compiler works."

  $CC $CFLAG $TESTX $TESTFILE $CRYPTLIB > aux 2>&1

  ans=`cat aux`

  UseMd5=
  if [ "x$ans" = "x" ] ; then
		echo " -libcrypt used."
        crypt=$CRYPTLIB
        usecrypt=-DUSE_LIB_CRYPT
        if [ $SYSTEM = Linux ] ; then
    		UseMd5=-DUSE_MD5	# for Linux, but we have to develop a test file !!!!!!!!!!!!!!!!!!!!!!!!!!
	fi
  else
	echo " -libcrypt not used."
        crypt=
        usecrypt=
  fi

  $RM aux
  $RM $TESTX
  $RM $TESTFILE


##########################################
#
#	INCLUDE FILE SECTION
#

# Creating include file for unix utils

#UNIXUTILDIR=src/modules/mod_virtual_users
#echo "Editing include file for utility executables"
#$CP $UNIXUTILDIR/aux_include.tmpl $UNIXUTILDIR/aux_include.h
#echo "#define           BWB_ROOT_DIR    \"$prefix\"" >> $UNIXUTILDIR/aux_include.h     


# Creating include file for main program

BWBMAINDIR=src/bwbmain

echo "Creating include file for main program"

$CP $BWBMAINDIR/main.h.tmpl $BWBMAINDIR/main.h
echo "#define	BWB_SERVER_ROOT		\"$prefix/\"" >> $BWBMAINDIR/main.h

echo >> $BWBMAINDIR/main.h
echo "/* when the server runs like daemon stdout and stderr are wrote in this file*/" >> $BWBMAINDIR/main.h

field1=`echo $OUTPUTFILE | $CUT -d/ -f1`
if [ "x" = "x$field1" ]; then
	echo "#define BWB_OUTPUT_FILE		\"$OUTPUTFILE\"" >> $BWBMAINDIR/main.h
	OUTPUTFILE=$OUTPUTFILE
else
	echo "#define BWB_OUTPUT_FILE		\"$prefix/log/$OUTPUTFILE\"" >> $BWBMAINDIR/main.h
	OUTPUTFILE=$prefix/log/$OUTPUTFILE
fi

echo >> $BWBMAINDIR/main.h

echo "/* this is an auxiliar file used by beeweebdstart and beeweebdstop to manage the server */" >> $BWBMAINDIR/main.h
field1=`echo $PIDFILE | $CUT -d/ -f1`
if [ "x" = "x$field1" ]; then
	echo "#define BWB_PID_FILE		\"$PIDFILE\"" >> $BWBMAINDIR/main.h
	PIDFILE=$PIDFILE
else
	echo "#define BWB_PID_FILE		\"$prefix/log/$PIDFILE\"" >> $BWBMAINDIR/main.h
	PIDFILE=$prefix/log/$PIDFILE
fi

echo >> $BWBMAINDIR/main.h
echo "#endif" >> $BWBMAINDIR/main.h
echo >> $BWBMAINDIR/main.h


echo Output file used: $OUTPUTFILE	
echo Pid file used: $PIDFILE	


echo "Include file for main program created."


#####################################
#
#  searching modules to compile
#

echo Loading modules to compile
MODDIR=src/modules
daux=`ls $MODDIR`
i=0 
while [ ok ] ; do 
	i=$(($i+1))
	ax=`echo $daux | $CUT -d\  -f$i`
	if [ "x$ax" = x ]; then
		break 
	fi 
	if [ "$ax" != "Makefile" ] &&  [ "$ax" != "utils" ]  &&  [ "$ax" != "CVS" ] ; then
		if [ "$ax" != "mod_default" ] ; then
			dirs="$dirs $ax"
		else
			dirs="$dirs $ax/unix"
		fi
	fi
done
echo modules to install:  $dirs


# writing into file Makefile

echo "Creating Makefile."
echo "	Creating section variables..."



echo "SYSTEM			= $SYSTEM" > aux
echo "CC			= $CC" >> aux
echo "CFLAGS			= $CCFLAG " >> aux
echo "OPTIMOPT		= $COPT" >> aux
echo "SHAREDOPT		= $SHARED" >> aux
echo "PICOPT			= $PIC" >> aux
echo  >> aux
echo "prefix			= $prefix" >> aux
echo "OSTYPEDEF		= $OSTYPEDF">> aux
echo "LIBCRYPT		= $crypt" >> aux
echo "USELIBCRYPT		= $usecrypt" >> aux
echo "USEMD5 			= $UseMd5" >> aux
echo "BWBLIBVER 		= $bwblib" >> aux
echo "USESHADOW		= $useshadow" >> aux
echo "SOCK_LIB  		= $sock_lib" >> aux
echo "MODULES   		= $dirs" >> aux
echo "PID_FILE   		= $PIDFILE" >> aux
echo "OUTPUT_FILE  		= $OUTPUTFILE" >> aux
rm -f Makefile.include
cat aux Makefile.include.tmpl > Makefile.include
rm  aux

echo "Makefile created"



##################################################
#
#	Configuration file
#

echo "Creating Configuration file 'beeweeb.conf'."
echo "  -entering conf directory"
cd conf

LOGCONF=mod_log.conf.tmpl
LogName=mod_log
SYSCONF=mod_system_users.conf.tmpl
SysName=mod_system_users
VRTCONF=mod_virtual_users.conf.tmpl
VirtName=mod_virtual_users
RDRCONF=mod_redirect.conf.tmpl
RdrName=mod_redirect
ADVCONF=mod_advertising.conf.tmpl
AdvName=mod_advertising

ENDTAG="</module"
LOADMTAG=LoadModule

echo > beeweeb.aux
echo > log_aux
echo > sys_aux
echo > txt_aux
echo > adv_aux
echo > rdr_aux

for actual_module in $dirs; do
echo "        module $actual_module"
 case $actual_module in


 #========================
 #
 #  SystemUser module

 $SysName)
	if [ $MOD_SYS = "1" ] ; then
		
		echo "	creating system users module config file ... "
		echo  "  $LOADMTAG  $actual_module.so" >> beeweeb.aux 			
		echo >> beeweeb.aux 			
		origline=`grep prefix $SYSCONF`
		start=`grep -n prefix $SYSCONF | cut -d\: -f1 | head -1`
		end=`grep -n $ENDTAG $SYSCONF | cut -d\: -f1 | head -1`
		stop=$(($end-$start-1))
		head -$(($start-1)) $SYSCONF > aux.0
		tail -$stop $SYSCONF > aux.2
		modified_line=""
		i=0
		while [ ok ] ; do
	        i=$(($i+1))
	        ax=`echo $origline | $CUT -d\$  -f$i`
	        if [ "x$ax" = x ]; then
	                break
	        fi
	        if [ "$ax" = "prefix" ] ; then
	                modified_line="$modified_line$prefix"
	        else
	                modified_line="$modified_line$ax"
	        fi
		done
		echo $modified_line >> aux.1
		cat aux.0 aux.1 aux.2 > sys_aux
		$RM aux.0 aux.1 aux.2
		echo "	  ...done"
	fi
 ;;


 #========================
 #
 # 	VirtualUser module

 $VirtName)

	if [ $MOD_VRT = "1" ] ; then

		echo "	creating virtual users module config file ..."
		echo  "  $LOADMTAG  $actual_module.so" >> beeweeb.aux 			
		echo >> beeweeb.aux 			
		origline=`grep prefix $VRTCONF`
		start=`grep -n prefix $VRTCONF | cut -d\: -f1 | head -1`
		end=`grep -n $ENDTAG $VRTCONF | cut -d\: -f1 | head -1`
		stop=$(($end-$start-1))
		head -$(($start-1)) $VRTCONF > aux.0
		tail -$stop $VRTCONF > aux.2
		modified_line=""
		i=0
		while [ ok ] ; do
	        i=$(($i+1))
	        ax=`echo $origline | $CUT -d\$  -f$i`
	        if [ "x$ax" = x ]; then
	                break
	        fi
	        if [ "$ax" = "prefix" ] ; then
	                modified_line="$modified_line$prefix"
	        else
	                modified_line="$modified_line$ax"
	        fi
		done
		echo $modified_line >> aux.1
		cat aux.0 aux.1 aux.2 > txt_aux
		$RM  aux.0 aux.1 aux.2
		echo "	  ...done"
		
		#creating database file

		Passwdtestfile=virtual_passwd

		md5pwd=$1$pPWQQ9B8$viTdGkBEkIkDgA7eL46mK.
		oldpwd=dxmKKI/ldIab2

		echo "        creating database file for test"

		if [ "x$CRYPTLIB" = x ] ; then
			password=$md5pwd
		else
			password=$oldpwd
		fi
	
		$RM -f $Passwdtestfile
		echo "dan!"$password"!"$password"!"$prefix"/conf/testusers/!!!!" >> $Passwdtestfile
	fi
 ;;

 #========================
 #
 #  Logging module

 $LogName)
	if [ $MOD_LOG = "1" ] ; then
	
		echo "	creating logging module config file ... "
		echo  "  $LOADMTAG  $actual_module.so" >> beeweeb.aux 			
		echo >> beeweeb.aux 			
		origline=`grep "prefix" $LOGCONF`
		start=`grep -n prefix $LOGCONF | cut -d\: -f1 | head -1`
		end=`grep -n $ENDTAG $LOGCONF | cut -d\: -f1 | head -1`
		stop=$(($end-$start-1))
		head -$(($start-1)) $LOGCONF > aux.0
		tail -$stop $LOGCONF > aux.2
		modified_line=""
		i=0
		while [ ok ] ; do
    	    i=$(($i+1))
    	    ax=`echo $origline | $CUT -d\$  -f$i`
    	    if [ "x$ax" = x ]; then
    	            break
    	    fi
    	    if [ "$ax" = "prefix" ] ; then
    	            modified_line="$modified_line$prefix"
    	    else
    	            modified_line="$modified_line$ax"
		    fi
		done
		echo $modified_line >> aux.1
		cat aux.0 aux.1 aux.2 > log_aux
		$RM aux.0 aux.1 aux.2
		echo "	  ...done"
	fi

 ;;

 #========================
 #
 #  Advertising module

 $AdvName)
	if [ $MOD_ADV = "1" ] ; then
	
		echo "	creating advertising module config file ... "
		echo  "  $LOADMTAG  $actual_module.so" >> beeweeb.aux 			
		echo >> beeweeb.aux 			
		origline=`grep "prefix" $ADVCONF`
		start=`grep -n prefix $ADVCONF | cut -d\: -f1 | head -1`
		end=`grep -n $ENDTAG $ADVCONF | cut -d\: -f1 | head -1`
		stop=$(($end-$start-1))
		head -$(($start-1)) $ADVCONF > aux.0
		tail -$stop $ADVCONF > aux.2
		modified_line=""
		i=0
		while [ ok ] ; do
    	    i=$(($i+1))
    	    ax=`echo $origline | $CUT -d\$  -f$i`
    	    if [ "x$ax" = x ]; then
    	            break
    	    fi
    	    if [ "$ax" = "prefix" ] ; then
    	            modified_line="$modified_line$prefix"
    	    else
    	            modified_line="$modified_line$ax"
		    fi
		done
		echo $modified_line >> aux.1
		cat aux.0 aux.1 aux.2 > adv_aux
		$RM aux.0 aux.1 aux.2
		echo "	  ...done"
	fi

 ;;

 #========================
 #
 #  Redirect module

 $RdrName)
	if [ $MOD_RDR = "1" ] ; then
	
		echo "	creating redirect module config file ... "
		echo  "  $LOADMTAG  $actual_module.so" >> beeweeb.aux 			
		echo >> beeweeb.aux 			
		origline=`grep "prefix" $RDRCONF`
		start=`grep -n prefix $RDRCONF | cut -d\: -f1 | head -1`
		end=`grep -n $ENDTAG $RDRCONF | cut -d\: -f1 | head -1`
		stop=$(($end-$start-1))
		head -$(($start-1)) $RDRCONF > aux.0
		tail -$stop $RDRCONF > aux.2
		modified_line=""
		i=0
		while [ ok ] ; do
    	    i=$(($i+1))
    	    ax=`echo $origline | $CUT -d\$  -f$i`
    	    if [ "x$ax" = x ]; then
    	            break
    	    fi
    	    if [ "$ax" = "prefix" ] ; then
    	            modified_line="$modified_line$prefix"
    	    else
    	            modified_line="$modified_line$ax"
		    fi
		done
		echo $modified_line >> aux.1
		cat aux.0 aux.1 aux.2 > rdr_aux
		$RM aux.0 aux.1 aux.2
		echo "	  ...done"
	fi

 ;;

 esac

done

# case default module
if [ $MOD_DFT = "1" ] ; then
	echo "	creating default module config file ..."
	echo "  $LOADMTAG  mod_default.so" >> beeweeb.aux 			
	echo >> beeweeb.aux 			
fi

echo "</Server>" >> beeweeb.aux
echo >> beeweeb.aux
echo >> beeweeb.aux
echo "###  Section 2." >> beeweeb.aux
echo "#" >> beeweeb.aux
echo "#" >> beeweeb.aux


# configuration file

cat beeweeb.conf.tmpl beeweeb.aux sys_aux txt_aux log_aux adv_aux rdr_aux> beeweeb.conf
$RM -f log_aux sys_aux txt_aux rdr_aux adv_aux beeweeb.aux

echo "  -leaving conf directory"
cd ..

echo "Configuration file created"


echo ""
echo "==========================================================="; \
echo "***********************************************************"; \
echo "*                                                         *"; \
echo "*  You now have successfully configured your system.      *"; \
echo "*  You now should first run Makefile.                     *"; \
echo "*  Please, read the README document before starting       *"; \
echo "*  Beeweeb server compilation                             *"; \
echo "*                                                         *"; \
echo "***********************************************************"; \
echo "==========================================================="; \
echo ""

