#! /bin/sh
# Usage: configure [options] [dirname [cc]]
# Examples:
#   configure make.gcc gcc
#   configure make.cc cc
#   configure obj

info_help () {
cat << \EOP
Usage: configure [options] [dirname]
dirname: Name of directory in which to build CLISP. This allows you to build
         CLISP with different compilers on the same machine or for different
         architectures in the same filesystem, sharing the same source.
         Default is `src'.
options: The following options are recognized:
         --help                print this message and exit
         --version             print the CLISP version and exit
         --config              unix/INSTALL step 3: configuration only
         --build               unix/INSTALL steps 3-7: configure, build, check
         --install             unix/INSTALL step 8: install
         The following options are passed to subordinate `configure' scripts:
         --quiet, --silent     do not print `checking...' messages
         --prefix=PREFIX       base directory for files to be installed
         --exec-prefix=PREFIX  base directory for architecture-dependent
                               files to be installed
         --with-PACKAGE        use PACKAGE
         --without-PACKAGE     do not use PACKAGE
Example:
         configure --build built-with-gcc
         su bin
         configure --install built-with-gcc
         exit
Specifying the C compiler:
         If you wish to specify the C compiler that will get used to build
         CLISP, set the environment variables CC, CFLAGS, LIBS. Put compiler
         flags that have to be passed to the preprocessor into the CC
         variable, not the CFLAGS variable. For example, if you want to use
         gcc in ANSI C mode, execute the following before calling `configure':
               setenv CC "gcc -ansi"                 if using csh
               export CC="gcc -ansi"                 if using bash or ksh
               CC="gcc -ansi"; export CC             if using sh
EOP
}

# Abort in case something fails.
set -e

subdir_configure_args=''
makemake_args=''
do_config=''
do_build=''
do_install=''
argcount=0

prev=''
passnext=''
for arg # in "$@"
do
  # If the previous option needs an argument, assign it.
  if test -n "$prev"; then
    # If the previous option needs an argument for subdir_configure_args, pass it.
    if test -n "$passnext"; then
      subdir_configure_args="$subdir_configure_args $arg"
      passnext=''
    else
      eval "$prev=\$arg"
    fi
    prev=''
  else

    case "$arg" in

      --build | --buil | --bui | --bu | --b)
        do_config=1
        do_build=1 ;;

      --config | --confi | --conf | --con | --co | --c)
        do_config=1 ;;

      --help | --hel | --he | --h)
        info_help
        exit 0 ;;

      --install | --instal | --insta | --inst | --ins | --in | --i)
        do_install=1 ;;

      --version | --versio | --versi | --vers | --ver | --ve | --v)
        version="CLISP version "`cat src/VERSION`
        echo $version
        exit 0 ;;

      --exec_prefix=* | --exec-prefix=* | --exec-prefi=* | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* | -exec=* | --exe=* | --ex=* | --e=* | \
      --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=* | \
      --quiet | --quie | --qui | --qu | --q | \
      --silent | --silen | --sile | --sil | --si | --s)
        subdir_configure_args="$subdir_configure_args $arg" ;;

      --exec_prefix | --exec-prefix | --exec-prefi | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- | -exec | --exe | --ex | --e | \
      --prefix | --prefi | --pref | --pre | --pr | --p)
        subdir_configure_args="$subdir_configure_args $arg"
        prev="$arg"
        passnext=yes ;;

      --with-* | --without-*)
        subdir_configure_args="$subdir_configure_args $arg"
        makemake_args="$makemake_args $arg" ;;

      --*)
        echo "$0: invalid argument $arg" 1>&2
        echo "$0: Try \`$0 --help'" 1>&2
        exit 1 ;;

      *) # Everything else counts as a positional argument
        argcount=`expr $argcount + 1`
        eval "POSARG$argcount=\$arg" ;;

    esac

  fi
done
if test -n "$prev"; then
  echo "$0: missing argument to $prev" 1>&2
  echo "$0: Try \`$0 --help'" 1>&2
  exit 1
fi

DIRNAME=$POSARG1
COMPILER=$POSARG2 # deprecated

if test -z "$do_config" -a -z "$do_build" -a -z "$do_install"; then
  # Default is --config
  do_config=1
fi

