#!/bin/sh
#
# PHP/FI Install Script
#
# $Id: install,v 1.35 1997/01/12 21:53:05 rasmus Exp $
#
DEF_LOG_DIR=/usr/local/etc/httpd/cgi-data
DEF_ACC_DIR=/usr/local/etc/httpd/cgi-data

if echo '\c' | grep -s c >/dev/null 2>&1
then
	ECHO_N="echo -n"
	ECHO_C=""
else
	ECHO_N="echo"
	ECHO_C='\c'
fi

echo
echo "PHP/FI Installation"
echo
echo "If you do not understand any of the following questions,"
echo "then the default should be safe - simply hit 'Return'."
echo

$ECHO_N "Do you have mSQL installed on this system? [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "Top of your mSQL install tree? [/usr/local/Minerva] $ECHO_C"
HLM=-DHAVE_LIBMSQL=1
read INST_TOP
if [ "$INST_TOP." = "." ]
then
	INST_TOP=/usr/local/Minerva
fi
else
	INST_TOP=
fi

shome="/usr/local/sybase"
$ECHO_N "Do you have Sybase libraries installed on this system [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "Sybase home [$shome] $ECHO_C"
HLS=-DHAVE_SYBASE=1
read SYB_TOP
if [ "$SYB_TOP." = "." ]
then
    SYB_TOP=$shome
fi
else
    SYB_TOP=
fi

$ECHO_N "Do you have Postgres95 installed in this system? [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "Top of your Postgres95 install tree? [/usr/local/postgres95] $ECHO_C"
HLP=-DHAVE_LIBPQ=1
read PGINST_TOP
if [ "$PGINST_TOP." = "." ]
then
	PGINST_TOP=/usr/local/postgres95
fi
else
	PGINST_TOP=
fi

$ECHO_N "Do you have mysql installed on this system? [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "Top of your mysql install tree? [/usr/local/mysql] $ECHO_C"
HLMY=-DHAVE_LIBMYSQL=1
read INST_TOPY
if [ "$INST_TOPY." = "." ]
then
	INST_TOPY=/usr/local/mysql
fi
else
	INST_TOPY=
fi

$ECHO_N "Do you have Solid installed in this system? [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "Top of your Solid install tree? [/usr/local/solid] $ECHO_C"
HLS=-DHAVE_LIBSOLID=1
read SOLIDINST_TOP
if [ "$SOLIDINST_TOP." = "." ]
then
   SOLIDINST_TOP=/usr/local/solid
fi
else
   SOLIDINST_TOP=
fi

$ECHO_N "Would you like to compile PHP/FI as an Apache module? [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "Are you compiling for an Apache 1.1 or later server? [Yn] $ECHO_C"
read APACHE_API
if [ "$APACHE_API." = "." ] || [ "$APACHE_API" = "y" ] || [ "$APACHE_API" = "Y" ]
then
	APACHE_API=-DAPACHE_NEWAPI=1
else
	APACHE_API=
fi

$ECHO_N "Does your Apache server support ELF dynamic loading? [yN] $ECHO_C"
read APACHE_ELF_DLOPEN
if [ "$APACHE_ELF_DLOPEN." = "." ] || [ "$APACHE_ELF_DLOPEN" = "n" ] || [ "$APACHE_ELF_DLOPEN" = "N" ]
then
    MODULE_TARGET=module_user_build
else
    MODULE_TARGET=module_auto_build
    if [ "$INST_TOP" != "" -o "$PGINST_TOP" != "" ]
    then
        $ECHO_N "Do you want to build the dynamic, static, or both modules? [dsB] $ECHO_C"
        read answer
	case .$answer in
	    .|.b|.B)
		MODULE_PHP="mod_php-dynamic.so mod_php-static.so"
		;;
	    .|.s|.S)
		MODULE_PHP="mod_php-static.so"
		;;
	    *)
		MODULE_PHP="mod_php-dynamic.so"
		;;
	esac
    fi
