#!/bin/csh

#
# lbfgsvar.script: Script to run L-BFGS with variations.
#
# Version for SUN workstations under UNIX.
# 
# Use: lbfgs.script [-c] [-e "options"] [-k] [-l] [-n] [-p probname] [-r] [-o]
#
# Where: options -c : Print output from lbfgsvar program in concise mode.
#                -e : Specify compile line options for lbfgsvar.F.
#                     Also activates -n and -r options.
#                     (Default: -e "-DMVAL=5 -DINFO=1")
#                -k : Keep executable when script completes.
#                     (Default: Do not keep.)
#                -l : CPU time limit in seconds.
#                     (Default: -l 99999999)
#                -n : Recompile executable even if it already exists.
#                     (Default: Use previous executable if it exists.)
#                -p : Decode probname.SIF. Activates -n option.
#                     (Default: Use most recently decoded SIF file.)
#                -r : Remake lbfgsvar.o. 
#                     (Default: Do not remake.)
#                -o : Verbose mode.
#                     (Default: Not verbose.)
#
# Tamara Gibson (Kolda), Summer 1996.
# Copyright (c) 1996.
# University of Maryland at College Park.
# All rights reserved.
#

#
# Basic system commands
#

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

#
# Directories for various items
#

set TMP=/tmp
set OBJ=$CUTEDIR/tools/objects/double
set LBVDIR=$CUTEDIR/lbfgsvar
set EXEC=$cwd

#
# Other Variables
#

set FORTRAN=f77
set FFLAGS=-e
set METHOD=3
set BLAS=$LBVDIR/blas.o

#
# Option Defaults
#

# CONCISE = 1 (output in concise mode) or 0 (not concise)
set CONCISE = 0

# DOSIF = 1 (read SIF file) or 0 (use previous SIF info)
set DOSIF = 0

# KEEP = 0 (delete executable) or 1 (keep it)
set KEEP=0

# LIMIT = CPU time limit in seconds
set LIMIT=99999999

# NEW = 1 (make new executable) or 0 (use old executable)
set NEW = 1

# OFLAGS = flags for compiling lbfgsvar.F 
set OFLAGS = "-DMVAL=5 -DINFO=1"

# OUTPUT = 0 (summary output) or 1 (display detailed info)
set OUTPUT=0

# REMAKE = 1 (remake lbfgsvar.o) or 0 (don't remake)
set REMAKE = 0


#
# Interpret Arguments
#

@ last = $#argv
@ i=1
set SCRIPT = "lbfgsvar.script"

while ($i <= $last) 
	set opt = $argv[$i]
	if ( "$opt" == '-h' ) then
echo 'Use: lbfgs.script [-c] [-e "options"] [-k] [-l] [-n] [-p probname] [-r] [-o]'
echo 'Where: options -c : Print output from lbfgsvar program in concise mode.'
echo '               -e : Specify compile line options for lbfgsvar.F.'
echo '                    Also activates -n and -r options.'
echo '                    (Default: -e "-DMVAL=5 -DINFO=1")'
echo '               -k : Keep executable when script completes.'
echo '                    (Default: Do not keep.)'
echo '               -l : CPU time limit in seconds.'
echo '                    (Default: -l 99999999)'
echo '               -n : Recompile executable even if it already exists.'
echo '                    (Default: Use previous executable if it exists.)'
echo '               -p : Decode probname.SIF. Activates -n option.'
echo '                    (Default: Use most recently decoded SIF file.)'
echo '               -r : Remake lbfgsvar.o. '
echo '                    (Default: Do not remake.)'
echo '               -o : Verbose mode.'
echo '                    (Default: Not verbose.)'
		exit 1
	else if ( "$opt" == '-c' ) then
		set CONCISE = 1
	else if ( "$opt" == '-e' ) then
		@ i++
		set REMAKE = 1
		set NEW = 1
		set OFLAGS = "$argv[$i]"
	else if ( "$opt" == '-k' ) then
		set KEEP = 1
	else if ( "$opt" == '-l' ) then
		@ i++
		set LIMIT = $argv[$i]
	else if ( "$opt" == '-n' ) then
		set NEW = 1
	else if ( "$opt" == '-p' ) then
		set DOSIF = 1
		@ i++
		set NEW = 1
		set SIFNAME = $argv[$i]
		set SIFFILE = "$MASTSIF/$argv[$i].SIF"
	else if ( "$opt" == '-o' ) then
		set OUTPUT = 1
	else if ( "$opt" == '-r' ) then
		set REMAKE = 1
		set NEW = 1
	else
		echo ' '
		echo 'usage:: '$SCRIPT' [-c] [-e "options"] [-h] [-k] [-l seconds]'
		echo '                  [-n] [-p SIFFILE] [-r]'
		echo ' '
		exit 1
	endif
	@ i++
end


#
# Decode SIF File
#

