#!/bin/sh
#       configure -- prepare to compile/install the HaXml package
#       author:   Malcolm.Wallace@cs.york.ac.uk, August 2002

VERSION="1.13"

if [ -f obj/prefix ]
then INSTALLDIR=`cat obj/prefix`
else INSTALLDIR=/usr/local/bin
fi

# We need a working `echo' command: at least Solaris2.6 may not have it.
CCC=${CC-gcc}
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

# We need a working `which' command: CYGWIN at least doesn't have it,
#  and some installed 'which's behave badly, e.g. Solaris, OSF/1.
which () {
  ( for path in `echo \"$PATH\" | sed -e 's/:/\" \"/g'`
    do
      thefile=`echo $path | tr -d "\""`/$1
      if [ -f "$thefile" -a -x "$thefile" ]
      then echo $thefile
           exit 0
      fi
    done; exit 1 )
}

DARWIN=false
CYGWIN=false
MINGW=false
case `uname -s` in
  CYGWIN*) CYGWIN=true;;
  MINGW*)  MINGW=true ;;
  Darwin*) DARWIN=true ;;
esac

# Process command-line arguments
while [ "$1" != "" ]
do
  case $1 in
    --buildwith=*)  BUILDWITH=`echo "$1" | cut -c13-` ;;
    --buildopts=*)  BUILDOPTS=$BUILDOPTS" "`echo "$1" | cut -c13-` ;;
    --prefix=*)     INSTALLDIR=`echo "$1" | cut -c10-` ;;
    --help|-h)
  echo "`basename $0` options:     [default in brackets]"
  echo "  --buildwith=comp     Build HaXml for the given compiler [detected]"
  echo "  --buildopts=flags    Give extra flags needed by your build compiler [none]"
  echo "  --prefix=bindir      Installed executables go in bindir [/usr/local/bin]"
            exit 0 ;;
    --version|-v) echo "HaXml:     $VERSION"
                  exit 0 ;;
    *) echo "`basename $0`: unrecognised option $1"
       echo '    (use --help for option information)'
       exit 1 ;;
  esac
  shift
done

echo "Looking for Haskell compilers:"
# Assume that we start out with a blank config.
HMAKEKNOWN=
GHCKNOWN=
NHCKNOWN=
HUGSKNOWN=
HMAKENUM=0
GHCNUM=0
NHCNUM=0
HUGSNUM=0

echo -n "  Looking for hmake... "
if which hmake >/dev/null 2>&1
then
  HMAKEKNOWN=`which hmake`
  HMAKEVERSION=`${HMAKEKNOWN} --version | head -1 | cut -d' ' -f2`
  HMAKENUM=`echo $HMAKEVERSION | tr "v." "  " | ( read x y z; echo $x$y )`
fi
if [ "$HMAKEKNOWN" != "" ]
then echo "found ${HMAKEVERSION}"
else echo "(not found)"
fi

echo -n "  Looking for ghc...   "
if which ghc >/dev/null 2>&1
then
  GHCKNOWN=`which ghc`
  GHCVERSION=`${GHCKNOWN} --version 2>&1 | sed 's/^.*version[ ]*\([0-9.]*\).*/\1/'`
  GHCNUM=`echo $GHCVERSION | tr "v." " " | ( read x y z; echo $x$y; )`
  if [ "$GHCNUM" -lt "100" ]
  then GHCNUM=${GHCNUM}0
  fi
fi
if [ "$GHCKNOWN" = "" ]
then echo "(not found)"
else echo "found ${GHCVERSION}"
     if [ "$GHCNUM" -lt "502" ]
     then  echo "    Warning: HaXml needs ghc-5.02 or later.  Ignoring ghc."
           GHCKNOWN=
     else  GHCLIBDIR=`${GHCKNOWN} -v 2>&1 | grep ' package config file: ' | head -1 | sed 's/.* package config file: //'`
           if $CYGWIN; then GHCLIBDIR=`cygpath -u $GHCLIBDIR`; fi
           if $MINGW; then GHCLIBDIR=`echo $GHCLIBDIR | tr '\\\\' '/'`; fi
           GHCLIBDIR=`dirname $GHCLIBDIR`
           GHCINCDIR=$GHCLIBDIR/imports
     fi
fi