if test -n "$do_config"; then

  if test -f /coherent; then
    # The old configure files are needed for Coherent.
    v='.old'
    vcache=''
  else
    v=''
    vcache='--cache-file=../config.cache'
  fi
  subdir_configure_args="$subdir_configure_args $vcache"

  if [ "$COMPILER" != "" ] ; then
    CC="$COMPILER"
    export CC
  fi
  if [ "$DIRNAME" = "" -o "$DIRNAME" = src ] ; then
    DIRNAME=src
    chmod a+x $DIRNAME/configure$v
    if test -d $DIRNAME/readline; then
      chmod a+x $DIRNAME/readline/configure$v
    fi
    if test -d $DIRNAME/newreadline; then
      chmod a+x $DIRNAME/newreadline/configure$v
    fi
    if test -d $DIRNAME/stdwin; then
      chmod a+x $DIRNAME/stdwin/configure$v
    fi
    echo "executing $DIRNAME/configure$v ..."
    cd $DIRNAME
    ${CONFIG_SHELL-/bin/sh} configure$v
    if test -d readline; then
      echo "executing $DIRNAME/readline/configure$v ..."
      cd readline
      ${CONFIG_SHELL-/bin/sh} configure$v $subdir_configure_args
      cd ..
    fi
    if test -d newreadline; then
      echo "executing $DIRNAME/newreadline/configure$v ..."
      cd newreadline
      ${CONFIG_SHELL-/bin/sh} configure$v $subdir_configure_args
      cd ..
    fi
    if test -d stdwin; then
      echo "executing $DIRNAME/stdwin/configure$v ..."
      cd stdwin
      ${CONFIG_SHELL-/bin/sh} configure$v --with-x11 $subdir_configure_args
      cd ..
    fi
  else

    # checking how to copy files
    echo "blabla" > conftest.x
    err=`/bin/sh -c "cp -p conftest.x conftest.y 2>&1"`
    if test -z "$err"; then
      CP='cp -p'
    else
      CP='cp'
    fi
    rm -f conftest*

    if [ ! -d $DIRNAME ] ; then
      mkdir $DIRNAME
    fi
    for f in readline newreadline
    do
      if [ -d src/$f ] ; then
        if [ ! -d $DIRNAME/$f ] ; then
          mkdir $DIRNAME/$f
        fi
      fi
    done
    for f in readline/doc readline/examples \
             stdwin stdwin/H stdwin/Gen stdwin/Tools stdwin/Ports \
             stdwin/Ports/alfa stdwin/Ports/msdos stdwin/Ports/vtrm \
             stdwin/Ports/x11 stdwin/Packs stdwin/Packs/textedit stdwin/Packs/vt \
             queens
    do
      if [ -d src/$f ] ; then
        if [ ! -d $DIRNAME/$f ] ; then
          mkdir $DIRNAME/$f
        fi
        ( cd src
          for g in $f/* ; do
            if [ -f $g ] ; then
              if [ "$g" = readline/Makefile ] ; then
                $CP -i $g ../$DIRNAME/$g
              else
                rm -f ../$DIRNAME/$g
                ln $g ../$DIRNAME/$g || $CP -f $g ../$DIRNAME/$g
              fi
            fi
          done
        )
      fi
    done
    for f in configure$v unixconf.h.in makemake.in \
             readline/configure$v readline/config.h.in readline/Makefile.in \
             newreadline/configure$v
    do
      if [ -f src/$f ] ; then
        rm -f $DIRNAME/$f
        ln src/$f $DIRNAME/$f || $CP -f src/$f $DIRNAME/$f
      fi
    done
    chmod a+x $DIRNAME/configure$v
    if test -d $DIRNAME/readline; then
      chmod a+x $DIRNAME/readline/configure$v
    fi
    if test -d $DIRNAME/newreadline; then
      chmod a+x $DIRNAME/newreadline/configure$v
    fi
    if test -d $DIRNAME/stdwin; then
      chmod a+x $DIRNAME/stdwin/configure$v
    fi
    echo "executing $DIRNAME/configure$v ..."
    cd $DIRNAME
    ${CONFIG_SHELL-/bin/sh} configure$v --srcdir=../src
    if test -d readline; then
      echo "executing $DIRNAME/readline/configure$v ..."
      cd readline
      ${CONFIG_SHELL-/bin/sh} configure$v --srcdir=../../src/readline $subdir_configure_args
      cd ..
    fi
    if test -d newreadline; then
      echo "executing $DIRNAME/newreadline/configure$v ..."
      cd newreadline
      ${CONFIG_SHELL-/bin/sh} configure$v --srcdir=../../src/newreadline $subdir_configure_args
      cd ..
    fi
    if test -d stdwin; then
      echo "executing $DIRNAME/stdwin/configure$v ..."
      cd stdwin
      ${CONFIG_SHELL-/bin/sh} configure$v --srcdir=../../src/stdwin --with-x11 $subdir_configure_args
      cd ..
    fi
  fi
  cd ..

fi

if test -n "$do_build"; then

  if test -d src/readline; then
    makemake_args="$makemake_args --with-readline"
  else
    if test -d src/newreadline; then
      makemake_args="$makemake_args --with-newreadline"
    else
      makemake_args="$makemake_args --with-noreadline"
    fi
  fi

  cd $DIRNAME
  ./makemake $makemake_args > makefile
  make
  make check
  cd ..

fi

if test -n "$do_install"; then

  cd $DIRNAME
  make install
  cd ..

fi