fi
if [ $MODULE_TARGET = module_auto_build ]
then
    apache_dir=/usr/local/include/apache
    $ECHO_N "Apache include directory (which has httpd.h)? [$apache_dir] $ECHO_C"
else
    apache_dir=/usr/local/etc/httpd/src
    $ECHO_N "Apache source directory (which has httpd.h)? [$apache_dir] $ECHO_C"
fi
APACHE=-DAPACHE=1
PRG=libphp.a
read APACHE_DIR
if [ "$APACHE_DIR." = "." ]
then
	APACHE_DIR=$apache_dir
fi
else
	APACHE=
	PRG=php.cgi
fi

if [ "$APACHE." = "." ]
then
$ECHO_N "Do you want to dynamically link utility libraries? [Yn] $ECHO_C"
read a
if [ "$a" = "" -o "$a" = y -o "$a" = Y ]
then
    USEDLIBS='$(LIBS)'
else
    USEDLIBS='$(STATICLIBS)'
fi

$ECHO_N "Would you like to compile PHP/FI with FastCGI support? [yN] $ECHO_C"
read a
if [ "$a" = "y" ] || [ "$a" = "Y" ]
then
$ECHO_N "FastCGI development kit source directory? [/usr/local/etc/httpd/fcgi-devel-kit] $ECHO_C"
PHPFASTCGI=-DPHPFASTCGI=1
PRG=php.cgi
read FCGI_DIR
if [ "$FCGI_DIR." = "." ]
then
	FCGI_DIR=/usr/local/etc/httpd/fcgi-devel-kit
fi
else
	PHPFASTCGI=
	PRG=php.cgi
fi
fi

$ECHO_N "Would you like to include access control support? [yN] $ECHO_C"
read a
if [ "$a." = "." ] || [ "$a" = "n" ] || [ "$a" = "N" ]
then
	ACCESS_CONTROL=
	ACCESS_DIR=
else
	ACCESS_CONTROL=-DACCESS_CONTROL
	echo 
	echo "You need to choose an access control directory."
	echo "This directory must be readable and writable by the"
	echo "user id under which your http server runs."
	echo
	$ECHO_N "Access control directory? [$DEF_ACC_DIR] $ECHO_C"
	read b
	if [ "$b." = "." ]
	then
		b=$DEF_ACC_DIR
	fi
	ACCESS_DIR=-DACCESS_DIR=\\\\\"$b\\\\\"
fi

