#!/bin/sh
# configure script for LoopTools
# last modified 16 Jul 02 by Thomas Hahn
# note: has nothing to do with GNU autoconf


test=test$$

trap "rm -f $test*" 0 1 2 15

if (echo "test\c"; echo 1,2,3) | grep c > /dev/null ; then
  if (echo -n test; echo 1,2,3) | grep n >/dev/null ; then
    echo_n=
    echo_c='
'
  else
    echo_n=-n
    echo_c=
  fi
else
  echo_n=
  echo_c='\c'
fi


findprog()
{
  echo $echo_n "looking for $1... $echo_c" 1>&2
  shift
  for prog in $* ; do
    full=`csh -cf "which $prog"`
    if [ -x "$full" ] ; then
      echo $full 1>&2
      basename $full
      return 0
    fi
  done
  echo "no $* in your path" 1>&2
  return 1
}


CONF_PREFIX=`expr "$*" : ".*--prefix=\(.*\)"`
[ -z "$CONF_PREFIX" ] && CONF_PREFIX=`tcsh -c 'echo $HOSTTYPE'`


## look for some programs

CONF_MAKE=`findprog make gmake Make make` || exit 1

CONF_CC=`findprog gcc $CC gcc` || exit 1

CONF_CFLAGS=${CFLAGS--O}

CONF_MCCTARGETS=LoopTools.exe
CONF_MCC=`findprog mcc mcc mprep`
if [ $? -eq 1 ] ; then
  echo "Disabling compilation of the MathLink executable."
  echo "Note: if you have Mathematica installed properly, this should not"
  echo "      occur; if you want to work with LoopTools in Mathematica,"
  echo "      check the installation of mcc (setting of paths, etc.) and"
  echo "      then run $0 again."
  CONF_MCCTARGETS=""
fi
## JGM -- needed to add this to correct an error of non-MSDOS8.3 names in Mathematica path
CONF_MCC="mprep.exe "
CONF_MCCFLAGS="-o "

case $HOSTTYPE in
*sun*) CONF_MCCFLAGS="$CONF_MCCFLAGS -lnsl -lsocket" ;;
esac

CONF_FC=`findprog f77 $FC pgf77 xlf fort77 f77 f90 g77` || exit 1

CONF_FFLAGS=${FFLAGS--O}


## find the Fortran libraries

echo $echo_n "extracting the Fortran libraries... $echo_c" 1>&2
cat > $test.f << _EOF_
	program dontpanic
	print *, "Hi"
	end
_EOF_
CONF_LIBS=""
verbose=`$CONF_FC -v -o $test $test.f 2>&1`
case "$verbose" in
  *g77*) CONF_FFLAGS="-O0" ;;
esac
for ldflag in `echo "$verbose" | grep -i ld | tr ",:" "  "` ; do
  case $ldflag in
  *.o | *ld | *collect2)	;;
  -l* | -L* | *.a)		CONF_LIBS="$CONF_LIBS $ldflag" ;;
  /*)				CONF_LIBS="$CONF_LIBS -L$ldflag" ;;
  esac
done
echo $CONF_LIBS 1>&2


## does f77 recognize preprocessor statements?

echo $echo_n "does $CONF_FC understand preprocessor directives... $echo_c" 1>&2
cat > $test.F << _EOF_
#define SAY_HI "Hi"
	program dontpanic
	print *, SAY_HI
	end
_EOF_
rm -f $test.exe
$CONF_FC $test.F -o $test.exe > /dev/null 2>&1
if [ -x $test.exe ] ; then
  echo "yes" 1>&2
  CONF_FFC="$CONF_FC"
else
  echo "no" 1>&2
  CONF_FFC="utils/F77"
fi


## does f77 support REAL*16?

echo $echo_n "does $CONF_FC support REAL*16... $echo_c" 1>&2
cat > $test.f << _EOF_
	program test
	real*16 r
	r = qext(1d0)
	end
_EOF_
rm -f $test.exe
$CONF_FC $test.f -o $test.exe > /dev/null 2>&1
if [ -x $test.exe ] ; then
  echo "yes" 1>&2
else
  echo "no" 1>&2
  CONF_FFLAGS="$CONF_FFLAGS -DQEXT=DBLE"
fi


## does Fortran append underscores to symbols?

echo $echo_n "does $CONF_FC append underscores... $echo_c" 1>&2
cat > $test.f << _EOF_
	subroutine uscore
	print *, "hi"
	end
_EOF_
echo "extern void uscore(); main() { uscore(); }" > ${test}a.c
$CONF_FC -c $test.f > /dev/null 2>&1
rm -f $test.exe
$CONF_CC ${test}a.c $test.o -o $test.exe > /dev/null 2>&1
if [ -x $test.exe ] ; then
  echo "no" 1>&2
  sed -e 's/_$//g' -e 's/base_/base/g' include/ltproto.h.in > include/ltproto.h
else
  echo "yes" 1>&2
  cp include/ltproto.h.in include/ltproto.h
  CONF_CFLAGS="$CONF_CFLAGS -DHAVE_UNDERSCORE"
fi

echo "creating makefile" 1>&2

cat - makefile.in > makefile << _EOF_
# --- variables defined by configure ---
CC = $CONF_CC
CFLAGS = $CONF_CFLAGS -L./ -looptools -Wl,-Lc:/cygwin/lib -lml32i2b -lgdi32 -Lc:/cygwin/lib/gcc-lib/i686-pc-cygwin/2.95.3-5 -lg2c
FC = $CONF_FC
FFC = $CONF_FFC
FFLAGS = $CONF_FFLAGS
MCC = $CONF_MCC
MCCFLAGS = $CONF_MCCFLAGS
MCCTARGETS = $CONF_MCCTARGETS
PREFIX = $CONF_PREFIX
# --- end defs by configure ---

_EOF_

sed "s:f77:$CONF_FC:g" utils/F77.in > utils/F77
chmod +x utils/F77

sed "s:^gcc.*:&$CONF_LIBS:g" utils/ccf.in > utils/ccf
chmod +x utils/ccf


echo "" 1>&2
echo "now you must run $CONF_MAKE" 1>&2
echo "" 1>&2

exit 0