# There may be another version of ghc to look for.
if [ -n "$BUILDWITH" ]
then
if [ "`basename $BUILDWITH | cut -c1-3`" = "ghc" ]
then
  VER=`basename $BUILDWITH | cut -c5-`
  if [ -n "$VER" -a "$VER" != "$GHCVERSION" ]
  then
    echo -n "  Looking for $BUILDWITH...   "
    if which $BUILDWITH >/dev/null 2>&1
    then
      GHC2KNOWN=`which $BUILDWITH`
      GHC2VERSION=`${GHC2KNOWN} --version 2>&1 | sed 's/^.*version[ ]*\([0-9.]*\).*/\1/'`
      GHC2NUM=`echo $GHC2VERSION | tr "." " " | ( read x y z; echo $x$y; )`
      if [ "$GHC2NUM" -lt "100" ]
      then GHC2NUM=${GHC2NUM}0
      fi
    fi
    if [ "$GHC2KNOWN" = "" ]
    then  echo "(not found)"
    else  echo "found ${GHC2VERSION}"
          if [ "$GHC2NUM" -lt "502" ]
          then echo "    Warning: HaXml needs ghc-5.02 or later.  Ignoring."
               GHC2KNOWN=
          else GHCKNOWN="$GHC2KNOWN"
               GHCVERSION="$GHC2VERSION"
               GHCNUM="$GHC2NUM"
               GHCLIBDIR=`${GHCKNOWN} -v 2>&1 | grep ' package config file: ' | head -1 | sed 's/.* package config file: //'`
               if $CYGWIN; then GHCLIBDIR=`cygpath -u $GHCLIBDIR`; fi
               if $MINGW; then GHCLIBDIR=`echo $GHCLIBDIR | tr '\\\\' '/'`; fi
               GHCLIBDIR=`dirname $GHCLIBDIR`
               GHCINCDIR=$GHCLIBDIR/imports
          fi
    fi
  fi
fi
fi

echo -n "  Looking for nhc98... "
if which nhc98 >/dev/null 2>&1
then
  NHCKNOWN=`which nhc98`
  NHCVERSION=`${NHCKNOWN} --version | head -1 | cut -d' ' -f2`
  NHCNUM=`echo $NHCVERSION | tr "v." "  " | ( read x y z; echo $x$y )`
fi
if [ "$NHCKNOWN" = "" ]
then  echo "(not found)"
else  echo "found ${NHCVERSION}"
     if [ "$HMAKENUM" -lt "306" ]
     then echo "    Warning: building with nhc98 needs hmake-3.06 or later."
          echo "    Available from:   http://www.haskell.org/hmake/"
     fi
     if [ "$NHCNUM" -lt "114" ]
     then  echo "    Warning: HaXml needs nhc98-1.14 or later.  Ignoring nhc98."
           NHCKNOWN=
     else  NHCLIBDIR=`grep '^NHC98LIBDIR' $NHCKNOWN | cut -c27- | cut -d'}' -f1 | head -1`
           NHCINCDIR=`grep '^NHC98INCDIR' $NHCKNOWN | cut -c27- | cut -d'}' -f1 | head -1`
     fi
fi

month() {
  read mon yr;
  case $mon in
    Jan*) echo 01;;
    Feb*) echo 02;;
    Mar*) echo 03;;
    Apr*) echo 04;;
    May)  echo 05;;
    Jun*) echo 06;;
    Jul*) echo 07;;
    Aug*) echo 08;;
    Sep*) echo 09;;
    Oct*) echo 10;;
    Nov*) echo 11;;
    Dec*) echo 12;;
    *)    echo 00;;
  esac;
}

echo -n "  Looking for hugs...  "
if which hugs >/dev/null 2>&1
then
  HUGSKNOWN=`which hugs`
  HUGSVERSION=`echo :q | $HUGSKNOWN 2>/dev/null | grep Version | cut -c18-32`
  HUGSNUM=`echo $HUGSVERSION | cut -d' ' -f2`
  HUGSNUM=$HUGSNUM`echo $HUGSVERSION | month`
fi
if [ "$HUGSKNOWN" = "" ]
then  echo "(not found)"
else  echo "found ${HUGSVERSION}"
  if [ "$HUGSNUM" -lt "200309" ]
  then  echo "    Warning: HaXml configuration only supports Hugs >= Sept 2003."
        HUGSKNOWN=
  fi
