#!/bin/sh
PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin
s=/tmp/stderr_$$
t=/tmp/f77_$$.o
CC=${CC_f2c:-'/usr/bin/cc'}
EFL=${EFL:-/v/bin/efl}
EFLFLAGS=${EFLFLAGS:-'system=portable deltastno=10'}
RATFOR=${RATFOR:-/usr/bin/ratfor}
RFLAGS=${RFLAGS:-'-6&'}
F2C=${F2C:-/usr/bin/f2c}
F2CFLAGS=${F2CFLAGS:='-ARw8 -Nn802'}
rc=0
trap "rm -f $s $t; exit \$rc" 0
lib=/lib/num/lib.lo
OUTF=a.out
OFILE=""
cOPT=1
set -- `getopt 6CD:HI:O:N:Scgm:o:puw "$@"`
case $? in 0);; *) exit 1;; esac
CCFLAGS=
GFLAG=
while
	test X"$1" != X--
do
	# If an optional argument is omitted, getopt skip parsing
	# the string on the command line.  So we call it again before
	# parsing each option.
	set -- `getopt 6CD:HI:O:N:Scgm:o:puw "$@"`

	case "$1"
	in
	-C)	CFLAGS="$CFLAGS -C"
		CCFLAGS="$CCFLAGS -C"
		shift;;

	-D)	CCFLAGS="$CCFLAGS -D$2"
		shift 2;;

	-H)	cat <<__END_OF_USAGE__
usage:	$0 [-g] [-O] [-o absfile] [-c] files [-l library]
	-C		Check that subscripts are in bounds.
	-H		Print out this help message.
	-I includepath	passed to C compiler (for .c files)
	-Ntnnn		allow nnn entries in table t
	-S		leave assembler output on file.s
	-c		Do not call linker, leave relocatables in *.o.
	-g		generate debugging output and save .c output.
	-l library	(passed to ld).
	-m486		optimize instruction scheduling for a 486 CPU
	-o objfile	Override default executable name a.out.
	-u		complain about undeclared variables
	-w		omit all warning messages
	-w66		omit Fortran 66 compatibility warning messages
	files		FORTRAN source files ending in .f .
			C source files ending in .c .
			Assembly language files ending in .s .
			efl source files ending in .e .
			RATFOR files ending in .r .
__END_OF_USAGE__
		shift;;

	-I)	CCFLAGS="$CCFLAGS -I$2"
		shift 2;;

	-O)	case $2 in
		  -1) O=-O1;; -2) O=-O2;; -3) O=-O3;;
		  1) O=-O1;; 2) O=-O2;; 3) O=-O3;;
		  *) O=-O;; esac
		case $O in -O?) shift;; *);; esac
		shift;;

	-N)	F2CFLAGS="$F2CFLAGS $1""$2"
		shift 2;;

	-S)	CFLAGS="$CFLAGS -S"
		cOPT=0
		shift;;

	-c)	cOPT=0
		shift;;

	-g)	CFLAGS="$CFLAGS -g"
		CCFLAGS="$CCFLAGS -g"
		F2CFLAGS="$F2CFLAGS -g"
		# iff GFLAG non-null, retain intermediate .c files
		GFLAG=1
		shift;;

	-m)	case $2 in
		  486) CCFLAGS="$CCFLAGS -m486"; shift;;
		esac
		shift;;

	-o)	OFILE=$2
		OUTF=$2
		shift 2;;

	-p)	CFLAGS="$CFLAGS -p"
		CCFLAGS="$CCFLAGS -p"
		shift;;

	-u)	F2CFLAGS="$F2CFLAGS -u"
		shift;;

	-w)	F2CFLAGS="$F2CFLAGS -w"
		case $2 in -6) F2CFLAGS="$F2CFLAGS"66; shift
			case $2 in -6) shift;; esac;; esac
		shift;;

	*)	echo "invalid parameter $1" 1>&2
		shift;;
	esac
done
shift

while
	test -n "$1"
do
	case "$1"
	in
	*.[fF])
		case "$1" in *.f) f=".f";; *.F) f=".F";; esac
		b=`basename $1 $f`
		if [ X"$cOPT" != X0 -o X"$OFILE" = X ]; then
			OFILE=$b.o
		fi
		$F2C $F2CFLAGS $1
		case $? in 0);; *) exit;; esac
		$CC -c $CFLAGS $b.c -o $OFILE 2>$s
		rc=$?
		sed '/parameter .* is not referenced/d;/warning: too many parameters/d' $s 1>&2
		case $rc in 0);; *) exit;; esac
		OFILES="$OFILES $OFILE"
		if [ -z "$GFLAG" ]; then
			rm $b.c
		fi 
		case $cOPT in 1) cOPT=2;; esac
		shift;;
	*.e)
		b=`basename $1 .e`
		if [ X"$cOPT" != X0 -o X"$OFILE" = X ]; then
			OFILE=$b.o
		fi
		$EFL $EFLFLAGS $1 >$b.f
		case $? in 0);; *) exit;; esac
		$F2C $F2CFLAGS $b.f
		case $? in 0);; *) exit;; esac
                $CC -c $CFLAGS $b.c -o $OFILE
		case $? in 0);; *) exit;; esac
		OFILES="$OFILES $OFILE"
		rm $b.[cf]
		case $cOPT in 1) cOPT=2;; esac
		shift;;
	*.r)
		b=`basename $1 .r`
		if [ X"$cOPT" != X0 -o X"$OFILE" = X ]; then
			OFILE=$b.o
		fi
		$RATFOR $RFLAGS $1 >$b.f
		case $? in 0);; *) exit;; esac
		$F2C $F2CFLAGS $b.f
		case $? in 0);; *) exit;; esac
		$CC -c $CFLAGS $b.c -o $OFILE
		case $? in 0);; *) exit;; esac
		OFILES="$OFILES $OFILE"
		rm $b.[cf]
		case $cOPT in 1) cOPT=2;; esac
		shift;;
	*.s)
		echo $1: 1>&2
		if [ X"$cOPT" != X0 -o X"$OFILE" = X ]; then
			OFILE=`basename $1 .s`.o
		fi
		${AS:-/usr/bin/as} -o $OFILE $AFLAGS $1
		case $? in 0);; *) exit;; esac
		OFILES="$OFILES $OFILE"
		case $cOPT in 1) cOPT=2;; esac
		shift;;
	*.c)
		echo $1: 1>&2
		if [ X"$cOPT" != X0 -o X"$OFILE" = X ]; then
			OFILE=`basename $1 .c`.o
		fi
                $CC -c $CFLAGS $CCFLAGS $1 -o $OFILE
		rc=$?; case $rc in 0);; *) exit;; esac
		OFILES="$OFILES $OFILE"
		case $cOPT in 1) cOPT=2;; esac
		shift;;
	*.o)
		OFILES="$OFILES $1"
		case $cOPT in 1) cOPT=2;; esac
		shift;;
	-l)
		OFILES="$OFILES -l$2"
		shift 2
		case $cOPT in 1) cOPT=2;; esac
		;;
	-l*)
		OFILES="$OFILES $1"
		shift
		case $cOPT in 1) cOPT=2;; esac
		;;
	-o)
		OUTF=$2; shift 2;;
	*)
		OFILES="$OFILES $1"
		shift
		case $cOPT in 1) cOPT=2;; esac
		;;
	esac
done

case $cOPT in 2) $CC -o $OUTF -u MAIN__ $OFILES -lf2c -lm;; esac
rc=$?
exit $rc
