#!/bin/csh -f
# snp: apply SNOPT to a problem and delete the executable after use.
#
# version for SUN and SGI workstations under UNIX
#
# Use: snp [-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 CP   = /bin/cp
set RM   = "/bin/rm -f"
set LN   = /bin/ln
set MAKE = /bin/make
set CAT  = /bin/cat
set MV   = /bin/mv
unset noclobber

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

set SNPATH=$CUTEDIR/snopt

#
#  Linker and compiler flags (machine dependent)
#  These are for IRIX 6.4
#

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 SNOPT (double precision only)
#  Check for existence of SNOPT object file
#

if (-e $SNPATH/libcutesnopt.so) then
  set SNOPT="-lcutesnopt"
  set SNPMA=snoptma.o
  echo ' '
  echo 'SNOPT dynamic shared object library is '$SNPATH/libcutesnopt.so
  echo ' '
else
  echo ' '
  echo 'SNOPT object file not in '$SNPATH
  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
#
#  SNOPT is distributed in double precision only.
#  If you wish to create and run your own single precision
#  version of SNOPT, place the compiled single precision
#  shared object library 'libcutesnopts.so' in the directory
#  $CUTEDIR/snopt, replace the three lines below with
#   set PRECISION=0
#  and do the corresponding replacement in the sdsnp 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 ' SNOPT is not available in single precision.'
    echo ' Execution stopping.'
    exit 0
  else if ("$opt" == '-h') then
    echo ' Use: snp [-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: snp [-n] [-h] [-k] [-o i] [-l secs]'
    exit 1
  endif
  @ i++
end

#
#  open the SNOPT specs and basis files, using the default specs if necessary
#

if (! -e SNOPT.SPC) $CP $SNPATH/SNOPT.SPC SNOPT.SPC
if (-e fort.4) $RM fort.4
$LN -s SNOPT.SPC fort.4
if (-e fort.9) $RM fort.9
if (-e SNOPT.BASIS) then
  if (-e fort.11) $RM fort.11
  $LN -s SNOPT.BASIS fort.11
endif
if (-e fort.12) $RM fort.12

#
#  run SNOPT without rebuilding it
#

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

#  tidy up the current directory, deleting all junk.

  $RM fort.4
  if (-e fort.15) $MV fort.15 SNOPT.OUT
  if (-e fort.11) $MV fort.11 SNOPT.BASIS
  if (-e fort.12) $MV fort.12 SNOPT.NEWBASIS
  if (! $KEEP) $RM $EXEC/snpmin
  exit 0
endif

#
#  build SNOPT and tools
#

#
#  Set the directory for object files
#

set OBJ=$CUTEDIR/tools/objects/double

# 
#  check for installation of requested precision
#
if (! -e $OBJ/initw.o) then
  echo ' '
  echo 'Object files for tools not in '$OBJ
  echo 'Terminating execution.'
  exit 4
endif

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

#
# 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) \
  $MAKE -f $CUTEDIR/compil p >& $TMP/snpf77
if (! -e EXTERN.f || -z EXTERN.f) \
  $MAKE -f $CUTEDIR/compil pnoextern >& $TMP/snpf77

if ($status != 0) then
  $CAT $TMP/snpf77
  $RM $TMP/snpf77
  exit 1
endif

# link all the SNOPT and tools files together.

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

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 snpmin\
    ELFUNS.o GROUPS.o RANGES.o SETTYP.o EXTERN.o\
    $OBJ/csetup.o $OBJ/cofg.o $OBJ/ccfg.o $OBJ/cscfg.o\
    $OBJ/cgr.o $OBJ/csgr.o\
    $OBJ/elgrd.o $OBJ/others.o $OBJ/initw.o $OBJ/local.o\
    $OBJ/$SNPMA $RUNPATH $SNPATH $LINKPATH $SNPATH $SNOPT $BLAS
  if ($cwd != $EXEC) $MV snpmin $EXEC/snpmin
endif

if (! -e EXTERN.f) then
  $FORTRAN $FFLAGS -o snpmin\
    ELFUNS.o GROUPS.o RANGES.o SETTYP.o\
    $OBJ/csetup.o $OBJ/cofg.o $OBJ/ccfg.o $OBJ/cscfg.o\
    $OBJ/cgr.o $OBJ/csgr.o\
    $OBJ/elgrd.o $OBJ/others.o $OBJ/initw.o $OBJ/local.o\
    $OBJ/$SNPMA $RUNPATH $SNPATH $LINKPATH $SNPATH $SNOPT $BLAS
  if ($cwd != $EXEC) $MV snpmin $EXEC/snpmin
endif

#  run SNOPT on the current test problem.

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

limit cputime $LIMIT
$EXEC/snpmin

#  tidy up the current directory, deleting all junk.

$RM fort.4
if (-e fort.15) $MV fort.15 SNOPT.OUT
if (-e fort.11) $MV fort.11 SNOPT.BASIS
if (-e fort.12) $MV fort.12 SNOPT.NEWBASIS
if (! $KEEP) $RM $EXEC/snpmin
