#!/bin/sh

# for Linux systems, allow setting of G2CLIBDIR, G2CLIBNAME to set
# location and name of required f2c/g2c library

if [ -z "$G2CLIBDIR" ] ; then
    G2CLIBDIR="/usr/lib/gcc-lib/i386-redhat-linux/2.96"
fi
if [ -z "$G2CLIBNAME" ] ; then
    G2CLIBNAME="g2c"
fi

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ; then
    echo "usage: exttest1 <C compiler> <F77 complier> <GAMS solver> [p3pc]"
    echo 
    echo "Possible OS/Compiler Combinations"
    echo "AIX:    C: xlc"
    echo "        Fortran: xlf"
    echo "HP-UX:  C: c89, gcc"
    echo "        Fortran: f77"
    echo "Linux:  C: gcc"
    echo "        Fortran: g77"
    echo "OSF1:   C: cc"
    echo "        Fortran: f77"
    echo "SunOS:  C: cc, gcc"
    echo "        Fortran: f77"
    echo "Available GAMS solvers:"
    echo "        conopt, conopt2"
    echo "If optional p3pc arg is used, build Delphi with p3pc"
    echo ""
    exit
fi
OS=`uname`
CC=$1
F77=$2
if [ ! -z "$4" ] ; then
    P3PC=p3pc
else
    P3PC=
fi

if [ $OS = "HP-UX" ]; then
  if [ $CC = "gcc" ]; then
    echo "Running: $CC -fpic -c ex3c_cb.c"
    $CC -fpic -c ex3c_cb.c
  elif [ $CC = "c89" ]; then
    echo "Running: $CC +Z -c ex3c_cb.c"
    $CC +Z -c ex3c_cb.c
  else
    echo "Sorry can't test HP-UX with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp ex3f_cb.for ex3f_cb.f
  cp unxmsg_f.for unxmsg_f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 +Z -c ex3f_cb.f"
    $F77 +Z -c ex3f_cb.f
    echo "Running: $F77 +Z -c unxmsg_f.f"
    $F77 +Z -c unxmsg_f.f
  else
    echo "Sorry can't test HP-UX with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld -b -o ex3c_cb.so ex3c_cb.o -lc -lm
  ld -b -o ex3f_cb.so ex3f_cb.o unxmsg_f.o -lc -lm
elif [ $OS = "OSF1" ]; then
  if [ $CC = "cc" ]; then
    echo "Running: $CC -c ex3c_cb.c"
    $CC -std1 -c ex3c_cb.c
  else
    echo "Sorry can't test OSF1 with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  sed /\!DEC/d ex3f_cb.for > ex3f_cb.f
  cp unxmsg_f.for unxmsg_f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 -c ex3f_cb.f"
    $F77 -c ex3f_cb.f
    echo "Running: $F77 -c unxmsg_f.f"
    $F77 -c unxmsg_f.f
  else
    echo "Sorry can't test OSF1 with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld  -o ex3c_cb.so ex3c_cb.o -lc -lm -shared
  ld  -o ex3f_cb.so ex3f_cb.o unxmsg_f.o -lc -lm -lfor -shared
elif [ $OS = "IRIX64" ]; then
  if [ $CC = "cc" ]; then
    echo "Running: $CC -c ex3c_cb.c"
    $CC -c -n32 -mips4 ex3c_cb.c
  else
    echo "Sorry can't test IRIX64 with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp ex3f_cb.for ex3f_cb.f
  cp unxmsg_f.for unxmsg_f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 -c ex3f_cb.f"
    $F77 -c ex3f_cb.f
    echo "Running: $F77 -c unxmsg_f.f"
    $F77 -c unxmsg_f.f
  else
    echo "Sorry can't test IRIX64 with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld  -o ex3c_cb.so ex3c_cb.o -shared
  ld  -o ex3f_cb.so ex3f_cb.o unxmsg_f.o -shared
