#!/bin/sh
# configure script for LoopTools
# last modified 27 Jul 01 by Thomas Hahn
# note: has _nothing_ to do with GNU autoconf


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_FFLAGS="-O"

CONF_CFLAGS="-O"

CONF_MCCFLAGS="-O"
echo "$HOSTTYPE" | grep sun > /dev/null && \
  CONF_MCCFLAGS="$CONF_MCCFLAGS -lnsl -lsocket"

CONF_MCCTARGETS="LoopTools"


## look for some programs...

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

CONF_AWK=`findprog awk mawk gawk nawk awk` || exit 1

CONF_GCC=`findprog gcc gcc` || exit 1

if [ "$1" = "-g77" ] ; then
  CONF_F77=`findprog f77 g77`
else
  CONF_F77=`findprog f77 f77 fort77 xlf g77 f90 pgf77`
fi
if [ $? -eq 1 ] ; then
  echo $echo_n "  then how about f2c... $echo_c" 1>&2
  CONF_F77=`csh -cf "which f2c"`
  if [ ! -x "$CONF_F77" ] ; then 
    echo "no Fortran-77 compiler in your path." 1>&2
    exit 1
  fi
  echo $CONF_F77 1>&2
  CONF_F77=utils/f77c
fi
sed "s:f77:$CONF_F77:g" utils/F77.in > utils/F77
chmod +x utils/F77

[ "$CONF_F77" = "g77" ] && CONF_FFLAGS=""

CONF_MCC=`findprog mcc mcc`
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


## does f77 recognize preprocessor statements?

echo $echo_n "does $CONF_F77 understand preprocessor directives... $echo_c" 1>&2
cat > test$$.F << _EOF_
#define SAY_HI "Hi"
	program dontpanic
	print *, SAY_HI
	end
_EOF_
$CONF_F77 -c test$$.F 2>&1 | egrep -i \
  "unimplemented|unsupported|invalid|warning|incompatible" > /dev/null
if [ $? -eq 1 ] ; then
  echo "yes" 1>&2
  CONF_FF77="$CONF_F77"
else
  echo "no" 1>&2
  CONF_FF77="utils/F77"
fi


## does f77 support REAL*16?

echo $echo_n "does $CONF_F77 support REAL*16... $echo_c" 1>&2
cat > test$$.f << _EOF_
	program test
	real*16 r
	r = qext(1d0)
	end
_EOF_
$CONF_F77 -c test$$.f 2>&1 | egrep -i \
  "unimplemented|unsupported|invalid|warning|incompatible" > /dev/null
if [ $? -eq 1 ] ; 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_F77 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_F77 -c test$$.f > /dev/null 2>&1
$CONF_GCC test$$a.c test$$.o 2>&1 | grep -i uscore > /dev/null
if [ $? -eq 0 ] ; then
  echo "yes" 1>&2
  cp include/ltproto.h.in include/ltproto.h
  CONF_CFLAGS="$CONF_CFLAGS -DHAVE_UNDERSCORE"
else
  echo "no" 1>&2
  sed -e 's/_$//g' -e 's/base_/base/g' \
    include/ltproto.h.in > include/ltproto.h
fi


## 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_F77 -v test$$.f -o test$$ > test$$.ld 2>&1
CONF_LIBS=`$CONF_AWK 'BEGIN { libs = "" }
/ld|LD/ {
  gsub("[:,]", " ");
  n = split($0, lp, " ");
  for(i = 1; i <= n; ++i)
    if(lp[i] ~ /^-l|^-L|\.a$/) libs = libs " " lp[i];
    else if(lp[i] ~ /^\// && lp[i] !~ /\.o$|ld$/)
      libs = libs " -L" lp[i];
}
END { print libs }' test$$.ld`
echo $CONF_LIBS 1>&2
sed "s:^gcc.*:&$CONF_LIBS:g" utils/ccf.in > utils/ccf
chmod +x utils/ccf


echo "creating makefile" 1>&2

cat - makefile.in > makefile << _EOF_
# --- variables defined by configure ---
CC = $CONF_GCC
CFLAGS = $CONF_CFLAGS
FC = $CONF_F77
FFC = $CONF_FF77
FFLAGS = $CONF_FFLAGS
MCC = $CONF_MCC
MCCFLAGS = $CONF_MCCFLAGS
MCCTARGETS = $CONF_MCCTARGETS
# --- end defs by configure ---

_EOF_

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

exit 0

