#!/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)
#       (updated - January 2002)

# When incrementing this version number, don't forget to change the
# corresponding definition in Makefile.inc!
HMAKEVERSION="3.13 (2006-11-01)" 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 $OS in
  CYGWIN*) PWD=`cygpath -w "$PWD" | tr '\\\\' '/'`
           case $PWD in	# further 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
           READLINE=""
           EXE=.exe ;;
  *) EXE="" ;;
esac

case "$PWD" in
 *' '*)
   echo "Error: hmake will not build in a directory with spaces anywhere"
   echo "       in the full pathname."
   echo "Suggestion: move the build tree somewhere else."
   exit 1 ;;
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."
       ${CCC} -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=$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-` ;;
    --confdir=*) CONFDIR=`echo "$1" | cut -c11-` ;;
    --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 version of 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 "  --confdir=dir        Install configuration data in dir [\$libdir]"
  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 "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... '[' $HMAKEVERSION ']'
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`"

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

case $OS in
  CYGWIN*) INSTALLDIR=`cygpath -w "$INSTALLDIR" | tr '\\\\' '/'`
           BUILDDIR=`cygpath -w "$BUILDDIR" | tr '\\\\' '/'`
           LIBDIR=`cygpath -w "$LIBDIR" | tr '\\\\' '/'`
           CONFDIR=`cygpath -w "$CONFDIR" | tr '\\\\' '/'`
           BINDIR=`cygpath -w "$BINDIR" | tr '\\\\' '/'`
     ;;
  *) ;;
esac

case $MACHINE in
  *Darwin[56]) CCC=${CC-cc};;
  *)           CCC=${CC-gcc};;
esac
#case $OS in
# SunOS)  LIBCOMPAT=" -lsocket"  ;;
# NetBSD) LIBCOMPAT=" -lcompat"  ;;
# *)      LIBCOMPAT=  ;;
#esac

# Detect Haskell compilers and choose one for building with.
# (Also generates a little script for configuring hmake later.)
if [ -d src ]	# Delay this step if it is a binary (src-less) distribution.
then
  echo "--------"
  CCC=$CCC script/confhc $BUILDWITH
  echo "--------"
fi

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"
    echo "hmakerc:              $CONFDIR/$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 "Build directory root:"
    echo -n "    $BUILDDIR"
    if [ ! -d $BUILDDIR ]
    then mkdir -p $BUILDDIR;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo "Object files build in:"
    echo -n "    $BUILDDIR/$MACHINE"
    if [ ! -d $BUILDDIR/$MACHINE ]
    then mkdir -p $BUILDDIR/$MACHINE;  echo ' (created)'
    else echo ' (exists)'
    fi
    echo "Executables and libs:"
    echo -n "    $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=`cat targets/$MACHINE/buildwith`
    echo "I have guessed you will build hmake with:   $BUILDWITH"
  else
    echo "You are going to build hmake with:          $BUILDWITH"
  fi
  GHCSYM=`cat targets/$MACHINE/ghcsym || true`
  if [ "$BUILDOPTS" != "" ]
  then echo "You gave the following extra build options: $BUILDOPTS"
  fi

  if [ "$EXE" = "" ]
  then
    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.o curses.c
      echo -n "$CURSES "
      echo '(detected) '
    else
      echo -n "$CURSES "
      echo '(cached) '
    fi

    echo -n "Testing for the readline library:           "
    if [ "$READLINE" = "" ]
    then
      echo 'main(){readline();}' >rd.c
      if ${CCC} -o rd.out rd.c -lreadline >/dev/null 2>&1
      then echo '-lreadline (detected)'
           READLINE="-DUSE_READLINE=1 -lreadline"
      else if ${CCC} -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
    else
      echo -n "$READLINE "
      echo '(cached) '
    fi
    rm -f rd.c rd.out
  fi

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

  TRUE=/bin/true
  if [ ! -x $TRUE ]
  then TRUE=/usr/bin/true
       echo "Found /usr/bin/true not /bin/true"
  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
  # compiler detection deferred from earlier because of BUILDWITH variable
  echo "--------"
  script/confhc $BUILDWITH
  echo "--------"
  sh targets/$MACHINE/hmake3.config	# ...and setup an hmakerc file
fi


