#!/bin/sh

#
# Save command line in a way that can be re-run.
# This takes care of spaces, but if there are shell-meta characters
# in the arguments, oops.
#

cmdline=
for arg in "$0" "$@" ; do
  [ -n "$cmdline" ] && cmdline="$cmdline "
  case $arg in
  *" "* | " "* | *" " )
    cmdline=$cmdline$(printf "\"%s\"" "$arg")
    ;;
  * )
    cmdline=$cmdline$arg
    ;;
  esac
done

#
# Default variables
#

prefix=/usr/local

#
# Parse configuration variables
#
while [ $# -gt 0 ] ; do
  case $1 in
  --no-* )
    var=${1#--no-}
    val=
    ;;
  --*=* )
    var=${1%=*}
    var=${var#--}
    val=${1#*=}
    ;;
  --*= )
    var=${1%=*}
    var=${var#--}
    val=
    ;; --* )
    var=${1#--}
    val=y
    ;;
  *=* )
    var=${1%=*}
    val=${1#*=}
    ;;
  *= )
    var=${1%=*}
    val=
    ;;
  * )
    printf "$0: '$1' doesn't look like a configuration variable assignment\n"
    printf "$0: use --help to get help\n"
    exit 1
  esac

  if ! printf $var | grep -q -E '^[[:alpha:]][[:alnum:]]*$' ; then
    printf "$0: '$var' isn't a proper configuration variable name\n"
    exit 1;
  fi

  eval "$var=\"$val\""
  shift
done

#
# If --help was given (or --help=<nonempty> or help=<nonempty>) then
# print help and exit. The termination status is failed, to indicate
# that configuration was not done.
#

if [ -n "$help" ] ; then
cat <<!

usage: $0 { variable=value }*\n"

The configure script prepares Meta-CVS for building and installation.
To configure a program means to establish the values of kakefile variables
which influence how the software is built, where it is installed. 
These variables can also influence what features are present in the
software, and can determine various defaults for those behaviors which are
dynamically configurable when the software is run.

Configuration variables are recorded in a file called config.make. This is a
GNU makefile, and consequently uses the GNU make syntax. It is included in the
main Makefile by an include statement.

The configure script is flexible. It allows variables to be entered in any
of these forms:

Canonical:

   variable=value      Defines the given variable as having the given value.
   variable=           Defines the variable as having an empty value.
                       An empty value serves as boolean false.

Long-option style:

   --variable=value    Same as 'variable=value', but resembles a GNU-style
                       long option.
   --variable          Same as 'variable=y'.
   --no-variable       Same as 'variable='.

No variables are required. The configure script establishes default values
for any variables which are needed by the build, but which are not specified
on the command line. 

After running $0, check that the config.make contents are sane.

!
  exit 1
fi 

#
# Variables are read, --help wasn't given, so let's configure!
#

#
# Grok software version from CVS varaible
#

name_key="$Name: mcvs-1-1-98 $"
save_ifs=$IFS ; IFS=":-$ " ; set -- $name_key ; IFS=$save_ifs
mcvs_ver_major=$3
mcvs_ver_minor=$4
mcvs_ver_patch=$5

#
# The all important banner. 
#

if [ $3 ] ; then
  banner_text=$(printf " Configuring Meta-CVS %d.%d.%d \n" $3 $4 $5)
else
  banner_text=" Configuring Meta-CVS (unknown version) "
fi
banner_box=$(printf "%.*s\n" ${#banner_text} \
		    "-------------------------------------------")
printf "+%s+\n|%s|\n+%s+\n" $banner_box "$banner_text" $banner_box

#
# From here on in, we bail if any command fails.
# 

set -e

#
# Verify sanity of --prefix
#

printf "Checking prefix %s ... " "$prefix"
case $prefix in
"" )
  ;;
" "* | *" "* | *" " )
  printf "incorrect (contains spaces)\n"
  exit 1
  ;;