elif [ $OS = "AIX" ]; then
  if [ $CC = "xlc" ]; then
    echo "Running: $CC -c ex3c_cb.c"
    $CC -c ex3c_cb.c
  else
    echo "Sorry can't test AIX with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp ex3f_cb.for ex3f_cb.f
  cp unxmsg_f.for unxmsg_f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "xlf" ]; then
    echo "Running: $F77 -c ex3f_cb.f"
    $F77 -c ex3f_cb.f
    echo "Running: $F77 -c unxmsg_f.f"
    $F77 -c unxmsg_f.f
  else
    echo "Sorry can't test AIX with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  echo "gefunc" > gefunc.exp
  ld  -o ex3c_cb.so ex3c_cb.o -bE:gefunc.exp -G -lc -lm 
  ld  -o ex3f_cb.so ex3f_cb.o unxmsg_f.o -bE:gefunc.exp -G -lc -lm 
elif [ $OS = "SunOS" ]; then
  if [ $CC = "gcc" ]; then
    echo "Running: $CC -fpic -c ex3c_cb.c"
    $CC -fpic -c ex3c_cb.c
  elif [ $CC = "cc" ]; then
    echo "Running: $CC -c ex3c_cb.c"
    $CC -c ex3c_cb.c
  else
    echo "Sorry can't test SunOS with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp ex3f_cb.for ex3f_cb.f
  cp unxmsg_f.for unxmsg_f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 -c ex3f_cb.f"
    $F77 -c ex3f_cb.f
    echo "Running: $F77 -c unxmsg_f.f"
    $F77 -c unxmsg_f.f
  else
    echo "Sorry can't test SunOS with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld  -o ex3c_cb.so ex3c_cb.o -G
  ld  -o ex3f_cb.so ex3f_cb.o unxmsg_f.o -G
elif [ $OS = "Linux" ]; then
    if [ $CC = "gcc" ]; then
	echo "Running: $CC -fpic -c ex3c_cb.c"
	$CC -fpic -c ex3c_cb.c
    else
	echo "Sorry can't test Linux with C compiler $CC..."
	exit;
    fi
    sed s/__intone__/Integer*1/ geheader.in > geheader.inc
    if [ $F77 = "g77" ]; then
	echo "Running: $F77 -fpic -c ex3f_cb.for"
	$F77 -fpic -c ex3f_cb.for
	echo "Running: $F77 -fpic -c unxmsg_f.for"
	$F77 -fpic -c unxmsg_f.for
    else
	echo "Sorry can't test Linux with Fortran compiler $F77..."
	exit;
    fi
    echo "Linking..."
    ld -o ex3c_cb.so ex3c_cb.o -shared
    ld -o ex3f_cb.so ex3f_cb.o unxmsg_f.o -L$G2CLIBDIR -l$G2CLIBNAME -shared
else
    echo "Unknown operating system $OS. Exiting..."
    exit
fi
if [ ! -z "$P3PC" ] ; then
    $P3PC -B ex3d.dpr
    if [ ! -s ex3d.so ] ; then
	echo "$P3PC did not create a shared object"
	exit
    fi
fi

if [ -s ex3c_cb.so ] && [ -s ex3f_cb.so ] ; then
    echo "Checking GAMS system..."
    VER=`gamscmex.out audit | cut -c43-45`
    if [ $VER -le 113 ]; then
	echo "GAMS version not suitable for external functions"
	exit
    fi
    GAMSCMD="gams ex3 lo=2 nlp=$3"
    if [ -z "$P3PC" ] ; then
	GAMSCMD="$GAMSCMD user1=noD"
    fi
    echo "Running GAMS: $GAMSCMD"
    eval $GAMSCMD
    grep "@@@@" ex3.lst | grep "\-\-\-\-" | cut -d# -f2 > exttest3.out
    if [ -s exttest3.out ]; then
	cat exttest3.out
    else
	echo "Test not passed. Inspect ex3.lst for details."
    fi
fi
