#!/bin/sh
#	configure -- prepare to compile/install hmake
#	author:   Malcolm.Wallace@cs.york.ac.uk
#       (nhc13config - March 1998)
#       (nhc98config - May 1999)
#       (configure   - Oct 1999)
#       (hacked for just hmake - December 1999)

# When incrementing this version number, don't forget to change the
# corresponding definition in the Makefile!
HMAKEVERSION="2.03 (2001-10-02)" export HMAKEVERSION

if uname >/dev/null 2>&1
then OS=`uname -s`
else OS=unknown
fi

# Note for people building binary packages of hmake:
# The variable INSTALLDIR, default /usr/local, changed with --prefix=
# gives the final destination of everything.  The configure script
# hard-wires this path into scripts when you give the --install flag.
# However, if you set the environment variable DESTDIR, the installation
# process will still hardwire your INSTALLDIR into scripts, but actually
# install the files in $DESTDIR$INSTALLDIR.  In this way, you can build
# a complete installation tree in a temporary directory, then tar it up
# as a package with all the scripts pointing to the correct final location.
INSTALLDIR=/usr/local

USER=${USER-`whoami 2>/dev/null`}
MACHINE=`script/harch`
PWD=`pwd`
case $PWD in	# path mangling required for CYGWIN
  file://?/*)   PWD=`echo $PWD | sed -e "s|^file://||" | sed -e "s|^[^/]|&:|"` ;;
  //?/*)   PWD=`echo $PWD | sed -e "s|^//||" | sed -e "s|^[^/]|&:|"` ;;
esac

# We need a working `echo' command: at least Solaris2.6 may not have it.
case `echo -n hello | wc -c | ( read n ; echo $n )` in
    5) ;;
    *) echo "The builtin 'echo' command doesn't do '-n' - emulating it."
       ${CC-gcc} -o script/echo script/echo.c
       echo () { $PWD/script/echo "$@"; } ;;
esac

BUILDWITH=
GHCSYM=

BUILDLIBDIR=$PWD/lib
BUILDBINDIR=$PWD/script
BUILDDIR=$PWD/targets

INSTALL=no
LIB=yes
BIN=yes
MAN=yes

if [ -f targets/$MACHINE/config.cache ]
then		# cached settings override defaults above
  . targets/$MACHINE/config.cache
else
  if [ ! -d targets/$MACHINE ]
  then mkdir -p targets/$MACHINE
  fi
fi

while [ "$1" != "" ]
do
  case $1 in
    +lib) LIB=yes ;;
    -lib) LIB=no ;;
    +bin) BIN=yes ;;
    -bin) BIN=no ;;
    +man) MAN=yes ;;
    -man) MAN=no ;;
    --buildwith=*)  BUILDWITH=`echo "$1" | cut -c13-` ;;
    --buildopts=*)  BUILDOPTS=`echo "$1" | cut -c13-` ;;
    --builddir=*)   BUILDDIR=`echo "$1" | cut -c12-` ;;
    --prefix=*)     INSTALLDIR=`echo "$1" | cut -c10-` ;;
    --installdir=*) INSTALLDIR=`echo "$1" | cut -c14-` ;;
    --bindir=*)  BINDIR=`echo "$1" | cut -c10-` ;;
    --libdir=*)  LIBDIR=`echo "$1" | cut -c10-` ;;
    --mandir=*)  MANDIR=`echo "$1" | cut -c10-` ;;
    --hbcdir=*)  HBCDIR=`echo "$1" | cut -c10-` ;;
    --ghcdir=*)  GHCDIR=`echo "$1" | cut -c10-` ;;
    --install)   INSTALL=yes ;;
    --config)    INSTALL=no ;;
    --help|-h)
  echo "`basename $0` options:     [default in brackets]"
  echo "  --config             Configure only (do not install) [default]"
  echo "  --install            Configure AND install now"
  echo "  --help / -h          Display these options and quit"
  echo "  --version / -v       Display versions of `basename $0` and hmake"
  echo
  echo "  --buildwith=comp     Build hmake with given compiler [detected]"
  echo "  --buildopts=flags    Give extra flags to build-compiler [none]"
 #echo
 #echo "  --hbcdir=dir         Tell hmake about hbc/LML installation in dir [detected]"
 #echo "  --ghcdir=dir         Tell hmake about ghc installation in dir [detected]"
  echo
  echo "  --builddir=dir       Build intermediate object files under dir [./targets]"
  echo "  --prefix=rootdir     |"
  echo "  --installdir=rootdir | Use rootdir as base for installation [/usr/local]"
  echo "  --bindir=dir         Install scripts in dir [rootdir/bin]"
  echo "  --libdir=dir         Install libraries in dir [rootdir/lib/hmake]"
  echo "  --mandir=dir         Install man pages in dir [rootdir/man/man1]"
  echo
  echo "  [+/-]bin             Do/don't (re-)install scripts [+bin]"
  echo "  [+/-]lib             Do/don't (re-)install executables [+lib]"
  echo "  [+/-]man             Do/don't (re-)install man pages [+man]"
            exit 0 ;;
    --version|-v) echo "`basename $0`: $CONFIGVERSION"
                  echo "hmake:     $HMAKEVERSION"
                  echo '  [' $INSTALLINFO ']'
                  exit 0 ;;
    *) echo "`basename $0`: unrecognised option $1"
       echo '    (use --help for option information)'
       exit 1 ;;
  esac
  shift