$ECHO_N "Would you like to include page logging support? [Yn] $ECHO_C"
read a
if [ "$a." = "." ] || [ "$a" = "y" ] || [ "$a" = "Y" ]
then
	if [ "$HLM" = "-DHAVE_LIBMSQL=1" ]
	then
		if [ "$HLMY" = "-DHAVE_LIBMYSQL=1" ]
		then
			$ECHO_N "Would you like to store your log files in dbm, mSQL, or mysql format? [dmy] $ECHO_C"
		else
			$ECHO_N "Would you like to store your log files in dbm or mSQL format? [dm] $ECHO_C"
		fi
		read a
		if [ "$a" = "d" ] || [ "$a" = "D" ]
		then	
			LOGGING=-DLOGGING=1
			echo 
			echo "You need to choose a page logging directory."
			echo "This directory must be readable and writable by the"
			echo "user id under which your http server runs."
			echo
			$ECHO_N "Page logging directory? [$DEF_LOG_DIR] $ECHO_C"
			read b
			if [ "$b." = "." ]
			then
				b=$DEF_LOG_DIR
			fi
			LOG_DIR=-DLOG_DIR=\\\\\"$b\\\\\"
		else
			if [ "$a" = "m" ] || [ "$a" = "M" ]
			then
				LOGGING=-DMSQLLOGGING=1
				echo
				echo "mSQL Logging enabled.  Note that you will need to create a"
				echo "phpfi database and appropriate tables for each user id for"
				echo "which logging should be done.  See the documentation doc/doc.html"
				echo "for details."
				echo 
			else
				LOGGING=-DMYSQLLOGGING=1
				echo
				echo "mysql Logging enabled.  Note that you will need to create a"
				echo "phpfi database and appropriate tables for each user id for"
				echo "which logging should be done.  See the documentation doc/doc.html"
				echo "for details."
				echo 
			fi
		fi
	else
		if [ "$HLMY" = "-DHAVE_LIBMYSQL=1" ]
		then
			$ECHO_N "Would you like to store your log files in dbm or mysql format? [dy] $ECHO_C"
			read a
			if [ "$a" = "d" ] || [ "$a" = "D" ]
			then	
				LOGGING=-DLOGGING=1
				echo 
				echo "You need to choose a page logging directory."
				echo "This directory must be readable and writable by the"
				echo "user id under which your http server runs."
				echo
				$ECHO_N "Page logging directory? [$DEF_LOG_DIR] $ECHO_C"
				read b
				if [ "$b." = "." ]
				then
					b=$DEF_LOG_DIR
				fi
				LOG_DIR=-DLOG_DIR=\\\\\"$b\\\\\"
			else
				LOGGING=-DMYSQLLOGGING=1
				echo
				echo "mysql Logging enabled.  Note that you will need to create a"
				echo "phpfi database and appropriate tables for each user id for"
				echo "which logging should be done.  See the documentation doc/doc.html"
				echo "for details."
				echo 
			fi
		else
			LOGGING=-DLOGGING=1
			echo 
			echo "You need to choose a page logging directory."
			echo "This directory must be readable and writable by the"
			echo "user id under which your http server runs."
			echo
			$ECHO_N "Page logging directory? [$DEF_LOG_DIR] $ECHO_C"
			read b
			if [ "$b." = "." ]
			then
				b=$DEF_LOG_DIR
			fi
			LOG_DIR=-DLOG_DIR=\\\\\"$b\\\\\"
		fi
	fi
else
	LOGGING=
	LOG_DIR=
fi

$ECHO_N "Would you like to include file upload support? [Yn] $ECHO_C"
read a
if [ "$a." = "." ] || [ "$a" = "y" ] || [ "$a" = "Y" ]
then
	FILE_UPLOAD=-DFILE_UPLOAD
else
	FILE_UPLOAD=
fi

OPT_CFLAGS=-O

$ECHO_N "Would you like to build an ELF shared library? [yN] $ECHO_C"
read a
if [ "$a" = "" -o "$a" = n -o "$a" = N ]
then
    :
    PHPLIB_TARGET=staticlib
else
    SYS_CFLAGS=-fPIC
    SHLIB_TARGET=sharedlib
    PHPLIB_TARGET=sharedlib
fi

$ECHO_N "Additional directories to search for .h files []: $ECHO_C"
read a
if [ "$a." = "." ]
then
	ADD_INC_DIRS=
else
	ADD_INC_DIRS=`echo "$a" | sed 's@  *@ @g; s@^\([^ ]\)@-I\1@; s@ \([^ ]\)@ -I\1@g'`
	INC_CFLAGS="$ADD_INC_DIRS"
fi

$ECHO_N "Additional directories to search for library files []: $ECHO_C"
read a
if [ "$a." = "." ]
then
	ADD_LIB_DIRS=
else
	ADD_LIB_DIRS=`echo "$a" | sed 's@  *@ @g; s@^\([^ ]\)@-L\1@; s@ \([^ ]\)@ -L\1@g'`
	LDFLAGS=$ADD_LIB_DIRS
	export LDFLAGS
fi

./configure

mv src/Makefile src/Makefile.$$