if $DOSIF then
	if (! -e $SIFFILE) then
		echo ' '
		echo "Error: File $SIFFILE does not exit."
		echo 'Terminating Execution.'
		echo ' '
		exit 2
	endif
	
	if (! -e $CUTEDIR/sifdec_d) then
	   	echo ' '
	  	echo 'Error: Double-precision SIF decoder sifdec_d not in '$CUTEDIR
	  	echo 'Terminating execution.'
	  	exit 4
	endif
	
	if ( $OUTPUT ) then
	  	echo ' '
	  	echo 'Decoding SIF file ...'
	  	echo ' '
	endif
	
	if ( -e EXTERN.f ) $RM EXTERN.f
	echo $SIFNAME > $TMP/sdlbfgsvar.input
	echo $METHOD >> $TMP/sdlbfgsvar.input
	echo 0 >> $TMP/sdlbfgsvar.input
	$CP $SIFFILE .
	$CUTEDIR/sifdec_d < $TMP/sdlbfgsvar.input
	$RM $SIFNAME.SIF
	$RM $TMP/sdlbfgsvar.input
	if ( ! -e OUTSDIF.d ) then
	  	echo ' '
	 	echo "Error in decoding stage. The file OUTSDIF.d does not exist."
	  	echo "Terminating execution."
	  	exit 3
	endif
endif

#
# Run current executable if neither -n, -r, nor -e option is chosen
#

if (! $NEW) then
	if (! -e $EXEC/lbfgsvarmin || -x $EXEC/lbfgsvarmin) then
    		echo ' '
    		echo 'Error: Load module lbfgsvarmin not found/executable.'
    		echo 'Rerun with -n, -r, or -e option.'
    		echo ' '
    		exit 3
	endif
	if ($OUTPUT) then
		echo ' '
		echo 'Running lbfgsvar on current test problem ...'
		echo ' '
	endif
	limit cputim $LIMIT
	$EXEC/lbfgsvarmin
	if (! $KEEP) $RM $EXEC/lbfgsvarmin
	exit 4
endif

#
# Check that initw.o is available
#

if (! -e $OBJ/initw.o) then
	echo ' '
	echo 'Error: Object files for tools not in '$OBJ
	echo 'Terminating Execution'
	echo ' '
	exit 5
endif

#
# Compile lbfgsvar.F if necessary
#

if ($REMAKE || ! -e $LBVDIR/lbfgsvar.o) then
	if ($OUTPUT) then
		echo ' '
		echo 'Compiling lbfgsvar.o ...'
		echo ' '
	endif
	$RM /tmp/lbfgsvarcompile
	$RM $LBVDIR/lbfgsvar.o
	$FORTRAN $FFLAGS $OFLAGS -c $LBVDIR/lbfgsvar.F -o $LBVDIR/lbfgsvar.o >& /tmp/lbfgsvarcompile
endif

if (! -e $LBVDIR/lbfgsvar.o) then
	echo ' '
	echo 'Error: lbfgsvar.o does not exist.'
	echo 'Terminating execution.'
	$CAT /tmp/lbfgsvarcompile
	$RM /tmp/lbfgsvarcompile
	echo ' '
	exit 1
endif
$RM /tmp/lbfgsvarcompile

#
# Compile current test problem if necessary
#

if ($OUTPUT) then
  	echo ' '
  	echo 'Compiling the current test problem ... '
  	echo ' '
endif

$RM $TMP/lbfgsvarf77

if (-e EXTERN.f && ! -z EXTERN.f) then
	$MAKE -f $CUTEDIR/compil p >& $TMP/lbfgsvarf77
endif
if (! -e EXTERN.f || -z EXTERN.f) then
  	$MAKE -f $CUTEDIR/compil pnoextern >& $TMP/lbfgsvarf77
endif

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

$RM $TMP/lbfgsvarf77

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

#
# Compile executable
#

if ($OUTPUT) then
	echo ' '
	echo 'Compiling final executable ...'
	echo ' '
endif

if (-e EXTERN.f) then
  	$FORTRAN $FFLAGS -o lbfgsvarmin\
    		ELFUNS.o GROUPS.o RANGES.o SETTYP.o EXTERN.o\
    		$OBJ/usetup.o $OBJ/ufn.o $OBJ/ugr.o $OBJ/unames.o\
    		$OBJ/elgrd.o $OBJ/others.o $OBJ/initw.o $OBJ/local.o\
    		$OBJ/lbfgsvarma.o $LBVDIR/lbfgsvar.o $LBVDIR/linesearch.o\
		$BLAS
  	if ($cwd != $EXEC) $MV lbfgsvarmin $EXEC/.
	
endif

if (! -e EXTERN.f) then
  	$FORTRAN $FFLAGS -o lbfgsvarmin $OBJ/lbfgsvarma.o \
      		ELFUNS.o GROUPS.o RANGES.o SETTYP.o\
    		$OBJ/usetup.o $OBJ/ufn.o $OBJ/ugr.o $OBJ/unames.o\
    		$OBJ/elgrd.o $OBJ/others.o $OBJ/initw.o $OBJ/local.o\
    		$LBVDIR/lbfgsvar.o $LBVDIR/linesearch.o $BLAS
  	if ($cwd != $EXEC) $MV lbfgsvarmin $EXEC/.
	
endif

#
# Execute Program
#

if ($OUTPUT) then
	echo ' '
	echo 'Running lbfgsvar on current test problem ...'
	echo ' '
endif

limit cputime $LIMIT

if ($CONCISE) then
	$EXEC/lbfgsvarmin -c
else
	$EXEC/lbfgsvarmin
endif

#
# Remove Program if necessary
#

if (! $KEEP) $RM $EXEC/lbfgsvarmin

#
# Tamara Gibson (Kolda), Summer 1996.
# Copyright (c) 1996.
# University of Maryland at College Park.
# All rights reserved.
#