fi

if [ ! -z "$BUILDWITH" ]
then
  echo "  You want to build for only...  $BUILDWITH"
  if [ "`echo $BUILDWITH | cut -c1-3`" = "ghc" ]
  then
    if [ -z "$GHCKNOWN" ]
    then echo '   *** Did not find ghc - cannot continue.'
         exit 1
    else NHCKNOWN=
         HUGSKNOWN=
    fi
  else if [ "$BUILDWITH" = "nhc98" ]
  then
    if [ -z "$NHCKNOWN" ]
    then echo '   *** Did not find nhc98 - cannot continue.'
         exit 1
    else GHCKNOWN=
         HUGSKNOWN=
    fi
  else if [ "`echo $BUILDWITH | cut -c1-4`" = "hugs" ]
  then
    if [ -z "$HUGSKNOWN" ]
    then echo '   *** Did not find hugs - cannot continue.'
         exit 1
    else GHCKNOWN=
         NHCKNOWN=
    fi
  else echo '   *** I do not recognise this compiler:' $BUILDWITH
       exit 1
  fi;fi;fi;
fi

echo "  Executables install into... $INSTALLDIR"
echo "    (note: no extra /bin will be added to the end of that location)"
echo "    (      re-run ./configure --prefix=... if this wasn't your intention)"

# Prepare to build.
if [ ! -d obj ]
then mkdir obj
fi
if [ -f obj/compilers ]
then rm obj/compilers
fi
if [ "$HUGSKNOWN" != "" ]
then
  echo hugs >>obj/compilers
  if [ ! -d obj/hugs ]
  then mkdir obj/hugs
  fi
  cp -Rp src/tools src/Makefile obj/hugs
fi
if [ "$GHCKNOWN" != "" ]
then
  echo ghc >>obj/compilers
  if [ ! -d obj/ghc ]
  then mkdir obj/ghc
  fi
  echo $GHCLIBDIR >obj/ghc/ghclibdir
  echo $GHCINCDIR >obj/ghc/ghcincdir
  if $CYGWIN || $MINGW
    then cygpath -w ${GHCLIBDIR} >obj/ghc/ghclibdirraw
         cygpath -w ${GHCINCDIR}/ >obj/ghc/ghcincdirraw
    else echo ${GHCLIBDIR} >obj/ghc/ghclibdirraw
         echo ${GHCINCDIR}/ >obj/ghc/ghcincdirraw
  fi
  if ghc-pkg-$GHCVERSION -l >/dev/null
    then echo ghc-pkg-$GHCVERSION >obj/ghc/ghcpkgcmd
    else echo ghc-pkg >obj/ghc/ghcpkgcmd
  fi
  echo ${BUILDWITH-ghc} >obj/ghccmd
  cp -Rp src/Text src/tools src/Makefile src/pkg.conf obj/ghc
  if [ "$GHCNUM" -ge "504" ]
  then rm -rf obj/ghc/Text/PrettyPrint
       echo "-package base" >obj/ghc/ghcpkgs
  else echo "-package lang -package data" >obj/ghc/ghcpkgs
  fi
fi
if [ "$NHCKNOWN" != "" ]
then
  echo nhc98 >>obj/compilers
  if [ ! -d obj/nhc98 ]
  then mkdir obj/nhc98
  fi
  echo $NHCLIBDIR >obj/nhc98/nhc98libdir
  echo $NHCINCDIR >obj/nhc98/nhc98incdir
  cp -Rp src/Text src/tools src/Makefile obj/nhc98
  if [ "$NHCNUM" -ge "116" ]
  then rm -rf obj/ghc/Text/PrettyPrint
       echo "-package base" >obj/nhc98/nhc98pkgs
  else echo "" >obj/nhc98/nhc98pkgs
  fi
fi

echo $INSTALLDIR >obj/prefix
if $CYGWIN || $MINGW ; then echo .exe >obj/exe; else touch obj/exe; fi
if $CYGWIN || $MINGW ; then echo main.exe >obj/out; else echo a.out >obj/out; fi
if $DARWIN ; then echo -all_load; else echo --whole-archive; fi >obj/ldopt


echo "Ready to build.  Type 'make' then (as root) 'make install'."
exit 0