echo "$SED_MSQL"
# mSQL part of sed  command
if [ "$INST_TOP." = "." ]
then
SED_MSQL="      s,@MSQL_INCLUDE@,,
                s,@LIBMSQL@,,
		s,@STATICLIBMSQL@,,
                s,@MSQL_DEF@,,
                s,@MSQL_LIB@,,"
else
if [ -d "$INST_TOP/include/msql" ]
then
    MSQL_INCDIR=/msql
fi
if [ -d "$INST_TOP/lib/msql" ]
then
    MSQL_LIBDIR=/msql
fi
SED_MSQL="      s,@MSQL_INCLUDE@,-I$INST_TOP/include$MSQL_INCDIR,
                s,@LIBMSQL@,-lmsql,
		s,@STATICLIBMSQL@,$INST_TOP/lib$MSQL_LIBDIR/libmsql.a,
                s,@MSQL_DEF@,$HLM,
                s,@MSQL_LIB@,-L$INST_TOP/lib$MSQL_LIBDIR,"
fi

# Sybase part of sed command   
# ma_muquit@fccc.edu, Sep-15-96
if [ "$SYB_TOP." = "." ]
then
SED_SYB="      s,@SYB_INCLUDE@,,
               s,@LIBSYB@,,
               s,@STATICLIBSYB@,,
               s,@SYB_DEF@,,
               s,@SYB_LIB@,,"
else
SED_SYB="      s,@SYB_INCLUDE@,-I$SYB_TOP/include,
               s,@LIBSYB@, -lsybdb -ldnet_stub,
               s,@STATICLIBSYB@,-lsybdb -ldnet_stub,
               s,@SYB_DEF@,$HLS,
               s,@SYB_LIB@,-L$SYB_TOP/lib,"
fi  

# Postgres95 part of sed command

if [ "$PGINST_TOP." = "." ]
then
SED_PG95="      s,@PQ_INCLUDE@,,
                s,@LIBPQ@,,
                s,@STATICLIBPQ@,,
                s,@PQ_DEF@,,
                s,@PQ_LIB@,,"
else
if [ -d "$PGINST_TOP/include/postgres95" ]
then
    PG95_INCDIR=/postgres95
fi
if [ -d "$PGINST_TOP/lib/postgres95" ]
then
    PG95_LIBDIR=/postgres95
fi
SED_PG95="      s,@PQ_INCLUDE@,-I$PGINST_TOP/include$PG95_INCDIR,
                s,@LIBPQ@,-lpq,
		s,@STATICLIBPQ@,$PGINST_TOP/lib$PG95_LIBDIR/libpq.a,
                s,@PQ_DEF@,$HLP,
                s,@PQ_LIB@,-L$PGINST_TOP/lib$PG95_LIBDIR,"
fi

# mysql part of sed  command
if [ "$INST_TOPY." = "." ]
then
SED_MYSQL="      s,@MYSQL_INCLUDE@,,
                s,@LIBMYSQL@,,
		s,@STATICLIBMYSQL@,,
                s,@MYSQL_DEF@,,
                s,@MYSQL_LIB@,,"
else
if [ -d "$INST_TOPY/include/mysql" ]
then
    MYSQL_INCDIR=/mysql
fi
if [ -d "$INST_TOPY/lib/mysql" ]
then
    MYSQL_LIBDIR=/msql
fi
SED_MYSQL="      s,@MYSQL_INCLUDE@,-I$INST_TOPY/include$MYSQL_INCDIR,
                s,@LIBMYSQL@,-lmysql -lmysys -lstrings,
		s,@STATICLIBMYSQL@,$INST_TOPY/lib$MYSQL_LIBDIR/libmysql.a,
                s,@MYSQL_DEF@,$HLMY,
                s,@MYSQL_LIB@,-L$INST_TOPY/lib$MYSQL_LIBDIR,"
fi

# Solid part of sed command

if [ "$SOLIDINST_TOP." = "." ]
then
SED_SOLID="     s,@SOLID_INCLUDE@,,
                s,@LIBSOLID@,,
                s,@STATICLIBSOLID@,,
                s,@SOLID_DEF@,,
                s,@SOLID_LIB@,,"