-* )
  printf "incorrect (resembles a command option)\n"
  exit 1
  ;;
/* )
  ;;
* )
  printf "incorrect (must be absolute path)\n"
  exit 1
  ;;
esac

test_prefix=$prefix

while true ; do
  if [ -e $test_prefix ] ; then
    if [ ! -d $test_prefix ] ; then
      printf "incorrect ('%s' is not a directory)!\n" $test_prefix
      exit 1
    fi
    if [ ! -w $test_prefix ] ; then
      printf "okay (but no write access to '%s')\n" \
             "$test_prefix"
    else
      printf "okay\n"
    fi
    break
  fi
  test_prefix=$(dirname $test_prefix)
done

#
# Check for lndir
#

printf "Checking for lndir ... "

mkdir conf1.$$
touch conf1.$$/f
mkdir conf2
mkdir conf3
lndir -silent ../conf1.$$ conf2 > /dev/null 2>&1 && [ -h conf2/f ]
lndir_silent=$?
rm -rf conf2
lndir ../conf1.$$ conf3 > /dev/null 2>&1 && [ -h conf3/f ]
lndir_verbose=$?
rm -rf conf3
rm -rf conf1.$$

if [ $lndir_silent -eq 0 ] ; then
  printf "yes (supports -silent)\n"
  lndir_silent_flag=-silent
elif [ $lndir_verbose -eq 0 ] ; then
  printf "yes (no -silent)\n"
  lndir_silent_flag=
else
  printf "nonworking/missing\n"
  exit 1
fi

#
# Check for GNU make
#

printf "Checking for GNU Make ... "

output=$(make --version 2>&1)
set -- $output

if [ $1 != "GNU" -o $2 != "Make" ] ; then
  printf "missing\n"
  exit 1
fi

make_version=$3

save_ifs=$IFS ; IFS=. ; set -- $make_version ; IFS=$save_ifs

if [ $1 -lt 3 -o \( $1 -eq 3 -a $2 -lt 80 \) ] ; then
  printf "too old (%s found, 3.80 or newer needed)\n" $make_version
  exit 1
else
  printf "yes (%s found)\n" $make_version
fi

#
# Check for CLISP
#

printf "Checking for CLISP ... "

output=$(clisp --version 2>&1)
set -- $output

if [ $1 != "GNU" -o $2 != "CLISP" ] ; then
  printf "missing\n"
  exit 1
fi

clisp_version=$3

save_ifs=$IFS ; IFS=. ; set -- $clisp_version ; IFS=$save_ifs

if [ $1 -lt 2 -o \( $1 -eq 2 -a $2 -lt 38 \) ] ; then
  printf "too old (%s found, 2.38 or newer needed)\n" $clisp_version
  exit 1
else
  printf "yes (%s found)\n" $clisp_version
fi

#
# First, we have to figure out whether we are configured straight
# in the source directory, or whether we are in a separate build directory.
# In the latter case, we set up a symbolic link to the Makefile.
#
source_dir=$(dirname $0)

#
# Compute an absolute path to the source directory.
#
top_srcdir="$(cd "$source_dir" ; pwd -P)"

if [ "$source_dir" != "." ] ; then
  printf "symlinking Makefile -> $source_dir/Makefile\n"
  ln -sf "$source_dir/Makefile" .

  printf "symlinking code directory ...\n"
  mkdir -p code
  lndir $lndir_silent_flag $top_srcdir/code code
else
  printf "warning: its recommended to build in a separate directory\n"
fi

#
# Establish default values for any variables that are not specified
# on the command line.
#
prefix=${prefix-/usr/local}

#
# Save configuration in config.log
#
cat > config.log <<!

Configured on $(date) using

   $cmdline

!

#
# Finally, we generate config.make
#
printf "generating config.make\n"

cat > config.make <<!
# absolute path to source code directory
top_srcdir := $top_srcdir

# installation prefix
prefix := $prefix
!