echo


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

    echo "Adding build scripts for hmake, hmake-config, and hi to"
    echo "    $BUILDBINDIR..."
    sed -e "s|ExecutableDir|$BUILDLIBDIR|" script/hmake.inst |\
    sed -e "s|ConfDir|$BUILDLIBDIR|" |\
    sed -e "s|InstallVer|$HMAKEVERSION|" |\
    sed -e "s|^BUILTBY=$|BUILTBY=${BUILDWITH}|" |\
    sed -e "s|ScriptDir|$BUILDBINDIR|"   >$BUILDBINDIR/hmake
    sed -e "s|ExecutableDir|$BUILDLIBDIR|" script/hmake-config.inst |\
    sed -e "s|ConfDir|$BUILDLIBDIR|" |\
    sed -e "s|ScriptDir|$BUILDBINDIR|"   >$BUILDBINDIR/hmake-config
    sed -e "s|ExecutableDir|$BUILDLIBDIR|" script/hi.inst |\
    sed -e "s|ConfDir|$BUILDLIBDIR|" |\
    sed -e "s|InstallVer|$HMAKEVERSION|" |\
    sed -e "s|ScriptDir|$BUILDBINDIR|"   >$BUILDBINDIR/hi
    chmod +x $BUILDBINDIR/hmake $BUILDBINDIR/hmake-config $BUILDBINDIR/hi

    echo "Updating targets/$MACHINE/hmake3.config..."
    echo "$BUILDBINDIR/hmake-config $BUILDLIBDIR/$MACHINE/hmakerc \
		default $BUILDWITH"	>>targets/$MACHINE/hmake3.config
  fi

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

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

  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 "    harch "
    cp script/harch $DESTDIR$BINDIR
    echo -n "hmake "
    sed -e "s|ExecutableDir|$LIBDIR|" script/hmake.inst |\
    sed -e "s|ConfDir|$CONFDIR|" |\
    sed -e "s|InstallVer|$HMAKEVERSION|" |\
    sed -e "s|^BUILTBY=$|BUILTBY=${BUILDWITH}|" |\
    sed -e "s|ScriptDir|$BINDIR|"  >$DESTDIR$BINDIR/hmake
    echo -n "hmake-config "
    sed -e "s|ExecutableDir|$LIBDIR|" script/hmake-config.inst |\
    sed -e "s|ConfDir|$CONFDIR|" |\
    sed -e "s|ScriptDir|$BINDIR|"  >$DESTDIR$BINDIR/hmake-config
    echo -n "hi "
    sed -e "s|ExecutableDir|$LIBDIR|" script/hi.inst |\
    sed -e "s|ConfDir|$CONFDIR|" |\
    sed -e "s|InstallVer|$HMAKEVERSION|" |\
    sed -e "s|ScriptDir|$BINDIR|"  >$DESTDIR$BINDIR/hi
    echo
    chmod a+rx $DESTDIR$BINDIR/hmake $DESTDIR$BINDIR/harch $DESTDIR$BINDIR/hi
    chmod a+rx $DESTDIR$BINDIR/hmake-config
  else
    echo 'Not (re-)installing scripts'
  fi

  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 "    "
    for file in $BUILDLIBDIR/$MACHINE/*
    do
      echo -n "`basename $file` "
      if [ -f $file ]; then cp -p $file $DESTDIR$LIBDIR/$MACHINE; fi
    done
    echo
    echo "Conf files go into:"
    echo -n "    $DESTDIR$CONFDIR/$MACHINE"
    if [ ! -d $DESTDIR$CONFDIR/$MACHINE ]
    then mkdir -p $DESTDIR$CONFDIR/$MACHINE;  echo ' (created)'
    else echo ' (exists)'
    fi
    ln -f $DESTDIR$LIBDIR/$MACHINE/runhs $DESTDIR$BINDIR
    mv $DESTDIR$LIBDIR/$MACHINE/hmakerc $DESTDIR$CONFDIR/$MACHINE || true
    echo
  else
    echo 'Not (re-)installing executables and library files'
  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
    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 [ "$CONFDIR" != "$LIBDIR" ]; then echo "CONFDIR=$CONFDIR" ; 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 "READLINE=\"$READLINE\"" ;
  echo "INSTALLVER=\"$HMAKEVERSION\"" ;
  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


syntax highlighted by Code2HTML, v. 0.9.1