else
if [ -d "$SOLIDINST_TOP/include" ]
then
    SOLID_INCDIR=include
fi
if [ -d "$SOLIDINST_TOP/lib" ]
then
    SOLID_LIBDIR=lib
fi
SED_SOLID="     s,@SOLID_INCLUDE@,-I$SOLIDINST_TOP/$SOLID_INCDIR,
                s,@LIBSOLID@,$SOLIDINST_TOP/$SOLID_LIBDIR/solcli.a,
               s,@STATICLIBSOLID@,$SOLIDINST_TOP/$SOLID_LIBDIR/solcli.a,
                s,@SOLID_DEF@,$HLS,
                s,@SOLID_LIB@,,"
fi


# FastCGI substitutions
if [ "$PHPFASTCGI." = "." ]
then
SED_FCGI="      s,@FCGI_INCLUDE@,,
                s,@LIBFCGI@,,
                s,@STATICLIBFCGI@,,
                s,@FCGI_LIB@,,
                s,@FCGI_DEF@,,"
else
SED_FCGI="      s,@FCGI_INCLUDE@,-I$FCGI_DIR/include,
		s,@FCGI_DEF@,$PHPFASTCGI,
                s,@LIBFCGI@,-lfcgi,
                s,@STATICLIBFCGI@,$FCGI_DIR/lib/libfcgi.a,
                s,@FCGI_LIB@,-L$FCGI_DIR/libfcgi,"
fi

# Apache module section

if [ "$APACHE_DIR." = "." ]
then
SED_APACHE="      s,@APACHE_INCLUDE@,,"
else
SED_APACHE="      s,@APACHE_INCLUDE@,-I$APACHE_DIR,"
fi

if [ -f /etc/debian_version ]
then
    DEBIAN=-DDEBIAN
else
    DEBIAN=
fi

if grep "sendmail" config.cache >/dev/null 2>&1
then
	PHPSENDMAIL="-DPHPSENDMAIL=1"
else
	PHPSENDMAIL=""
fi

if grep "#define HAVE_REGCOMP 1" config.h >/dev/null 2>&1
then
	echo "Your system appears to have a Posix compliant regex library";
	echo "On some systems this library is broken.  UnixWare 2.0.x is an"
	echo "example.  Answering [Y]es to the following question will"
	echo "compile in the bundled regex library instead of yours."
	echo "Note that if you are compiling the module version for"
	echo "Apache 1.1.1 or earlier, answer No here.  Unless you are"
	echo "certain that your system library is broken, answer No here."
	echo ""
	echo "*IMPORTANT* If you are compiling the Apache module, make sure"
	echo "you set Apache's regexp setting accordingly in the Configuration"
	echo "file.  ie. WANTHSREGEX=no if you answer No here."
	echo ""
	$ECHO_N "Would you like the bundled regex library? [yN] $ECHO_C"
	read a
	if [ "$a" = y -o "$a" = Y ]
	then
		echo "Using the included Posix regular expression library"
		echo ""
		LIBREGEX=-lregex	
		LIBREGEXDIR=-L./regex
		REGEX_INCLUDE=-I./regex
		OURREGEX=regex/libregex.a
		REGEXMSG1="For Apache 1.1.1 or below you must copy src/regex/libregex.a to $APACHE_DIR and add -llibregex.a to the above"
		REGEXMSG2="For Apache 1.2 or higher you must set the WANTHSREGEX Configuration parameter to 'yes'"
		STATICLIBREGEX=regex/libregex.a
		sed "s,HAVE_REGCOMP,PHP_BROKEN_SYSTEM_REGEX," <config.h >config.$$
		rm -f config.h
		mv config.$$ config.h
	else
		echo "Using your system's Posix regular expression library"
		echo ""
		LIBREGEX=
		STATICLIBREGEX=
		LIBREGEXDIR=
		REGEX_INCLUDE=
		OURREGEX=
		REGEXMSG1=""
		REGEXMSG2="For Apache 1.2 you must set the WANTHSREGEX Configuration parameter to 'no'"
	fi
