#!/bin/bash
# PATH=/bin:/usr/bin:/usr/local/bin:/usr/lang/
# f77-style shell script to compile and load fortran, C, and assembly codes

#	usage:	cppFC [-g] [-O] [-o absfile] [-c] files [-l library]

#		-o objfile	Override default executable name a.out.

#		-c		Do not call linker, leave relocatables in *.o.

#		-S		leave assembler output on file.s

#		-l library	(passed to ld).

#		-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 .

#		-D def		passed to C compiler (for .c files)
#                                or cpp for .f files
#		-I includepath	passed to C compiler (for .c files)

#		-Ntnnn		allow nnn entries in table t
##   echo "  cppFC: nparam=$# $*"
s=/tmp/stderr_$$
t=/tmp/f77_$$.o
#FC=${F77:-f77}


source $EPICSTOP/Scrpt/setarch.sh
Fmsg=w
now=`pwd`
# we always use Cosmos/lib/$ARCH for .mod file; two -module cannot be
#  given (?; as of 2010/May)
FC="f90jx -X9  -f$Fmsg -Am -M$COSMOSTOP/lib/$ARCH"
CC=fcc
# FFLAGS=
EFL=${EFL:-/v/bin/efl}
EFLFLAGS=${EFLFLAGS:-'system=portable deltastno=10'}
CPPFLAGS=
rc=0
keeptemp=0
trap "rm -f $s $t; exit \$rc" 0
OUTF=a.out
cOPT=1
CCFLAGS=

while
	test -n "$1"
do
	case "$1"
	in
	-c)	cOPT=0
		shift
		;;

	-D*)	CCFLAGS="$CCFLAGS $1"
	        CPPFLAGS="$CPPFLAGS $1"
		shift 1
		;;

	-g)	FFLAGS="$FFLAGS -g"
		keeptemp=1
		shift;;

        +z)      FFLAGS="$FFLAGS +z"
	        shift
		;;

       +U77)    FFLAGS="$FFLAGS +U77"
	        shift
		;;
   
	-I*)	CPPFLAGS="$CPPFLAGS $1"
		shift
		;;

	-arch)	FFLAGS="$FFLAGS -arch $2"
		shift 2
		;;

	-o)	OUTF=$2
		shift 2
		;;

	-O)	FFLAGS="$FFLAGS -O"
		shift
		;;

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

	-w)	FFLAGS="$FFLAGS -w"
#		case $2 in -6) CPPFLAGS="$CPPFLAGS"66; shift
#			case $2 in -6) shift;; esac;; esac
		shift
		;;

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

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

	*.[fF])
		case "$1" in *.f) f=".f";; *.F) f=".F";; esac
		b=`basename $1 $f`
#		$F2C $F2CFLAGS $1
#	        /lib/cpp  -I$TOPDIR  $CPPFLAGS $1 > /tmp/temp$$.f
#                             awk is for absoft fortran
#	        /lib/cpp -C -P  $CPPFLAGS $1 | awk 'substr($0,1,1) != "#" {print}' -  > /tmp/temp$$.f




	        /lib/cpp -C -P   $CPPFLAGS $1  > ./temp$$.f


		case $? in 0);; *) exit;; esac
		echo "*********" $FC
		if [ $1 == "epSetPhotoE.f" ]; then 
		    echo "program is epSetPhotoE.f: -g is added to the above command" 
		    $FC  -c  -g  $FFLAGS ./temp$$.f -o $b.o > $s
		else
		    $FC  -c   $FFLAGS ./temp$$.f -o $b.o > $s
		fi

	        rm -f temp$$.d
		rc=$?
#		sed '/parameter .* is not referenced/d;/warning: too many parameters/d' $s 1>&2
		case $rc in 0);; *) exit;; esac
		OFILES="$OFILES $b.o"

		if [ $keeptemp = 0 ] ;	then
                  rm ./temp$$.f
		fi
		case $cOPT in 1) cOPT=2;; esac
		shift
		;;
	*.f90)
		f=".f90"
		b=`basename $1 $f`

	        /lib/cpp -C -P  $CPPFLAGS $1  > ./temp$$.f90

		case $? in 0);; *) exit;; esac
		echo "*********" $FC
                $FC  -c   $FFLAGS ./temp$$.f90 -o $b.o > $s

	        rm -f temp$$.d
		rc=$?
		case $rc in 0);; *) exit;; esac
		OFILES="$OFILES $b.o"
#		rm $b.c
		if [ $keeptemp = 0 ] ;	then
                  rm ./temp$$.f90
		fi
#		rm /tmp/temp$$.f
		case $cOPT in 1) cOPT=2;; esac
		shift
		;;

	*.c)
		echo $1: 1>&2
		OFILE=`basename $1 .c`.o
                $CC -c $CFLAGS $CCFLAGS $1
		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) $FC -o $OUTF -u MAIN__ $OFILES -lf2c -lm;; esac
rc=$?
exit $rc