done

echo Configuring for hmake... '[' $CONFIGVERSION ']'
if [ -f targets/$MACHINE/config.cache ]
then
  echo "Starting with earlier config in targets/$MACHINE/config.cache"
  echo '[' $INSTALLINFO ']'
  echo '  (but cmdline options have precedence)'
fi

INSTALLINFO="config: $MACHINE/$BUILDWITH by $USER@`uname -n` on `date | cut -c9-10` `date | cut -c5-8,25-`"

LIBDIR=${LIBDIR-$INSTALLDIR/lib/hmake}
BINDIR=${BINDIR-$INSTALLDIR/bin}
MANDIR=${MANDIR-$INSTALLDIR/man/man1}
#HBCDIR=${HBCDIR}
#GHCDIR=${GHCDIR}

case $OS in
  CYGWIN*) EXE=.exe ;;
  *)       EXE=  ;;
esac
#case $OS in
#  SunOS)  LIBCOMPAT=" -lsocket"  ;;
#  NetBSD) LIBCOMPAT=" -lcompat"  ;;
#  *)      LIBCOMPAT=  ;;
#esac

# Configure hmake
if [ -d src ]	# Delay this step if it is a binary (src-less) distribution.
then
  echo "--------"
  echo Configuring known compilers... '[' $HMAKEVERSION ']'
  if [ -f targets/$MACHINE/hmake.config ]
  then
    echo "  Compilers have already been detected once.  (Remove file"
    echo "    targets/$MACHINE/hmake.config"
    echo "    and run `basename $0` again if you want to re-do that step.)"
  # echo "  hbc/LML directory:    $HBCDIR"
  # echo "  ghc directory:        $GHCDIR"
  # echo
  else
    script/confhc $BUILDWITH
  fi
  echo "--------"
fi

echo
echo "                   Configuration report for hmake."

if [ "$INSTALL" = "no" ]
then
  echo "    (You can re-run `basename $0` to change settings before proceeding.)"
  echo "You wish (eventually) to install the following components in these locations:"
  echo "    (Installation directories are not created/checked at this stage.)"
  echo "Final install root:   $INSTALLDIR"
  if [ "$LIB" = "yes" ]
  then
    echo "hmake binaries:       $LIBDIR/$MACHINE"
  else
    echo "Executables and libs: (none)"
  fi
  if [ "$BIN" = "yes" ]
  then
    echo "Scripts:              $BINDIR"
  else
    echo "Scripts:              (none)"
  fi
  if [ "$MAN" = "yes" ]
  then
    echo "Man pages:            $MANDIR"
  else
    echo "Man pages:            (none)"
  fi

  if [ -d src ]	  # Don't bother with these if unpacking a binary distribution
  then
    echo
    echo "Now we check/create your build directories:"
    echo "Config directory:      targets/$MACHINE"
    echo -n "Build directory root:  $BUILDDIR"
    if [ ! -d $BUILDDIR ]
    then mkdir -p $BUILDDIR;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo -n "Object files build in: $BUILDDIR/$MACHINE"
    if [ ! -d $BUILDDIR/$MACHINE ]
    then mkdir -p $BUILDDIR/$MACHINE;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo -n "Executables and libs:  $BUILDLIBDIR/$MACHINE"
    if [ ! -d $BUILDLIBDIR/$MACHINE ]
    then mkdir -p $BUILDLIBDIR/$MACHINE;  echo ' (created)'
    else echo ' (exists)'
    fi
  else
    echo
    echo "Checking your binary distribution for integrity:"
    echo -n "Config directory:      targets/$MACHINE"
    if [ ! -d targets/$MACHINE ]
    then mkdir -p targets/$MACHINE;  echo ' (created ok)'
    else echo ' (exists)'
    fi
    echo -n "Executables and libs:  $BUILDLIBDIR/$MACHINE"
    if [ ! -d $BUILDLIBDIR/$MACHINE ]
    then echo ' (***MISSING!)'
         echo 'Help!  Have you got the right executables for your machine?'
         exit 1
    else echo ' (ok)'
    fi
  fi
