#!/bin/csh -f
# npp: apply NPOPT to a problem and delete the executable after use.
#
# Version for SUN and SGI workstations under UNIX
#
# Use: npp [-n] [-h] [-k] [-o i] [-l secs]
#
# where: options -n : use the load module f if it exists
#                     (Default: create a new load module)
#                -h : print this help and stop execution
#                -k : keep the load module after use
#                     (Default: delete the load module)
#                -o : 0 for silent mode, 1 for brief description of
#                     the stages executed
#                     (Default: -o 0)
#                -l : limit the cputime used to secs seconds
#                     (Default: -l 99999999)

#
#  A. Barclay & P. Gill at UCSD, Feb 1996
#  Created by modifying MINOS interface file
#  Nov 22, 1996: Object library added,
#  Aug 30, 1998: Dynamic Shared Object libraries added,

#
#  basic system commands
#

set RM   = "/bin/rm -f"
set MAKE = /bin/make
set CAT  = /bin/cat
set MV   = /bin/mv
set CP   = /bin/cp
unset noclobber

#
#  Define where the snopt files can be found.
#

set NPPATH=$CUTEDIR/npopt

#
#  compiler flags (machine dependent)
#

set FORTRAN=f77
set FFLAGS="-O2"
set LINKPATH="-L"
set RUNPATH="-rpath"

#
#  If there are compiled, library versions of the level-1 blas
#  (basic linear algebra subprograms), set BLAS to a list of
#  names of the object library suffix -lx, where the object library
#  libx.a contains relevant blas. For instance if the blas are
#  shared between object libraries libblas1.a and libblas2.a,
#  BLAS should be set to "-lblas1 -lblas2", noting that those in
#  libblas1.a will take precedence over those in libblas2.a.
#  If compiled blas are unavailable, BLAS should be set to ""
#

set BLAS="-lblas"

#
#  directory for the main executable file
#

set EXEC=$cwd

#
#  directory for temporary files
#

set TMP=/tmp

#
#  Name of the shared object version of NPOPT (double precision only)
#  Check for existence of NPOPT object file
#

if (-e $NPPATH/libcutenpopt.so) then
  set NPOPT="-lcutenpopt"
  set NPPMA=npoptma.o
  echo ' '
  echo 'NPOPT dynamic shared object library is '$NPPATH/libcutenpopt.so
  echo ' '
else
  echo ' '
  echo 'NPOPT object file not in '$NPPATH
  echo 'Terminating execution.'
  exit 5
endif

#
#  variables for each option
#

#
# PRECISION = 0 (single precision), = 1 (double precision)
#

set PRECISION=1

#
# NEW = 0 (run existing f module), = 1 (build a new module)
#

set NEW=0

#
# KEEP = 0 (discard f load module after use), = 1 (keep it)
#

set KEEP=0

#
# OUTPUT = 0 (summary output), = 1 (detailed output from decoder)
#

set OUTPUT=0

#
# LIMIT = 0 (no cputime limit)
#

set LIMIT=99999999

#
#  interpret arguments
#

@ last=$#argv
@ i=1

while ($i <= $last)
  set opt=$argv[$i]
  if ("$opt" == '-n') then
    set NEW=1
  else if ("$opt" == '-s') then
#
#  NPOPT is distributed in double precision only.
#  If you wish to create and run your own single precision
#  version of NPOPT, place the compiled single precision
#  object library 'libcutenpopts.a' in the directory $CUTEDIR/npopt,
#  replace the three lines below with
#   set PRECISION=0
#  and do the corresponding replacement in the sdnpo script.
#  All the other logic is already in place!
#  You should also edit the comments at the top of this
#  file and the output generated when the -h option
#  is selected, to indicate that a single precision
#  version is available.
#
    echo ' NPOPT is not available in single precision.'
    echo ' Execution stopping.'
    exit 0
  else if ("$opt" == '-h') then
    echo ' Use: npp [-n] [-h] [-k] [-o i] [-l secs]'
    echo ' '
    echo ' where: options -n : use the load module f if it exists'
    echo '                     (Default: create a new load module)'
    echo '                -h : print this help and stop execution'
    echo '                -k : keep the load module after use '
    echo '                     (Default: delete the load module)'
    echo '                -o : 0 for silent mode, 1 for brief description of'
    echo '                     the stages executed'
    echo '                     (Default: -o 0)'
    echo '                -l : limits the cputime to secs seconds'
    echo '                     (Default: -l 99999999)'
    exit 0
  else if ("$opt" == '-k') then
    set KEEP=1
  else if ("$opt" == '-o') then
    @ i++
    set OUTPUT=$argv[$i]
  else if ("$opt" == '-l') then
    @ i++
    set LIMIT=$argv[$i]
  else
    echo 'Use: gen [-n] [-h] [-k] [-o i] [-l secs]'
    exit 1
  endif
  @ i++