else
	echo "Using the bundled Posix regular expression library."
	echo "If you are compiling the module for Apache 1.1.1 or earlier"
	echo "you may experience difficulties.  It is strongly advised"
	echo "that you upgrade to Apache 1.2"
	echo ""
	LIBREGEX=-lregex	
	LIBREGEXDIR=-L./regex
	REGEX_INCLUDE=-I./regex
	OURREGEX=regex/libregex.a
	REGEXMSG1="For Apache 1.1.1 you must copy src/regex/libregex.a to $APACHE_DIR and add -llibregex.a to the above"
	REGEXMSG2="For Apache 1.2 you must set the WANTHSREGEX Configuration parameter to 'yes' for Apache 1.2"
	STATICLIBREGEX=regex/libregex.a
fi

# sed commmand
sed "		$SED_MSQL
        $SED_SYB
		$SED_PG95
		$SED_MYSQL
		$SED_SOLID
		$SED_APACHE
		$SED_FCGI
		s,@USEDLIBS@,$USEDLIBS,
		s,@OPT_CFLAGS@,$OPT_CFLAGS,
		s,@INC_CFLAGS@,$INC_CFLAGS,
		s,@SYS_CFLAGS@,$SYS_CFLAGS,
		s,@PHPSENDMAIL@,$PHPSENDMAIL,
		s,@HGD@,$HGD,
		s,@HLNK@,$HLNK,
		s,@ACCESS_CONTROL@,$ACCESS_CONTROL,
		s,@ACCESS_DIR@,$ACCESS_DIR,
		s,@LOGGING@,$LOGGING,
		s,@LOG_DIR@,$LOG_DIR,
		s,@FILE_UPLOAD@,$FILE_UPLOAD,
		s,@MODULE_PHP@,$MODULE_PHP,
		s,@APACHE@,$APACHE,
		s,@APACHE_DIR@,$APACHE_DIR,
		s,@APACHE_API@,$APACHE_API,
		s,@PRG@,$PRG,
		s,@DEBIAN@,$DEBIAN,
		s,@MODULE_TARGET@,$MODULE_TARGET,
		s,@SHLIB_TARGET@,$SHLIB_TARGET,
		s,@PHPLIB_TARGET@,$PHPLIB_TARGET,
		s,@LIBREGEX@,$LIBREGEX,
		s,@REGEXMSG1@,$REGEXMSG1,
		s,@REGEXMSG2@,$REGEXMSG2,
		s,@LIBREGEXDIR@,$LIBREGEXDIR,
		s,@REGEX_INCLUDE@,$REGEX_INCLUDE,
		s,@OURREGEX@,$OURREGEX,
		s,@STATICLIBREGEX@,$STATICLIBREGEX,
" < src/Makefile.$$ > src/Makefile
rm src/Makefile.$$

sed "s,HAVE_MMAP,PHP_HAVE_MMAP," < config.h >config.$$
mv config.$$ config.h

if [ -f src/main.o ]
then
rm -f src/*.o src/*.a src/regex/*.o src/regex/*.a
fi

if [ -f src/php.cgi ]
then
rm -f src/php.cgi
fi

if [ -f src/libphp.a ]
then
rm -f src/libphp.a
fi

echo "If none of -ldbm, -ldb nor -lgdbm were found, you should"
echo "probably get one of them.  It will work without these, but"
echo "but performance-wise you will gain a lot by installing"
echo "gdbm."
echo ""
echo "If the above item is ok, you are ready to compile"
if [ -f src/parse.c ]
then
echo "the package.  Type: cd src; make"
else
echo "the package.  Type: cd src; make parser; make"
fi