fi

echo

# The following tests are unnecessary in a binary (src-less) distribution.
if [ -d src ]
then
  if [ "$BUILDWITH" = "" ]
  then
    BUILDWITH=`grep "^BUILTBY=" targets/$MACHINE/hmake.config | cut -c9-`
    echo "I have guessed you will build hmake with:   $BUILDWITH"
  else
    echo "You are going to build hmake with:          $BUILDWITH"
  fi
  GHCSYM=`grep "^ghcsym=" targets/$MACHINE/hmake.config | cut -c8-10`
  if [ "$BUILDOPTS" != "" ]
  then echo "You gave the following extra build options: $BUILDOPTS"
  fi

  echo -n "Testing for the curses library:             "
  if [ "$CURSES" = "" ]
  then
    cat >curses.c <<!!!
#include <curses.h>
void test (void) { mvaddstr(1,2,"hello world"); }
int main (void) { test(); }
!!!
    if $CCC -o curses curses.c >/dev/null 2>/dev/null
    then CURSES=""
    else
      if $CCC -o curses curses.c -lncurses >/dev/null 2>/dev/null
      then CURSES="-lncurses"
      else
        if $CCC -o curses curses.c -lcurses >/dev/null 2>/dev/null
        then CURSES="-lcurses"
        else
          if $CCC -o curses curses.c -lcurses -ltermcap >/dev/null 2>/dev/null
          then CURSES="-lcurses -ltermcap"
          else CURSES="-ltermcap -lcurses"     # don't know a good default?
          fi
        fi
      fi
    fi
    rm -f curses curses.c
    echo -n "$CURSES "
    echo '(detected) '
  else
    echo -n "$CURSES "
    echo '(cached) '
  fi

  echo -n "Testing for the readline library: "
  echo 'main(){readline();}' >rd.c
  if ${CC-gcc} -o rd.out rd.c -lreadline >/dev/null 2>&1
  then echo '          -lreadline (detected)'
       READLINE="-DUSE_READLINE=1 -lreadline"
  else if ${CC-gcc} -o rd.out rd.c -lreadline $CURSES >/dev/null 2>&1
    then echo '          -lreadline $CURSES (detected)'
       READLINE="-DUSE_READLINE=1 -lreadline $CURSES"
    else echo '          no  (detected)'
       READLINE=""
    fi
  fi
  rm -f rd.c rd.out

  if [ "$EXE" = "" ]
  then echo "Executables need .exe suffix?               no  (detected)"
  else echo "Executables need .exe suffix?               yes (detected)"
  fi

else	# do the following only in binary distributions
  if [ -f lib/$MACHINE/config ]
  then
    . lib/$MACHINE/config
    echo "Your binary distribution of hmake was built by $BUILDWITH."
  else
    echo "ERROR: Your distribution of hmake has no sources and no executables!"
    exit 1
  fi
  echo "--------"
  echo Configuring known compilers... '[' $HMAKEVERSION ']'
  if [ -f targets/$MACHINE/hmake.config ]
  then
    echo "  Compilers have already been detected once.  (Remove file"
    echo "    targets/$MACHINE/hmake.config"
    echo "    and run `basename $0` again if you want to re-do that step.)"
  else
    script/confhc $BUILDWITH
  fi
  echo "--------"
fi


echo


if [ "$INSTALL" = "no" ]
then
  if [ -d src ]	  # Don't overwrite config if unpacking binary distribution!
  then
    echo "Adding Makefile config script to $BUILDLIBDIR/$MACHINE..."
    ( echo "BUILDWITH=$BUILDWITH";
      echo "BUILDOPTS=\"$BUILDOPTS\"";
      echo "INSTALLVER=\"$CONFIGVERSION\"";
      echo "INSTALLINFO=\"$INSTALLINFO\"";
      echo "BUILDBASEDIR=$BUILDDIR";
      echo "READLINE=\"$READLINE\"";
      echo "EXE=$EXE";
      echo "GHCSYM=$GHCSYM";
    ) >$BUILDLIBDIR/$MACHINE/config

    echo "Updating hmake config script in $BUILDLIBDIR/$MACHINE/hmake.config..."
    sed -e "s|InstallVer|$HMAKEVERSION|" targets/$MACHINE/hmake.config >$BUILDLIBDIR/$MACHINE/hmake.config
    echo "Adding build scripts for hmake and hi to $BUILDBINDIR..."
    sed -e "s|ExecutableDir|$BUILDLIBDIR|" script/hmake.inst |\
    sed -e "s|ScriptDir|$BUILDBINDIR|"   >$BUILDBINDIR/hmake
    sed -e "s|ExecutableDir|$BUILDLIBDIR|" script/hi.inst |\
    sed -e "s|ScriptDir|$BUILDBINDIR|"   >$BUILDBINDIR/hi
    chmod +x $BUILDBINDIR/hmake $BUILDBINDIR/hi
  fi

