#!/bin/sh
#========================================================================
# This is a UNIX compiler-like interface for SFTRAN3 code,
# permitting one-step preprocessing, compilation, and linking.
#
# SFTRAN3 processor options recognized and passed to sf3 are:
#
# -C	- produce CDC-style code for READ/WRITE END and ERR parameters
# -D	- debug - include sf3 code as comments in output Fortran code
# -E	- run SFTRAN3 processor only, do not invoke Fortran compiler
# -H	- display a help file and terminate immediately
# -I:n	- set structure indentation to n (0..9).
# -K	- keep ".f" files after compilation
# -L	- set the LINEUP option which connects structure begin/end with
#	  dotted lines in the ".sfl" files
# -M:nn - set maximum lines/page to nn (suggest 58 for 6 lpi and 80 for
#         8 lpi); ignored when prettyprinting.
# -N	- no ".sfl" files
# -P	- prettyprinted source code sent to ".sfl" files instead of a
#	  normal listing
# -Q	- quick, do not read .xsf3rc files
# -R	- reprocess everything, even if ".f" or ".o" files are newer
#	  than ".sf3" files
# -V	- verbose; show command lines
# -W:nn	- set ".sfl" file line width to nn (72..120)
#
# All other options are passed through verbatim to the Fortran
# compiler which may in turn send them to the linker.
#
# Commonly-used options can be set in an environment variable,
# SF3FLAGS, or in a local initialization file, ./.xsf3rc, or if
# not found, in a file in the login directory, $HOME/.xsf3rc.
#
# If both an initialization file and an environment variable are
# specified, the environment variable switches will be presented
# to sf3 first, overriding the same switches from the startup
# file.  Similarly, command line switches override ones from
# both of these sources.
#
# If a native Fortran compiler option conflicts with one of the
# above, it can be supplied inside a quoted string argument with
# a leading backslash or a leading brace pair.  E.g. "{}-N" and
# "\-N" will be passed to Fortran as -N, instead of to sf3.
# [14-Aug-1991]
#=======================================================================

__OTHERARGS__=
__READ_RC__=1
__SF3__=/usr/local/plot79/sf3
__SF3_FILES__=
__SF3_ONLY__=0
__VERBOSE__=0

# Find default compiler name (Sun, HP/Apollo, Stardent, IBM RS/RT/3090 AIX ...)

__COMPILERS__="/opt/SUNWspro/bin/f77 /usr/lang/f77 /bin/f77 /usr/bin/f77 /usr/local/f77 /bin/fc /usr/bin/fvs /usr/bin/xlf"

for __FC__ in  $__COMPILERS__ __NO_FORTRAN_COMPILER__
do
	if [ -f $__FC__ ]
	then
		break
	fi
done

if [ $__FC__ = __NO_FORTRAN_COMPILER__ ]
then
	echo "No Fortran compiler: expected one of $__COMPILERS__"
	exit 1
fi

# Separate sf3 arguments from all others
for arg in $*
do
	case $arg in
	-[DHKLNPR]|-I:*|-M:*)
#	-[CDHKLNPR]|-I:*|-M:*) # disable -C to avoid conflict with f77 -C
		__SF3_FLAGS__="$__SF3_FLAGS__ $arg" ;;
	-E)
		__SF3_ONLY__=1 ;;
	-Q)
		__READ_RC__=0 ;;
	-V)
		__VERBOSE__=1 ;;
	*.sf3)
		__SF3_FILES__="$__SF3_FILES__ $arg"
		__OTHERARGS__="$__OTHERARGS__ `basename $arg .sf3`.f" ;;
	*)
		__OTHERARGS__="$__OTHERARGS__ $arg" ;;
	esac
done

# FFLAGS and SF3FLAGS may be set from the environment
__FFLAGS__=${FFLAGS-}

# Append environment variable switches
__SF3_FLAGS__="$__SF3_FLAGS__ ${SF3FLAGS-}"

# Append initialization file switches
if [ $__READ_RC__ -eq 1 ]
then
	# Pick up default arguments, if any
	for OPTFILE in ./.xsf3rc $HOME/.xsf3rc
	do
		# option file must exist and be readable to use it here
		if [ -f $OPTFILE -a -r $OPTFILE ]
		then
			__SF3_FLAGS__="$__SF3_FLAGS__ `cat $OPTFILE`"
			break
		fi
	done
fi

if [ $__VERBOSE__ -eq 1 ]
then
	echo $__SF3__ $__SF3_FLAGS__ $__SF3_FILES__
fi
$__SF3__ $__SF3_FLAGS__ $__SF3_FILES__
status=$?

if [ $status -ne 0 ]
then
	exit $status
fi

if [ $__SF3_ONLY__ -ne 1 ]
then
	if [ $__VERBOSE__ -eq 1 ]
	then
		echo $__FC__ $__FFLAGS__ $__OTHERARGS__
	fi
	$__FC__ $__FFLAGS__ $__OTHERARGS__
fi