end

#
# copy the default specs file and remove old fort.9, if necessary
#

if (! -e NPOPT.SPC) $CP $CUTEDIR/npopt/NPOPT.SPC NPOPT.SPC
if (-e fort.15) $RM fort.15

#
#  run NPOPT without rebuilding it
#

if (! $NEW) then
  if (! -e $EXEC/nppmin || ! -x $EXEC/nppmin) then
    echo ' '
    echo "load module nppmin not found/executable. Rerun with -n option"
    echo ' '
    exit 3
  endif
  if ($OUTPUT) then
    echo ' '
    echo "running NPOPT on current test problem ... "
    echo ' '
  endif
  limit cputime $LIMIT
  $EXEC/nppmin

#  tidy up the current directory, deleting all junk.

  if (! $KEEP) $RM $EXEC/nppmin
  if (-e fort.15) $MV fort.15 NPOPT.LIS
  exit 0
endif

#
#  build NPOPT and tools
#
#  directory for object files
#

set OBJ=$CUTEDIR/tools/objects/double

#
#  if there are no library blas, include the default ones.
#
if ("$BLAS" == '') then
   set BLAS=$OBJ/linpac.o
endif

#
#  list of Fortran tools needed for the interface
#

set TOOLS = " $OBJ/csetup.o $OBJ/cnames.o $OBJ/cofg.o $OBJ/ccfg.o "

# ensure that the current test problem has been compiled.

if ($OUTPUT) then
  echo 'compiling the current test problem, if that is necessary ... '
  echo ' '
endif

if (-e EXTERN.f && ! -z EXTERN.f) then 
  $MAKE -f $CUTEDIR/compil p >& $TMP/nppf77
endif
if (! -e EXTERN.f || -z EXTERN.f) then 
  $MAKE -f $CUTEDIR/compil pnoextern >& $TMP/nppf77
endif
if ($status != 0) then
  $CAT $TMP/nppf77
  $RM $TMP/nppf77
  exit 1
endif

# link all the NPOPT and tools files together.

if ($OUTPUT) then
  $CAT $TMP/nppf77
  echo ' '
  echo 'linking all the object files together ... '
  echo ' '
endif
$RM $TMP/nppf77

if (-e EXTERN.o && -z EXTERN.o) $RM EXTERN.o
if (-e EXTERN.f && -z EXTERN.f) $RM EXTERN.f

if (-e EXTERN.f) then
  $FORTRAN $FFLAGS -o nppmin\
  ELFUNS.o GROUPS.o RANGES.o SETTYP.o EXTERN.o $TOOLS\
  $OBJ/elgrd.o $OBJ/others.o $OBJ/initw.o $OBJ/local.o\
  $OBJ/$NPPMA $RUNPATH $NPPATH $LINKPATH $NPPATH $NPOPT $BLAS
  if ( $cwd != $EXEC ) $MV nppmin $EXEC/nppmin
endif

if (! -e EXTERN.f) then
  $FORTRAN $FFLAGS -o nppmin\
  ELFUNS.o GROUPS.o RANGES.o SETTYP.o $TOOLS\
  $OBJ/elgrd.o $OBJ/others.o $OBJ/initw.o $OBJ/local.o\
  $OBJ/$NPPMA $RUNPATH $NPPATH $LINKPATH $NPPATH $NPOPT $BLAS
  if ( $cwd != $EXEC ) $MV nppmin $EXEC/nppmin
endif

#  run NPOPT on the current test problem.

if ($OUTPUT) then
  echo ' '
  echo 'running NPOPT on current test problem ... '
  echo ' '
endif

limit cputime $LIMIT
$EXEC/nppmin

#  tidy up the current directory, deleting all junk.

if (! $KEEP) $RM $EXEC/nppmin
if (-e fort.15) $MV fort.15 NPOPT.LIS
