#! /bin/sh
# configure script for LoopTools
# last modified 20 Apr 06 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=`which $prog`
    if [ -x "$full" ] ; then
      echo $full 1>&2
      echo $prog
      return 0
    fi
  done
  echo "no "$@" in your path" 1>&2
  return 1
}


ldflags()
{
  # Mma 5.1's mcc needs this for static linking
  LDFLAGS="$LDFLAGS -lpthread"

  while read cmd args ; do
    case $cmd in
    *collect2 | *ld)
      set -- `echo $args | tr ':,' '  '`
      while [ $# -gt 0 ] ; do
        case $1 in
        *.o)
          ;;
        -l* | -L* | *.a)
          LDFLAGS="$LDFLAGS $1" ;;
        /*)
          LDFLAGS="$LDFLAGS -L$1" ;;
        -rpath*)
          LDFLAGS="$LDFLAGS -Wl,$1,$2"
          shift ;;
        esac
        shift
      done
    esac
  done

  # this is supposed to fix the saveFP/restFP problem on Macs
  case $CONF_HOSTTYPE in
  *mac*) LDFLAGS=`echo $LDFLAGS | sed 's/-lgcc /-lcc_dynamic /g'` ;;
  esac

  echo $LDFLAGS
}


CONF_HOSTTYPE=`tcsh -cf 'echo $HOSTTYPE'`

CONF_PREFIX=$CONF_HOSTTYPE

for arg in "$@" ; do
  case "$arg" in
  --prefix=*) CONF_PREFIX=`expr "$arg" : ".*--prefix=\(.*\)"` ;;
  -*) echo "Unknown option $arg" 1>&2 ;;
  *=*) eval `echo $arg\" | sed 's/=/="/'` ;;
  *) echo "Unknown argument $arg" 1>&2 ;;
  esac
done


## look for some programs

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

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

CONF_CFLAGS="${CFLAGS--O3 -fomit-frame-pointer -Wall -g}"

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

if $CONF_FC -v -c 2>&1 | grep "gcc version" > /dev/null ; then
  CONF_FFLAGS="-O0 -Wall -g"
else
  CONF_FFLAGS="${FFLAGS-default}"
  [ "$CONF_FFLAGS" = default ] && case "$CONF_FC$CONF_HOSTTYPE" in
  *pgf77*)
	CONF_FFLAGS="-fast -Minform=inform -g77libs -Ktrap=fp -g" ;;
  *ifort*)
	CONF_FFLAGS="-O3 -fpe0 -g" ;;
  *alpha)
	CONF_FFLAGS="-fast -fpe0 -g3" ;;
  *sun* | *sparc*)
	CONF_FFLAGS="-fast -ftrap=common -g" ;;
  *hp*)
	CONF_FFLAGS="-O2 +U77 +FPVZO -g" ;;
  *)
	CONF_FFLAGS="-O -g" ;;
  esac
fi


## find the Fortran libraries

echo $echo_n "extracting the Fortran libraries... $echo_c" 1>&2

rm -f $test*

cat > $test.f << _EOF_
	program dontpanic
	print *, "Hi"
	end
_EOF_

CONF_LDFLAGS=`$CONF_FC $CONF_FFLAGS -v -o $test $test.f 2>&1 | ldflags`

echo $CONF_LDFLAGS 1>&2


## does f77 recognize preprocessor statements?

echo $echo_n "does $CONF_FC understand preprocessor directives... $echo_c" 1>&2

rm -f $test*

cat > $test.F << _EOF_
#define SAY_HI "Hi"
	program dontpanic
	print *, SAY_HI
	end
_EOF_

$CONF_FC $CONF_FFLAGS $test.F -o $test > /dev/null 2>&1

if [ -x $test ] ; then
  echo "yes" 1>&2
  CONF_FCPP="$CONF_FC"
else
  echo "no" 1>&2
  CONF_FCPP="tools/F77"
fi


## does f77 support REAL*16?

echo $echo_n "does $CONF_FC support REAL*16... $echo_c" 1>&2

rm -f $test*

cat > $test.f << _EOF_
	program test
	real*16 r
	r = qext(1d0)
	end
_EOF_

$CONF_FC $CONF_FFLAGS $test.f -o $test > /dev/null 2>&1

if [ -x $test ] ; then
  echo "yes" 1>&2
  CONF_FFLAGS="$CONF_FFLAGS -DQUAD"
else
  echo "no" 1>&2
fi


## does Fortran append underscores to symbols?

echo $echo_n "does $CONF_FC append underscores... $echo_c" 1>&2

rm -f $test*

cat > $test.f << _EOF_
	subroutine uscore
	print *, "hi"
	end
_EOF_

echo "extern void uscore(); main() { uscore(); }" > ${test}a.c

$CONF_FC $CONF_FFLAGS -c $test.f > /dev/null 2>&1

$CONF_CC $CONF_CFLAGS ${test}a.c $test.o -o $test $CONF_LDFLAGS > /dev/null 2>&1

if [ -x $test ] ; then
  echo "no" 1>&2
  sed 's/_$//g' include/clooptools.h.in > include/clooptools.h
else
  echo "yes" 1>&2
  cp include/clooptools.h.in include/clooptools.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
FC = $CONF_FC
FCPP = $CONF_FCPP
FFLAGS = $CONF_FFLAGS
PREFIX = $CONF_PREFIX
# --- end defs by configure ---


_EOF_

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

sed "s:^libs=.*:libs=\"$CONF_LDFLAGS\":g" tools/fcc.in > tools/fcc
chmod +x tools/fcc


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

exit 0

