#!/bin/sh

LIBS=shared

if [ $# -lt 4 ] ; then
  echo "usage: $0 <problem_name> <spc_file> <exe_dir> <lib_dir> [static]"
  echo "where:                                                          "
  echo " <problem_name> is one of the following:                        "
  echo "   adlittle   pilot4     refinery                               "
  echo " <spc_file> is the options file (e.g., "lp" for lp.spc)         "
  echo " <exe_dir>  is the full path to the executables                 "
  echo " <lib_dir>  is the full path to the libraries                   "
  echo " By default, shared object libraries are used.  The optional    " 
  echo "             argument "static" gives static object libraries.   "
  exit 1 
fi

CURDIR=$PWD
PROBLEM=$1
SPC=$2
EXEDIR=$3
LIBDIR=$4
LIBTYP=$5

if [ "$LIBTYP" = "" ]; then
   LIBTYP=$LIBS
fi

SPC=`basename $SPC .spc`  # Delete the .spc suffix if present

#
# Copy relevant files to the directory of executables
#

if test ! -f  $PROBLEM.mps
then
   echo "Error: \"$PROBLEM\" is an unrecognized problem."
   echo "Error: No mps file found."
   exit 1
else
   cp  $PROBLEM.mps $EXEDIR/fort.10
fi

if test ! -f $SPC.spc 
then
   echo "Error: No specs file  $SPC.spc  found for \"$PROBLEM\"."
   exit 2
else
   cp  $SPC.spc $EXEDIR/fort.4
fi

LD_LIBRARY_PATH=$LIBDIR:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
echo   LD_LIBRARY_PATH=$LD_LIBRARY_PATH

#
# Execute the example
#

case $LIBTYP in
*static*)   PROBEXE=sqopt_a  ;;
*shared*)   PROBEXE=sqopt    ;;
esac

echo "                                "
echo "Running executable: \"$PROBEXE\""
echo "                                "

cd $EXEDIR
./$PROBEXE

#
# Clean up
#

if test -f  $EXEDIR/fort.15
then
   \mv  $EXEDIR/fort.15  $EXEDIR/$PROBLEM.out
   \cp  $EXEDIR/$PROBLEM.out $CURDIR/
fi

if test -f  $EXEDIR/fort.11
then
   \mv  $EXEDIR/fort.11  $EXEDIR/$PROBLEM.basis
fi

if test -f   $EXEDIR/fort.12
then
   \mv $EXEDIR/fort.12  $EXEDIR/$PROBLEM.newbasis
fi

if test -f  $EXEDIR/fort.4
then
   \rm $EXEDIR/fort.4
fi

if test -f  $EXEDIR/fort.10
then
   \rm $EXEDIR/fort.10
fi
