#!/bin/sh

if  [ x$1 = x ] || [ x$2 = x ] || [ x$3 = x ]; then
        echo "usage: exttestmcp1 <C compiler> <Fortran complier> <GAMS solver>";
	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 "        miles pathc"
        exit;
fi
OS=`uname`
CC=$1
F77=$2
if [ $OS = "HP-UX" ]; then
  if [ $CC = "gcc" ]; then
    echo "Running: $CC -fpic -c exmcp1c.c"
    $CC -fpic -c exmcp1c.c
  elif [ $CC = "c89" ]; then
    echo "Running: $CC +Z -c exmcp1c.c"
    $CC +Z -c exmcp1c.c
  else
    echo "Sorry can't test HP-UX with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp exmcp1f.for exmcp1f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 +Z -c exmcp1f.f"
    $F77 +Z -c exmcp1f.f
  else
    echo "Sorry can't test HP-UX with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld -b -o exmcp1c.so exmcp1c.o -lc -lm
  ld -b -o exmcp1f.so exmcp1f.o
elif [ $OS = "OSF1" ]; then
  if [ $CC = "cc" ]; then
    echo "Running: $CC -c exmcp1c.c"
    $CC -std1 -c exmcp1c.c
  else
    echo "Sorry can't test OSF1 with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  sed /\!DEC/d exmcp1f.for > exmcp1f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 -c exmcp1f.f"
    $F77 -c exmcp1f.f
  else
    echo "Sorry can't test OSF1 with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld  -o exmcp1c.so exmcp1c.o -lc -lm -shared
  ld  -o exmcp1f.so exmcp1f.o -lc -lm -lfor -shared
elif [ $OS = "IRIX64" ]; then
  if [ $CC = "cc" ]; then
    echo "Running: $CC -c exmcp1c.c"
    $CC -c -n32 -mips4 exmcp1c.c
  else
    echo "Sorry can't test IRIX64 with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp exmcp1f.for exmcp1f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 -c exmcp1f.f"
    $F77 -c exmcp1f.f
  else
    echo "Sorry can't test IRIX64 with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld  -o exmcp1c.so exmcp1c.o -shared
  ld  -o exmcp1f.so exmcp1f.o -shared
elif [ $OS = "AIX" ]; then
  if [ $CC = "xlc" ]; then
    echo "Running: $CC -c exmcp1c.c"
    $CC -c exmcp1c.c
  else
    echo "Sorry can't test AIX with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp exmcp1f.for exmcp1f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "xlf" ]; then
    echo "Running: $F77 -c exmcp1f.f"
    $F77 -c exmcp1f.f
  else
    echo "Sorry can't test AIX with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  echo "gefunc" > gefunc.exp
  ld  -o exmcp1c.so exmcp1c.o -bE:gefunc.exp -G -lc -lm 
  ld  -o exmcp1f.so exmcp1f.o -bE:gefunc.exp -G -lc -lm 
elif [ $OS = "SunOS" ]; then
  if [ $CC = "gcc" ]; then
    echo "Running: $CC -fpic -c exmcp1c.c"
    $CC -fpic -c exmcp1c.c
  elif [ $CC = "cc" ]; then
    echo "Running: $CC -c exmcp1c.c"
    $CC -c exmcp1c.c
  else
    echo "Sorry can't test SunOS with C compiler $CC..."
    exit;
  fi
  echo "Copy *for to *f..."
  cp exmcp1f.for exmcp1f.f
  sed s/__intone__/byte/ geheader.in > geheader.inc
  if [ $F77 = "f77" ]; then
    echo "Running: $F77 -c exmcp1f.f"
    $F77 -c exmcp1f.f
  else
    echo "Sorry can't test SunOS with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld  -o exmcp1c.so exmcp1c.o -G
  ld  -o exmcp1f.so exmcp1f.o -G
elif [ $OS = "Linux" ]; then
  if [ $CC = "gcc" ]; then
    echo "Running: $CC -fpic -c exmcp1c.c"
    $CC -fpic -c exmcp1c.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 -fno-globals -Wno-globals -c exmcp1f.for"
    $F77 -fpic -c -fno-globals -Wno-globals exmcp1f.for
  else
    echo "Sorry can't test Linux with Fortran compiler $F77..."
    exit;
  fi
  echo "Linking..."
  ld -o exmcp1c.so exmcp1c.o -shared
  ld -o exmcp1f.so exmcp1f.o -L/usr/lib/gcc-lib/i386-redhat-linux/2.96 -lg2c -shared
else
 echo "Unknown operating system $OS. Exiting..."
 exit
fi
if [ -s exmcp1c.so ] && [ -s exmcp1f.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 exmcp1 lo=2 mcp=$3 user1=noD"
  echo "Running GAMS: $GAMSCMD"
  eval $GAMSCMD
  grep "@@@@" exmcp1.lst | grep "\-\-\-\-" | cut -d# -f2 > exttestmcp1.out
  if [ -s exttestmcp1.out ]; then
    cat exttestmcp1.out
  else
    echo "Test not passed. Inspect exmcp1.lst for details."
  fi
fi
