#!/bin/csh -f

# user needs to supply:
# --htdocs : top-level of www files

set htdocs  = ""
set rootdir  = ""
set rootwww  = ""
set args    = ""
set VERBOSE = 0

while ("$1" != "") 
 switch ("$1")
  case --htdocs*
   if ("$1" == "--htdocs") then
     shift
     set htdocs = $1
   else
     set htdocs = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --rootdir*
   if ("$1" == "--rootdir") then
     shift
     set rootdir = $1
   else
     set rootdir = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case --rootwww*
   if ("$1" == "--rootwww") then
     shift
     set rootwww = $1
   else
     set rootwww = `echo $1 | tr = ' ' | awk '{print $2}'`
   endif
   breaksw;
  case -v:
  case --verbose:
   set VERBOSE = 1;
   breaksw;
  case -h:
  case --help:
   goto usage
  default:
   set args=($args $1);
   breaksw;
 endsw
 shift
end

if ($#args != 2) goto usage

if ($VERBOSE) then
  echo
  echo "htdocs : $htdocs"
  echo "rootdir: $rootdir"
  echo "rootwww: $rootwww"
  echo "input  : $args[1]"
  echo "output : $args[2]"
endif

if ("$htdocs" == "") goto usage
if ("$rootdir" == "") goto usage
if ("$rootwww" == "") goto usage

cat $args[1] | sed "s|@HTDOCS@|$htdocs|" | sed "s|@ROOTDIR@|$rootdir|" | sed "s|@ROOTWWW@|$rootwww|" > $args[2]
exit 0

usage:
if ($#args > 2) then
  shift args
  shift args
  echo unrecognized options: $args
endif

cat <<EOF

  USAGE: generate [OPTION] (input) (output)
  
  replace install-time options in scripts
  
  set the WWW installation directory root with --htdocs (top on server)
  set the WWW top-level directory with --rootdir (top seen by client)
    
  switches:
    -h, --help              display this help and exit
    --htdocs (required)     set the top-level directory (internal)
    --rootdir (required)    set the top-level directory (external)

EOF
 exit 2;
