#!/bin/csh -f

# this is a very low-tech version of configure, not built by autoconf.

# user needs to supply:
# --htdocs : top-level of www files
# --dbhost : machine running database
# --dbuser : username for database access
# --dbpass : password for database access

set htdocs  = ""
set dbhost  = ""
set dbuser  = ""
set dbpass  = ""

set prefix  = ""
set bindir  = ""
set libdir  = ""
set datadir = ""

set root    = ""
set args    = ""

while ("$1" != "") 
 switch ("$1")
  # options passed by jhbuild or others which we ignore
  case --enable-maintainer-mode
  case --no-create
  case --no-recursion
  case --sbindir*
  case --libexecdir*
  case --sharedstatedir*
  case --localstatedir*
  case --oldincludedir*
  case --infodir*
  case --mandir*
  case --includedir*
  case --sysconfdir*
   # we need to strip the --opt word and --opt=word versions
   set word = `echo $1 | tr = ' '`
   if ($#word == 1) shift
   breaksw;
  case --htdocs*
   if ("$1" == "--htdocs") then
     shift
     set htdocs = $1
   else
     set htdocs = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --dbhost*
   if ("$1" == "--dbhost") then
     shift
     set dbhost = $1
   else
     set dbhost = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --dbuser*
   if ("$1" == "--dbuser") then
     shift
     set dbuser = $1
   else
     set dbuser = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --dbpass*
   if ("$1" == "--dbpass") then
     shift
     set dbpass = $1
   else
     set dbpass = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --prefix*
   if ("$1" == "--prefix") then
     shift
     set prefix = $1
   else
     set prefix = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --bindir*
   if ("$1" == "--bindir") then
     shift
     set bindir = $1
   else
     set bindir = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --libdir*
   if ("$1" == "--libdir") then
     shift
     set libdir = $1
   else
     set libdir = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --datadir*
   if ("$1" == "--datadir") then
     shift
     set datadir = $1
   else
     set datadir = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --help:
   goto usage
  case -*: 
   echo ""
   echo "Unknown option: $1"
   goto usage
  default:
   set args=($args $1);
   breaksw;
 endsw
 shift
end
if ($#args != 1) goto usage

echo
echo "prefix: $prefix"
echo "bindir: $bindir"
echo "libdir: $libdir"
echo "datadir:$datadir"
echo "htdocs: $htdocs"
echo "dbhost: $dbhost"
echo "dbuser: $dbuser"
echo "dbpass: $dbpass"
echo

if ("$htdocs" == "") goto usage
if ("$dbhost" == "") goto usage
if ("$dbuser" == "") goto usage
if ("$dbpass" == "") goto usage

# BINDIR holds the output binary files
if ("$bindir" == "") then
  if ("$prefix" == "") goto usage
  set bindir = $prefix/bin
endif

# LIBDIR set the install lib directory
if ("$libdir" == "") then
  if ("$prefix" == "") goto usage
  set libdir = $prefix/lib
endif

# DATADIR holds the general non-binary files
if ("$datadir" == "") then
  if ("$prefix" == "") goto usage
  set datadir = $prefix/share
endif

rm -f tmp.1 tmp.2

# convert Makefile.in to Makefile:
if (-e Makefile) mv -f Makefile Makefile~
cat Makefile.in | sed "s|@HTDOCS@|$htdocs|" > tmp.1
mv tmp.1 Makefile

# convert raw/site.php.in to raw/site.php
if (-e raw/site.php) mv -f raw/site.php raw/site.php~
cat raw/site.php.in | sed "s|@DBHOST@|$dbhost|"  > tmp.1
cat tmp.1           | sed "s|@DBUSER@|$dbuser|"  > tmp.2
cat tmp.2           | sed "s|@DBPASS@|$dbpass|"  > tmp.1
cat tmp.1           | sed "s|@BINDIR@|$bindir|"  > tmp.2
cat tmp.2           | sed "s|@PERLLIB@|$libdir|" > tmp.1
cat tmp.1           | sed "s|@SITE@|$datadir|"   > raw/site.php
rm -f tmp.1 tmp.2

# need to set the PERLLIB and BINDIR variables (use prefix, bindir, libdir)

exit 0

usage:
cat <<EOF
USAGE: configure [OPTION]

echo $#args
echo remaining args: $args

set the WWW installation directory root with --htdocs
set the IPP installation directory root with --prefix

set the database host, user, password with: --dbhost, --dbuser, -dbpass
 
either --prefix or all of --bindir, --libdir, --datadir are required

Configuration:
  -h, --help              display this help and exit

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX

Fine tuning of the installation directories:
  --bindir=DIR           user executables [PREFIX/bin/$ARCH] 
  --libdir=DIR           object code libraries [PREFIX/lib/$ARCH]
  --datadir=DIR          read-only architecture-independent data [PREFIX/share]

EOF
 exit 2;
