#!/bin/sh
#
# /usr/local/src/cgl/cglf
#
#  Used to preprocess, compile and link CFT programs
#  to CGL (common graphics library)
#
TMP="/tmp/"
CGL_INC="/usr/include/cgl"
F77=/usr/convex/fc
FFLAGS="-c -O2"
#
case $# in
0) echo Usage:  cglf file1  \(file1 must end with a .f suffix\); exit;;
*);;
esac
#
#
#
EXEC=`expr $1  : '\(.*\).f'`
for i in $*
do
	case $i in
	*.f) if test -r $i 
		then
			i=`expr $i : '\(.*\).f'`
		else
			echo "Can't find $i"
			exit 
	     fi;;
	*)   echo Input file must end with a .f suffix; exit
	esac
#
#
# Change all the 'INCLUDE ...' statements to '#includes "..." '
#
#          Angle brackets, i.e. <..> are changed to ".." only if
#            the file is an iris file (fget.h, fdevice.h, fgl.h) 
#
# Replace tabs by 7 spaces           
#
#
	sed -e 's,^[ 	]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee] *",#include ",' \
	    -e "s,^[ 	]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee] *<,#include <," \
	    -e "s,^[ 	]*[Ii][Nn][Cc][Ll][Uu][Dd][Ee] *'\(.*\)',#include \"\1\"," \
	    -e "s,\(^#include\) < *\(fget.h\) *>,\1 \"2\","\
	    -e "s,\(^#include\) < *\(fdevice.h\) *>,\1 \"\2\","\
	    -e "s,\(^#include\) < *\(fgl.h\) *>,\1 \"2\","\
	    -e 's/	/       /g'\
           	${i}.f  >${TMP}cgl$$.c
#
	echo \* Sed processing completed \*
#
#
# Run this through the C preprocessor....ugly
# delete temp files
#
	if cc -E -I${CGL_INC} ${TMP}cgl$$.c  > ${TMP}cgl$$.e 
	then
		rm -f ${TMP}cgl$$.c
	else
		rm -f ${TMP}cgl$$.e
		rm -f ${TMP}cgl$$.c
		exit
	fi
	echo \* Include Preprocessing completed \*
#
#
# delete # stuff left by the C pp
#
	egrep -v "^#.*|^[ 	]*$" ${TMP}cgl$$.e > $$.f  || exit 
	rm -f ${TMP}cgl$$.e
	echo \* Stripped Cpp junk \*
#
# Run this through the CFT compiler
# delete temp files 
#
	if ${F77} ${FFLAGS} $$.f
	then
		mv $$.o ${i}.o
		rm $$.f
	else
		rm $$.o $$.f
		exit
	fi
done
#
# Link this to the CFT version of CGL
#
OBJS=`echo $* ^ sed -e 's/\.f/\.o/g' `
${F77} -o $EXEC $OBJS -I${CGL_INC} /usr/local/lib/libcgl.a -lm
#
#
#
