#! /bin/sh

# Clay depends on ocamlc, ocamlyacc, ocamllex, your endianness, cpp, g++

# By default, we look for software in
# 1) the directory given by the arguments
# 2) the current directory
# 3) your path

configure_options="$*"
bindir=''
ocamlcdir=''
ocamlyaccdir=''
ocamllexdir=''
gppdir=''

echo
echo "** Configuration summary **"
echo

ERROR=0

while : ; do
  case "$1" in
    "") break;;
    -bindir|--bindir)
        bindir=$2; shift;;
    -ocamldir|--ocamldir)
        ocamlcdir=$2; ocamlyaccdir=$2; ocamllexdir=$2; shift;;
    -ocamlcdir|--ocamlcdir)
        ocamlcdir=$2; shift;;
    -ocamlyaccdir|--ocamlyaccdir)
        ocamlyaccdir=$2; shift;;
    -ocamllexdir|--ocamllexdir)
        ocamllexdir=$2; shift;;
    -gppdir|--gppdir)
        gppdir=$2; shift;;
    *) ERROR=1; echo "Unknown option \"$1\"." 1>&2;;
  esac
  shift
done
#echo "gppdir ..........." $gppdir
# Sanity checks

case "$bindir" in
  /*) ;;
  "") ;;
   *) ERROR=1; echo "The -bindir directory must be absolute." 1>&2; exit 2;;
esac

case "$ocamlcdir" in
  /*) ;;
  "") ;;
   *) ERROR=1; echo "The -ocamlcdir directory must be absolute." 1>&2; exit 2;;
esac

case "$ocamlyaccdir" in
  /*) ;;
  "") ;;
   *) ERROR=1; echo "The -ocamlyaccdir directory must be absolute." 1>&2; exit 2;;
esac

case "$ocamllexdir" in
  /*) ;;
  "") ;;
   *) ERROR=1; echo "The -ocamllexdir directory must be absolute." 1>&2; exit 2;;
esac

case "$gppdir" in
  /*) ;;
  "") ;;
   *) ERROR=1; echo "The -gppdir directory must be absolute." 1>&2; exit 2;;
esac

# Write options to Makefile.config

echo "# generated by ./configure $configure_options" > Makefile.config

# Where to install

case "$bindir" in
  "") echo 'BINDIR=/usr/bin' >> Makefile.config
      bindir="/usr/bin";;
   *) echo "BINDIR=$bindir" >> Makefile.config;;
esac
echo "binaries.......... $bindir"

# Do we have g++ 2.95.2?
if test -f $gppdir/g++; 
  then echo "g++ used.......... $gppdir/g++";
       echo "GPP=$gppdir/g++" >> Makefile.config;
  else if sh ./config/searchpath g++;
    then  echo "g++ used.......... g++";
       echo "GPP=g++" >> Makefile.config;
    else ERROR=1; echo "g++ not found"; exit 2;
  fi
fi

if test -f $gppdir/g++;
  then ver=`$gppdir/g++ --version`;
  else ver=`g++ --version`;
fi

case $ver in
  2.95.2) echo "g++ version ...... 2.95.2" ;;
  *) ERROR=1; 
     echo "g++ version " $ver "found";
     echo "g++ 2.95.2 is required for the Omega constraint checker.";
     exit 2;;
esac

# Do we have ocamlc?
if test -f $ocamlcdir/ocamlc;
  then   echo "ocamlc used....... $ocamlcdir/ocamlc";
         echo "OCAMLC=$ocamlcdir/ocamlc" >> Makefile.config;
  else if sh ./config/searchpath ocamlc;
    then   echo "ocamlc used....... ocamlc";
        echo "OCAMLC=ocamlc" >> Makefile.config;
    else ERROR=1; echo "ocamlc not found"; exit 2;
  fi
fi

# Do we have ocamlyacc?
if test -f $ocamlyaccdir/ocamlyacc;
  then   echo "ocamlyacc used.... $ocamlyaccdir/ocamlyacc";
         echo "OCAMLYACC=$ocamlyaccdir/ocamlyacc" >> Makefile.config;
  else if sh ./config/searchpath ocamlyacc;
    then   echo "ocamlyacc used.... ocamlyacc";
           echo "OCAMLYACC=ocamlyacc" >> Makefile.config;
    else ERROR=1; echo "ocamlyacc not found"; exit 2;
  fi
fi
# Do we have ocamllex?
if test -f $ocamllexdir/ocamllex;
  then   echo "ocamllex used..... $ocamllexdir/ocamllex";
       echo "OCAMLLEX=$ocamllexdir/ocamllex" >> Makefile.config;
  else if sh ./config/searchpath ocamllex;
    then echo "ocamllex used..... ocamllex"
         echo "OCAMLLEX=ocamllex" >> Makefile.config;
    else ERROR=1; echo "ocamllex not found"; exit 2;
  fi
fi

# 32 bit or 64 bit architecture?
TEMPARCH=`uname -m`
case $TEMPARCH in
  i[3456]86)
    BITS=32
    ;;
  x86_64)
    ERROR=1; BITS=64;
    echo "Currently only 32 bit architectures are supported.";
    echo "We welcome any help getting it to compile on a 64 bit architecture.";;
  *)
    ERROR=1; BITS=0;
    echo "Currently only 32 bit architectures are supported.";
    echo "We welcome any help getting it to compile on a 64 bit architecture.";;
esac
echo "Architecture ..... $BITS bits"

# Big or little endian?
if test $BITS == 32;
then
  gcc -w -o endian config/endian.c || exit 100;
else if test $BITS == 64;
then
  gcc -w -o endian config/endian.c -DARCH_SIXETYFOUR || exit 100;
fi
fi
./endian
case $? in
  0) ERROR=1; echo "             ..... big endian";
     echo "ENDIANNESS=\"ARCH_BIGENDIAN\"" >> Makefile.config;
     echo "Currently only little endian architectures are supported.";
     echo "We welcome any help getting it to compile on a big endian architecture.";;
  1) echo "             ..... little endian";
     echo "ENDIANNESS=\"ARCH_LITTLEENDIAN\"" >> Makefile.config;;
  2) ERROR=1; echo "This architecture seems to be neither big endian nor little endian.";
     echo "Clay won't run on this architecture.";;
  *) ERROR=1; echo "Something went wrong during endianness determination.";
     echo "You'll have to figure out endianness yourself (Makefile.config ENDIANNESS=\"big\" \"little\")";;
esac

if test $ERROR==0;
then 
  echo
  echo "** Clay configuration completed successfully **";
  echo
else
  echo "Configuration errors found. See above ";
fi