else	# [ "$INSTALL" = "yes" ]

  echo "Installation directories are now created/checked."
  echo "Install directory root is:"
  echo "    $DESTDIR$INSTALLDIR"

  if [ "$LIB" = "yes" ]
  then
    echo "Executables and object lib files go into:"
    echo -n "    $DESTDIR$LIBDIR/$MACHINE"
    if [ ! -d $DESTDIR$LIBDIR/$MACHINE ]
    then mkdir -p $DESTDIR$LIBDIR/$MACHINE;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo -n "    "
    #cp -p $BUILDLIBDIR/$MACHINE/ tar cf - . ) | ( cd $DESTDIR$LIBDIR/$MACHINE; tar xvf - )
    for file in $BUILDLIBDIR/$MACHINE/*
    do
      echo -n "`basename $file` "
      if [ -f $file ]; then cp -p $file $DESTDIR$LIBDIR/$MACHINE; fi
    done
    #echo -n "hmake.config "
    #sed -e "s|InstallVer|$HMAKEVERSION|" targets/$MACHINE/hmake.config \
    #                                     >$LIBDIR/$MACHINE/hmake.config
    echo
  else
    echo 'Not (re-)installing executables and library files'
  fi

  if [ "$BIN" = "yes" ]
  then 
    echo "Scripts go into:"
    echo -n "    $DESTDIR$BINDIR"
    if [ ! -d $DESTDIR$BINDIR ]
    then mkdir -p $DESTDIR$BINDIR;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo -n "hmake "
    sed -e "s|ExecutableDir|$LIBDIR|" script/hmake.inst |\
    sed -e "s|ScriptDir|$BINDIR|"  >$DESTDIR$BINDIR/hmake
    echo -n "hmake.config "
    sed -e "s|InstallVer|$HMAKEVERSION|" targets/$MACHINE/hmake.config >$DESTDIR$LIBDIR/$MACHINE/hmake.config
    echo -n "hi "
    sed -e "s|ExecutableDir|$LIBDIR|" script/hi.inst |\
    sed -e "s|ScriptDir|$BINDIR|"  >$DESTDIR$BINDIR/hi
    echo -n "harch "
    cp script/harch $DESTDIR$BINDIR
    echo
    chmod +x $DESTDIR$BINDIR/hmake $DESTDIR$BINDIR/harch $DESTDIR$BINDIR/hi
  else
    echo 'Not (re-)installing scripts'
  fi

  if [ "$MAN" = "yes" ]
  then
    echo "Man pages go into:"
    echo -n "    $DESTDIR$MANDIR"
    if [ ! -d $DESTDIR$MANDIR ]
    then mkdir -p $DESTDIR$MANDIR;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo -n "    "
    for file in man/*
    do
      echo -n "`basename $file` "
      cp $file $DESTDIR$MANDIR
    done
    echo
  else
    echo 'Not (re-)installing man pages'
  fi

fi

echo "Saving current configuration in targets/$MACHINE/config.cache"
( echo "INSTALLDIR=$INSTALLDIR" ;
  if [ "$LIBDIR" != "$INSTALLDIR/lib/hmake" ]; then echo "LIBDIR=$LIBDIR" ; fi;
  if [ "$MANDIR" != "$INSTALLDIR/man/man1" ]; then echo "MANDIR=$MANDIR" ; fi;
  if [ "$BINDIR" != "$INSTALLDIR/bin" ]; then echo "BINDIR=$BINDIR" ; fi;
  if [ "$HBCDIR" != "" ]; then echo "HBCDIR=$HBCDIR" ; fi;
  if [ "$GHCDIR" != "" ]; then echo "GHCDIR=$GHCDIR" ; fi;
  echo "BUILDWITH=$BUILDWITH" ;
  echo "BUILDOPTS=\"$BUILDOPTS\"" ;
  echo "BUILDDIR=$BUILDDIR" ;
  echo "LIB=$LIB" ;
  echo "BIN=$BIN" ;
  echo "MAN=$MAN" ;
  echo "EXE=$EXE" ;
  echo "INSTALLVER=\"$CONFIGVERSION\"" ;
  echo "INSTALLINFO=\"$INSTALLINFO\"" ;
) >targets/$MACHINE/config.cache

echo "Done."
if [ "$INSTALL" = "yes" ]
then
  echo
  echo "    Please ensure $BINDIR is in your PATH variable."
fi
echo
exit 0
