#!/bin/sh
#	confhc -- detect installed Haskell compilers and versions
#	author:   Malcolm.Wallace@cs.york.ac.uk, Sept 1999
#			added "hmake interactive" config, May 2000
#			removed support for nhc13, Nov 2001
#			simplified for hmake3, Jan 2002
#			modified for hat, June 2002

MACHINE=${MACHINE-`script/harch`}
PWD=`pwd`

echo "Looking for already-installed Haskell compilers:"
# Assume that we start out with a blank config.
NHCKNOWN=
HMAKEKNOWN=

# We need a working BSD-style `echo' command: at least Solaris may not have it.
#   (The calling script must have already compiled our emulated 'echo'.)
case `echo -n hello | wc -c | ( read n ; echo $n )` in
    5) ;;
    *) 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.
#if which which >/dev/null 2>&1  && ( which which | grep -v Warning >/dev/null )
#then
#  echo -n ""
#else
#  echo "No builtin 'which' command - attempting to emulate it."
  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 )
  }
#fi


# Not interested in HBC...
# ...
  
# First look for GHC.  Determining version number here is due to Simon Marlow.
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/'`
  echo $GHCVERSION | tr "." " " | ( read x y z; echo $x$y; ) \
						 >targets/$MACHINE/ghcsym;
fi
if [ "$GHCKNOWN" = "" ]
then
  echo "(not found)"
else
  echo "found ${GHCVERSION}"
fi
  
# There may be another version of ghc to look for.
COMP=$1
if [ "`echo $COMP | cut -c1-3`" = "ghc" ]
then
  VER=`echo $COMP | cut -c5-`
  if [ -n "$VER" -a "$VER" != "$GHCVERSION" ]
  then
    echo -n "  Looking for $COMP...   "
    if which $COMP >/dev/null 2>&1
    then
      GHC2KNOWN=`which $COMP`
      GHC2VERSION=`${GHC2KNOWN} --version 2>&1 | sed 's/^.*version[ ]*\([0-9.]*\).*/\1/'`
      echo $GHC2VERSION | tr "." " " | ( read x y z; echo $x$y; ) \
                                                 >targets/$MACHINE/ghcsym;
    fi
    if [ "$GHC2KNOWN" = "" ]
    then  echo "(not found)"
    else  echo "found ${GHC2VERSION}"
    fi
  fi
fi

if [ -n "$GHCKNOWN" ]
then if [ `cat targets/$MACHINE/ghcsym` -lt "504" ]
  then echo "  WARNING: hat-2.02 needs a version of ghc >= 5.04."
       GHCKNOWN=
  fi
fi


# Now, check for an installation of nhc98.
echo -n "  Looking for nhc98... "
if which nhc98 >/dev/null 2>&1
then
  NHCKNOWN=`which nhc98`
  NHCVERSION=`${NHCKNOWN} --version | head -1 | cut -d' ' -f2`
fi
if [ "$NHCKNOWN" != "" ]
then
  echo "found ${NHCVERSION}"
  if [ `echo $NHCVERSION | tr ".va" " " | ( read x y z; echo $x$y; )` -lt "115" ]
  then echo "  WARNING: hat-2.02 needs a version of nhc98 >= 1.16."
       NHCKNOWN=
  fi
else
  echo "(not found)"
fi
  

# Finally, check for an installation of hmake.
echo -n "  Looking for hmake... "
if which hmake >/dev/null 2>&1
then
  HMAKEKNOWN=`which hmake`
  HMAKEVERSION=`${HMAKEKNOWN} --version | head -1 | cut -d' ' -f2 | cut -c1-4`
  HMAKENUM=`echo ${HMAKEVERSION} | tr "." " " | ( read x y; echo $x$y; )`
fi
if [ "$HMAKEKNOWN" != "" ]
then
  echo "found ${HMAKEVERSION}"
  if [ "$HMAKENUM" -lt "305" ]
  then echo "  ERROR: hat needs a version of hmake >= 3.05."
       exit 1
  fi
else
  echo "(not found)"
fi

if [ -z "$GHCKNOWN" -a -z "$NHCKNOWN" ]
then echo "ERROR: hat needs at least one of ghc or nhc98."
     exit 1
fi

if [ "$1" = "" ]
then
  BUILDHMAKE=
  echo -n "I am guessing that you want to use "
  # in order of preference: ghc, then nhc98.
  if [ "$GHCKNOWN" = "" ];
  then
    echo -n nhc98
    BUILDHMAKE=nhc98
  else
    echo -n "ghc (not nhc98)"
    BUILDHMAKE=ghc
  fi
else
  BUILDHMAKE=$1
  echo -n "You said you want to use $BUILDHMAKE"
fi
echo " to build hat."

# ---- ----
export GHCKNOWN NHCKNOWN HMAKEKNOWN
echo $BUILDHMAKE >targets/$MACHINE/buildwith

echo "Done."
exit 0

