#!/bin/csh -f
# sdsnp: script to decode a sif file and then run SNOPT on the output
#
# IRIX 6.4 version
#
# Use: sdsnp [-h] [-k] [-o j] [-l secs] probname
#
# where: options -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 : sets a limit of secs second on the SNOPT
#                     runtime
#                     (Default: 99999999 seconds)
#
#       probname      probname.SIF is the name of the file containing
#                     the SIF file for the problem of interest.
#

#
#  A. Barclay & P. Gill at UCSD, February 1996
#
# ** Correction report.
#    Initial file    FEB 96: Created by modifying MINOS interface file

# ** End of Correction report.
#

#
#  basic system commands
#

set RM = "/bin/rm -f"
set LN = /bin/ln
set LS = /bin/ls
set CP = /bin/cp
set MV = /bin/mv

#
#  directory for temporary files
#

set TMP=/tmp

#
#  variables for each option
#

#
#  LIMIT (maximum cputime for running SNOPT)
#

set LIMIT=99999999

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

set PRECISION=1

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

set KEEP=0

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

set OUTPUT=0

#
#  interpret arguments
#

set METHOD=3

@ last = $#argv

if ($last == 0) then
  echo 'Use: sdsnp [-h] [-k] [-o j] [-l secs] probname'
  exit 1
endif

@ i=1

while ($i <= $last)
  set opt=$argv[$i]
  if ("$opt" == '-h') then
    echo ' Use: sdsnp [-h] [-k] [-o j] [-l secs] probname'
    echo ' '
    echo ' where: options -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 : sets a limit of secs second on the SNOPT'
    echo '                     runtime'
    echo '                     (Default: unlimited cputime)'
    echo ' '
    echo '       probname      probname.SIF is the name of the file containing'
    echo '                     the SIF file for the problem of interest.'
    exit 0
  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 object
#  'snopts.o' in the directory $CUTEDIR/snopt, replace the three 
#  lines below with
#   set PRECISION=0
#  and do the corresponding replacement in the snp 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" == '-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]
  endif
  @ i++
end

if (! -e "$argv[$last].SIF") then
  echo "file $argv[$last].SIF is not known in directory $PWD."
  echo "possible choices are:"
  echo ' '
  $LS *.SIF
  echo ' '
  exit 2
endif

#
# Check for installation of requested precision
#
if (! $PRECISION) then
  if (! -e $CUTEDIR/sifdec_s) then
    echo ' '
    echo 'Single-precision SIF decoder sifdec_s not in '$CUTEDIR
    echo 'Terminating execution.'
    exit 4
  endif
else
  if (! -e $CUTEDIR/sifdec_d) then
    echo ' '
    echo 'Double-precision SIF decoder sifdec_d not in '$CUTEDIR
    echo 'Terminating execution.'
    exit 4
  endif
endif

if ($OUTPUT) then
  echo 'convert the sif file into data and routines suitable for optimizer...'
  echo ' '
  echo 'problem details will be given'
  echo ' '
endif

if (-e EXTERN.f) $RM EXTERN.f

#  set up required links to output files

if (-e $argv[$last].BASIS) $CP $argv[$last].BASIS SNOPT.BASIS

# call with two argument allows user to chose minimization method

echo $argv[$last] > $TMP/sdsnp.input
echo $METHOD >> $TMP/sdsnp.input
echo $OUTPUT >> $TMP/sdsnp.input

#
# Modified for double precision only
#
if (! $PRECISION && ! $KEEP) then
  $CUTEDIR/sifdec_s < $TMP/sdsnp.input
  if ($OUTPUT) echo ' '
  if (! -e OUTSDIF.d) then
    echo ' '
    echo "error exit from decoding stage. terminating execution."
    exit 3
  endif
  if ($OUTPUT) echo ' '
  $CUTEDIR/interfaces/snp -n -s -o $OUTPUT -l $LIMIT
else if (! $PRECISION && $KEEP) then
  $CUTEDIR/sifdec_s < $TMP/sdsnp.input
  if (! -e OUTSDIF.d) then
    echo ' '
    echo "error exit from decoding stage. terminating execution."
    exit 3
  endif
  if ($OUTPUT) echo ' '
  $CUTEDIR/interfaces/snp -n -s -k -o $OUTPUT -l $LIMIT
else if ($PRECISION && ! $KEEP) then
  $CUTEDIR/sifdec_d < $TMP/sdsnp.input
  if (! -e OUTSDIF.d) then
    echo ' '
    echo "error exit from decoding stage. terminating execution."
    exit 3
  endif
  if ($OUTPUT) echo ' '
  $CUTEDIR/interfaces/snp -n -o $OUTPUT -l $LIMIT
else if ($PRECISION && $KEEP) then
  $CUTEDIR/sifdec_d < $TMP/sdsnp.input
  if (! -e OUTSDIF.d) then
    echo ' '
    echo "error exit from decoding stage. terminating execution."
    exit 3
  endif
  if ($OUTPUT) echo ' '
  $CUTEDIR/interfaces/snp -n -k -o $OUTPUT -l $LIMIT
endif

$RM $TMP/sdsnp.input
if (-e SNOPT.OUT)      $MV SNOPT.OUT      $argv[$last].snp
if (-e SNOPT.BASIS)    $MV SNOPT.BASIS    $argv[$last].BASIS
if (-e SNOPT.NEWBASIS) $MV SNOPT.NEWBASIS $argv[$last].NEWBASIS
