#!/bin/sh
set +x

help ( ) {
echo "configure commands available:"
echo ""
echo "--help			displays this page"
echo "--prefix=PREFIX		use PREFIX as install directory"
echo "--srcdir=SRCPREFIX	use SRCPREFIX as src directory"
echo "--enable-shared		build with shared objects"
exit 0
}

default_prefix=/usr/local/apache
src_prefix=../neotcl8.1.1-lite
shared=no

for i in $@
do
  case $i in
  --help) help ;;
  --prefix*) default_prefix=`echo $i | sed -e 's/^[^=]*=//g'` ;;
  --srcdir*) src_prefix=`echo $i | sed -e 's/^[^=]*=//g'` ;;
  --enable-shared) shared=yes ;;
  *) echo "Invalid configure option $i" ; help ; exit 1 ;;
  esac
done

if [ $# != "0" ]
then
	config_location=$src_prefix/tcl8.1.1/unix/tclConfig.sh
	neoconfig_location=$src_prefix/neo8.1.1/generic/neoConfig.sh
	ap_base=$default_prefix
	apxs_loc=$ap_base/bin/apxs
	db_loc=$default_prefix/BerkeleyDB
	db_lib_spec=$db_loc/lib/libdb.a
	if [ $shared = "yes" ]
	then
	  db_lib_spec="-L $db_loc/lib -l db"
	fi

	if [ ! -f $config_location ]
	then
	  echo "Could not find tclConfig.sh in `dirname $config_location`."
	  echo "Tcl was not configured properly."
	  exit 1
	fi
	. $config_location

	if [ ! -f $neoconfig_location ]
	then
	  echo "Could not find neoConfig.sh in `dirname $neoconfig_location`."
	  echo "NeoTcl was not configured properly."
	  exit 1
	fi
	. $neoconfig_location

	tcl_lib_spec=`echo $TCL_LIB_SPEC | sed -e 's/-L/-L /g;s/-l/-l /g'`
	tcl_lib_spec=`eval echo $tcl_lib_spec`
	tclx_lib_spec=`echo $TCLX_LIB_SPEC | sed -e 's/-L/-L /g;s/-l/-l /g'`
	tclx_lib_spec=`eval echo $tclx_lib_spec`
	tcl_libs=`echo $TCL_LIBS | sed -e 's/-L/-L /g;s/-l/-l /g'`
	tcl_libs=`eval echo $tcl_libs`
	tclx_libs=`echo $TCLX_LIBS | sed -e 's/-L/-L /g;s/-l/-l /g'`
	tclx_libs=`eval echo $tclx_libs`
	neo_libs=`echo $NEO_LIBS | sed -e 's/-L/-L /g;s/-l/-l /g'`
	neo_libs=`eval echo $neo_libs`
	neo_lib_spec=`echo $NEO_LIB_SPEC | sed -e 's/-L/-L /g;s/-l/-l /g'`
	neo_lib_spec=`eval echo $neo_lib_spec`

	sysconfdir=$ap_base/conf
	sbindir=$ap_base/bin
	targetdir=$ap_base

####
## Write this info into the final Makefile
####
cat > Makefile << EOF
##
## Locations to use in building NeoWebScript
## Constructed from configure script
##

APXS=$apxs_loc
APACHE_BASE=$ap_base
TCLDIR=$src_prefix/tcl8.1.1/unix
TCLCONFIGSH=\${TCLDIR}/tclConfig.sh
TCLXDIR=$src_prefix/tclX8.1.1/unix
TCLXCONFIGSH=\${TCLXDIR}/tclConfig.sh
DBDIR=$db_loc
DB_LIB_SPEC=$db_lib_spec
TCL_LIB_SPEC=$tcl_lib_spec $neo_lib_spec
TCLX_LIB_SPEC=$tclx_lib_spec
TCL_LIBS=$tcl_libs $neo_libs
TCLX_LIBS=$tclx_libs
TCL_SHLIB_SUFFIX=$TCL_SHLIB_SUFFIX
SYSCONFDIR=$sysconfdir
SBINDIR=$sbindir
PREFIX=$targetdir
EOF

cat < Makefile.in >> Makefile

LD_LIBRARY_PATH=$default_prefix/lib:$LD_LIBRARY_PATH

exit 0
fi

####
## This script gets some directory locations to use in the Makefile
####

# This script writes out a nwsConfig.sh file that sets some environment
# variables that give the location of Tcl, Db, etc.

echo "Configuring NeoWebScript-3.1 module."

default_prefix=/usr/local
ap_base=""
# try to find Apache
for dir in ${default_prefix}/apache*
do
    if [ -d ${dir} ]
    then
	ap_base=${dir}
	cat << EOF
I found a version of Apache running in $ap_base.
EOF
    fi
    break
done

#ask the user to locate Apache
cat << EOF
Where is Apache running on your system? [$ap_base]
EOF

read ans
if [ "X${ans}" = "X" ]
then
  if [ "X${ap_base}" = "X" ]
  then
    echo "I am sorry, but I must insist that you let us know where Apache is."
    exit 1
  fi
else
  ap_base=${ans}
fi

if [ ! -d $ap_base ]
then
  cat << EOF
The directory $ap_base does not contain Apache.
Please check your path.
EOF
exit 1
fi

apxs_loc=""

# Look for apxs binary, quit if we cannot find it

for f in ${ap_base}/bin/apxs ${ap_base}/sbin/apxs
do
    if [ -x $f ]
    then
        apxs_loc=${f}
        break
    else
        cat << EOF
The directory $ap_base does not contain the Apache apxs script.
Please check your path.
EOF
        exit 1
    fi
done

default_prefix=/usr/local/apache

## We now find Tcl
tcl_loc=""
if [ -r $default_prefix/lib/tclConfig.sh ]
then
    tcl_loc=$default_prefix
    cat << EOF
I found a version of Tcl in $tcl_loc.
EOF
fi

    cat << EOF
Where is Tcl installed on your system? [$tcl_loc]
EOF

read ans
if [ "$ans" = "" ]
then
  if [ "$tcl_loc" = "" ]
  then
    echo "I am sorry, but I must insist that you let us know where Tcl is installed."
    exit 1
  fi
else
  tcl_loc=$ans
fi

## Did they give us a good directory?
if [ ! -r $tcl_loc/lib/tclConfig.sh ]
then
  cat << EOF
The directory $tcl_loc/lib does not contain tclConfig.sh.
Please check your path.
EOF
exit 1
fi
tclConfig_loc="$tcl_loc/lib/tclConfig.sh"

## We look for TclX in the same place first, then ask them if needed
tclx_loc=""
if [ -r $tcl_loc/lib/tclxConfig.sh ]
then
    tclx_loc=$tcl_loc
else
    cat << EOF
Where is TclX installed on your system? [$tclx_loc]
EOF

  read ans
  if [ "$ans" = "" ]
  then
    if [ "$tclx_loc" = "" ]
    then
      echo "I am sorry, but I must insist that you let us know where TclX is installed."
      exit 1
    fi
  else
    tclx_loc=$ans
  fi
fi

## Did they give us a good directory?
if [ ! -r $tclx_loc/lib/tclxConfig.sh ]
then
  cat << EOF
The directory $tclx_loc/lib does not contain tclxConfig.sh.
Please check your path.
EOF
exit 1
fi
tclxConfig_loc="$tclx_loc/lib/tclxConfig.sh"

####
## We need to find DB now
####
db_file=""
for f in \
$default_prefix/BerkeleyDB/lib/libdb.a \
$default_prefix/BerkeleyDB/lib/libdb.so \
$default_prefix/lib/libdb.a \
$default_prefix/lib/libdb.so
do
  if [ -r $f ]
  then
    db_file=$f
    echo "I found a version of Berkeley DB in `dirname $f`."
    break
  fi
done

echo "Where is your libdb file for Berkeley DB (libdb.a or libdb.so)?"
echo "[$db_file]"

read ans
if [ "$ans" = "" ]
then
  if [ "$db_file" = "" ]
  then
    echo "I am sorry, but I must insist that you let us know where Berkeley DB is installed."
    exit 1
  fi
else
  db_file=$ans
fi

db_loc=`dirname $db_file`
db_loc=`dirname $db_loc`

case $db_file in
*libdb.a) db_lib_spec=$db_file ;;
*libdb.so) db_lib_spec="-L $db_loc -l db" ;;
*) echo "$db_file is not a valid libdb file.\nPlease check your path.";exit 1 ;;
esac

