#!/bin/sh
# configure script for the GNUmakefile
# this file is part of FormCalc
# last modified 12 Feb 03 th
# note: has nothing to do with GNU autoconf


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
}


findpath()
{
  echo $echo_n "looking for path to $1... $echo_c" 1>&2
  file=$2
  shift 2
  for path in $* ; do
    if [ -f "$path/$file" ] ; then
      echo $path 1>&2
      echo $path
      return 0
    fi
  done
  echo "no $file in $*" 1>&2
  return 1
}


## look for some programs

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

if [ -z "$FC" ] ; then
  case $HOSTTYPE in
  alpha)
	FC=f77
	FFLAGS="${FFLAGS--O -extend_source -warn truncated_source}" ;;
  *sun*)
	FC=f77
	FFLAGS="${FFLAGS--e}" ;;
  hp*)
	FC=fort77
	FFLAGS="${FFLAGS--O2 +es +U77}" ;;
  *86 | *linux)
	FC=pgf77
	FFLAGS="${FFLAGS--fast -Mextend -g77libs}" ;;
  esac
fi

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

$CONF_FC -v -c 2>&1 | grep "gcc version" > /dev/null && \
  CONF_FFLAGS="-O0 -ffixed-line-length-none"


## check for the necessary libraries

LTDIR=`tcsh -c 'echo LoopTools/$HOSTTYPE'`
CONF_LTDIR=`findpath LoopTools lib/libooptools.a ../$LTDIR ../../$LTDIR $HOME/$LTDIR /usr/local /usr` || exit 1

if [ "$1" = "--with-cernlib" ] ; then
  if [ -z "$CERN" -o -z "$CERN_LEVEL" -o ! -d $CERN/$CERN_LEVEL ] ; then
    echo ""
    echo "Warning: CERNlib is not or not properly installed on your system."
    echo "If it is installed, the environment variables CERN and CERN_LEVEL"
    echo "should be set such that \$CERN/\$CERN_LEVEL/lib is the directory"
    echo "in which the CERNlib libraries are installed."
    echo ""
  else
    CONF_CERNLIB='-L$(CERN)/$(CERN_LEVEL)/lib -lpdflib -lpacklib'
    CONF_FFLAGS="$CONF_FFLAGS -DCERNLIB"
  fi
fi


echo "creating GNUmakefile" 1>&2

cat - GNUmakefile.in > GNUmakefile << _EOF_
# --- variables defined by configure ---

LTDIR = $CONF_LTDIR
LIBS = -L\$(LTDIR)/lib -looptools $CONF_CERNLIB

FC = $CONF_FC
FFLAGS = $CONF_FFLAGS -I. -I\$(LTDIR)/include -g

ifdef DEBUG
FFLAGS += -DDEBUG
endif

# --- end defs by configure ---


_EOF_

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

exit 0

