#!/bin/sh

#     This is MANUALLY WRITTEN configure script, not one
#     made by autoconf, because it seems easier than
#     figuring out how to get autoconf to do this stuff.
#
#     If we start needing other autoconf stuff, then that
#     balance will change.   Also, my shell programming
#     may not be as robust.
#
#
#     NOTE THAT VARIABLES SHOULD APPEAR IN SEVERAL PLACES
#
#          - list of defaults below
#          - installation.pl.in
#          - sed command below (twice)
#
#   TO-DO:    change to have config.local be the over-rides
#

. config.defaults

if [ -f config.local ]; then
    echo "using config.local"
    . config.local
  else
    echo "no config.local found."
    echo " "
    echo "#  This config.local file generated on `date`" >> config.local
    echo "#"  >> config.local
    echo "#  It's a copy of config.defaults with the actual" >> config.local
    echo "#  assignment lines commented out.  Uncomment and" >> config.local
    echo "#  change the values you need to override." >> config.local
    echo "# " >> config.local
    sed -e 's/^\([^#]\)/#\1/' -e '/^#def/d'< config.defaults >> config.local
    echo "A config.local file has been generated for you.   Change anything"
    echo "in it you need to, then run $0 again."
    exit 1
fi

################################################################
##
##   Find Stuff
##

if [ -z "$swipl" ]; then
    #if swipl=`ls /usr/local/pl-*/bin/pl | tail -1`; then
    #    echo "Using prolog: $swipl"
    #else
        if swipl=`which pl`; then 
            echo "Using prolog: $swipl"
        else
            if swipl=`which swipl`; then 
                echo "Using prolog: $swipl"
            else
                echo "Can't find prolog.  'pl' or 'swipl' should be in path"
                exit 1
            fi
        fi
    #fi
fi


if [ "z" = "z$tidy" ]; then
    if tidy=`which tidy`; then 
            echo "Using tidy: $tidy"
        else
            echo "Can't find tidy.  'tidy' should be in path if you want it used."
            exit 0
    fi
fi

################################################################
##
##
##

if [ x"$init_dir" = xauto ]; then
    if [ -d /etc/rc.d/init.d ]; then
        init_dir=/etc/rc.d
    else
        if [ -d /etc/init.d ]; then
            init_dir=/etc
        else
            echo "Can't find system init.d; wont be able to install daemon controller"
            init_dir=
        fi
    fi
fi

################################################################
##
##   Store the config into prolog
##

echo '%  @@stamp@@' > config_data.pl.in
echo ':- dynamic app_config/2.' >> config_data.pl.in
#  generate lines like this:    app_config(port, '@@port@@').
sed -e '/^#/d' -e '/^ *$/d' -e 's/^\([^#=]*\).*$/app_config(\1, '"'"'@@\1@@'"'"')./' < config.defaults >>  config_data.pl.in
echo >>  config_data.pl.in


################################################################
##
##   Store the config into sh
##

echo '#  @@stamp@@' > config.out.in
#  generate lines like this:    port='@@port@@'
sed -e '/^#/d' -e '/^ *$/d' -e 's/^\([^#=]*\).*$/\1='"'"'@@\1@@'"'"'/' < config.defaults >>  config.out.in
echo >>  config.out.in


################################################################
##
##   Perform @@macro@@ substitution on *.in files
##
##   ??  rename ".in" to ".preconf" ??
##
##   We do this by creating a sed command-line to do the work
##
##   I can't figure out the right shell quoting to do this 
##   inside shell variables, so the sed command is built in
##   a file, which we then execute with ".".

echo 'sed < $file > $base \' > config.sedcmd
#  generate lines like this:     -e "s|@@port@@|$port|g" \
for count in `seq 4`; do
  # repeat all the substitution several times so that macros can 
  # expand via each other
  sed -e '/^#/d' -e '/^ *$/d' -e 's/^\([^#=]*\).*$/ -e "s|@@\1@@|$\1|g" \\/' < config.defaults >> config.sedcmd
  echo ' -e "s|@@stamp@@|$stamp|g" \' >> config.sedcmd
done
echo >> config.sedcmd

#for var in `sed -e '/^#/d' -e '/^ *$/d' -e 's/^\([^#=]*\).*$/\1/' < config.defaults`; do
#  export $var
#done

stamp="Generated by `/bin/pwd`/configure, `date`."
for file in Makefile.in test.sh.in startstop.in server-control.in config_data.pl.in config.out.in test_shared.sh.in ; do
  base=`basename $file .in`
  . config.sedcmd
  chmod --reference=$file $base
  echo wrote customized $base
done