## Did they give us a good directory (so we can find the file)?
if [ ! -r $db_file ]
then
  cat << EOF
$db_file is not a valid libdb file.
Please check your path.
EOF
exit 1
fi

. $tclConfig_loc

tcl_lib_spec=`echo $TCL_LIB_SPEC | sed -e 's/-L/-L /g;s/-l/-l /g'`
tcl_lib_spec=`eval echo $tcl_lib_spec`
tclx_lib_spec=`echo $TCLX_LIB_SPEC | sed -e 's/-L/-L /g;s/-l/-l /g'`
tclx_lib_spec=`eval echo $tclx_lib_spec`
tcl_libs=`echo $TCL_LIBS | sed -e 's/-L/-L /g;s/-l/-l /g'`
tcl_libs=`eval echo $tcl_libs`
tclx_libs=`echo $TCLX_LIBS | sed -e 's/-L/-L /g;s/-l/-l /g'`
tclx_libs=`eval echo $tclx_libs`

sysconfdir=`$apxs_loc -q SYSCONFDIR`
sbindir=`$apxs_loc -q SBINDIR`
targetdir=`$apxs_loc -q PREFIX`

####
## Write this info into the final Makefile
####
cat > Makefile << EOF
##
## Locations to use in building NeoWebScript
## Constructed from configure script
##

APXS=$apxs_loc
APACHE_BASE=$ap_base
TCLDIR=$tcl_loc
TCLCONFIGSH=$tclConfig_loc
TCLXDIR=$tclx_loc
TCLXCONFIGSH=$tclxConfig_loc
DBDIR=$db_loc
DB_LIB_SPEC=$db_lib_spec
TCL_LIB_SPEC=$tcl_lib_spec
TCLX_LIB_SPEC=$tclx_lib_spec
TCL_LIBS=$tcl_libs
TCLX_LIBS=$tclx_libs
TCL_SHLIB_SUFFIX=$TCL_SHLIB_SUFFIX
SYSCONFDIR=$sysconfdir
SBINDIR=$sbindir
PREFIX=$targetdir
EOF

cat < Makefile.in >> Makefile

cat << EOF
You may now do a "make" to build the module.

Thank you!
EOF

LD_LIBRARY_PATH=$default_prefix/lib:$LD_LIBRARY_PATH
