: /bin/sh
set +x

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

help ( ) {
echo "configure commands available:"
echo ""
echo "--help				Displays this page"
echo "--prefix=PREFIX			Use PREFIX as install directory"
echo "--with-tk=[no]			Build and install TK"
echo "--enable-shared=[yes]		Build shared objects"
echo "--enable-gcc=[yes]		Build with gcc if available"
echo "--link-statically=[no]		Link neoscript statically into Apache"
echo "--enable-module=[MODULE]	Build and load MODULE with NeoWebScript"
echo "MODULES:"
echo "	[auth_tcl]		Tcl Authorization module"
echo "	[log_neo]		NeoWebScript specific logging format"
echo "	[neo_userdir]		Modification of mod_userdir"
echo
exit 0
}

build_tk=no
build_shared=yes
enable_gcc=yes
link_static=no
ap_base=""

if [ $# != "0" ]
then
  for i in $@
  do
    case $i in
    --help)	help ;;
    --prefix=*) ap_base=`echo $i | sed -e 's/^[^=]*=//g'` ;install_apache=yes ;;
    --with-tk) build_tk=yes ;;
    --with-tk=*) build_tk=`echo $i | sed -e 's/^[^=]*=//g'` ;;
    --enable-shared) build_shared=yes ;;
    --enable-shared=*) build_shared=`echo $i | sed -e 's/^[^=]*=//g'` ;;
    --enable-gcc) enable_gcc=yes ;;
    --enable-gcc=*) enable_gcc=`echo $i | sed -e 's/^[^=]*=//g'` ;;
    --link-statically) link_static=yes ;;
    --link-statically=*) link_static=`echo $i | sed -e 's/^[^=]*=//g'` ;;
    --enable-module=*)
      mod="`echo $i | sed -e 's/^[^=]*=//g'`"
      case $mod in
      neoscript) ;;
      auth_tcl) MODULES="${MODULES} $mod" ;;
      log_neo) MODULES="${MODULES} $mod" ;;
      neo_userdir) MODULES="${MODULES} $mod" ;;
      *) echo "Bad module name '$mod'" ; exit 1 ;;
      esac
      ;;

    *)		echo "Bad or incomplete option $i" ; help ; exit 1 ;;
    esac
  done

fi

os=`neotcl8.1.1-lite/config/config.guess | cut -d - -f 3`
case $os in
  solaris*)
    if [ "$link_static" = "no" ]
    then
      echo "warning: building for Solaris and not linking statically"
      echo "	specify the --link-statically option on the command line"
    fi
    ;;
esac

if [ "$ap_base" = "" ]
then
  ap_base=/usr/local/apache

  echo "Configuration of NeoWebScript."

  cat << EOF
Where would you like NeoWebScript on your system? [$ap_base]
EOF

  read ans
  if [ "$ans" = "" ]
  then
    if [ "$ap_base" = "" ]
    then
      echo "I am sorry, but I must insist that you let us know where to install NeoWebScript."
      exit 1
    fi
  else
    ap_base=$ans
  fi

  # Look for Apache, and warn the user if it is there
  install_apache=yes
  if [ -d $ap_base ]
  then
    cat << EOF
The directory $ap_base exists already.  Do you want to
[O]verwrite, [A]bort Apache install but install everything else, or [Q]uit?
[O/A/Q] 
EOF

  read ans
  if [ "$ans" = "A" -o "$ans" = "a" ]
  then
    install_apache=no
  elif [ "$ans" = "Q" -o "$ans" = "q" ]
  then
    exit 1
  fi
  
fi

fi

APCONF="./configure --prefix=${ap_base} --enable-rule=SHARED_CORE --enable-module=so"
if [ "$link_static" = "yes" -o "$link_static" = "YES" ]
then
  MODDIR="`pwd`/modules"
  APCONF="${APCONF} --add-module=$MODDIR/mod_neoscript.c"
  for i in $MODULES
  do
    APCONF="${APCONF} --add-module=$MODDIR/mod_${i}.c"
  done
fi

if [ "$install_apache" = "yes" ]
then
    (
    cd apache_1.3.6
    ${APCONF}
    )
fi

if [ "$link_static" = "yes" -o "$link_static" = "YES" ]
then
  sh modules/buildPatch.sh ${ap_base}
  patch -s apache_1.3.6/src/Makefile < static1.patch
  patch -s apache_1.3.6/src/modules/extra/Makefile < static2.patch
fi

TCLCONF="./configure --prefix=${ap_base} --disable-make"
TOOLCONF="./configure --prefix=${ap_base}"
MODCONF="./configure --prefix=${ap_base}"
if [ "$build_tk" = "no" -o "$build_tk" = "NO" ]
then
  TCLCONF="${TCLCONF} --without-tk --enable-tk=NO"
fi

if [ "$build_shared" = "yes" -o "$build_shared" = "YES" ]
then
  TCLCONF="${TCLCONF} --enable-shared"
fi

if [ "$enable_gcc" = "yes" -o "$enable_gcc" = "YES" ]
then
  TCLCONF="${TCLCONF} --enable-gcc"
  TOOLCONF="${TOOLCONF} --enable-gcc"
fi

(
echo "Configuring for NeoTcl, Version 8.1.1-lite"
cd neotcl8.1.1-lite
${TCLCONF}
)

for i in $MODULES
do
  MODCONF="${MODCONF} --enable-module=${i}"
done

(
echo "Configuring for NeoWebScript, Version 3.2"
cd modules
${MODCONF}
)

(
echo "Configuring for NeoWebScript Tools"
cd tools
${TOOLCONF}
)

ALL="helpers"
APACHE="apache:"
APACHETXT="@echo Building Apache, Version 1.3.6"
NWSCONF=""
INSTALL="install-helpers install-nws install-htdocs install-misc"
if [ "$link_static" = "yes" -o "$link_static" = "YES" ]
then
  APACHE="$APACHE tcl install-tcl"
  INSTALL="install-apache $INSTALL"
  APACHETXT="$APACHETXT - Linking NeoWebScript Statically"
  NWSCONF="@sh modules/nwsModifyConf.sh \$(PREFIX)"
else
  ALL="tcl module $ALL"
  INSTALL="install-module $INSTALL"
fi

INSTALL_ALL="install-tcl install-module install-helpers install-nws-code install-htdocs install-misc"
if [ "$install_apache" = "yes" ]
then
  ALL="apache $ALL"
  INSTALL_ALL="install-apache $INSTALL_ALL"
fi

####
## Write this info into the final Makefile
####

cat > Makefile << EOF
##
## Makefile
## Constructed with $0 $@
##

PREFIX=$ap_base

all: $ALL

install: $INSTALL

install-all: $INSTALL_ALL

$APACHE
	$APACHETXT
	@(cd apache_1.3.6; make)

install-apache:
	@(cd apache_1.3.6; make install)
	$NWSCONF

EOF

cat < Makefile.in >> Makefile

cat << EOF
You may now do a "make"

Thank you!
EOF
