msg="Usage: S FUNCTION [options] name file ..."
misc=
mod=
case $# in 0) echo "$msg"; exit 1;; esac
if test ! -d x; then echo Current directory is not an S chapter; exit 1; fi
lib='$(LIBR)'
case $1 in -*) mod=$1; shift;; esac
case $mod in -*g*|-*h*) lib='$(GRZ)';; esac
fun=$1
shift
case $# in 0) echo "$msg"; exit 1;; esac
case $fun in *.[cCifre]) echo "$msg"; exit 1;; esac
touch /tmp/t1.$$
for i in $@
do
	case $i in
	*.[irfcCe]) echo "$i" >>/tmp/t1.$$
		;;
	*) misc="$misc $i"
		;;
	esac
done
comp=`sed </tmp/t1.$$ "s/\.[rfcCe]$/.o/
s/\.i$/.x/"`

ed - Smakefile <<!
g/^$fun:/.;/echo/d
g/^i.$fun.C:/d
w
!
rm -f i.$fun.C >/dev/null 2>&1	# get rid of old .C file if present
echo "i.$fun.C: ; echo 'INCLUDE(u/cinter)CINTER($fun,$mod)' >i.$fun.C" >>Smakefile
cat >>Smakefile <<!
$fun: i.$fun.o `echo $comp` # $mod
	\$(F77) \$(LDFLAGS) \$(STRIP) -o x/$fun i.$fun.o `echo $comp` $misc $lib
	@touch $fun
	@echo $fun loaded
!
case $mod in -*d*)
	if test ! -d ${fun}source
		then mkdir ${fun}source; echo Source files for device driver go in directory ${fun}source
	fi
	cat >>Smakefile <<!
dev.$fun:
	cd ${fun}source; S COMPILEALL
	\$(F77) \$(LDFLAGS) \$(STRIP) -o x/dev.$fun \$L/device.o \$L/defer.o ${fun}source/*.o \$(GRZLIB)
	@echo $fun driver loaded
!
	echo For device drivers, you must MAKE both $fun and dev.$fun
	;;
esac
if test ! -s .help/$fun
	then echo >.help/$fun "No documentation written yet for $fun"
fi
echo $fun initialized.
rm /tmp/t1.$$
