#!/bin/sh
# Given the name of a C source file generated by the Mercury compiler,
# and a count of the number of the number of modules in it (say N),
# generate N+1 files named $filename.part.{0,1,...N}.
#
# $filename.part.N will contain the module initialization stuff from the
# of the source file; the other files will contain a module each. Any stuff
# before the first module will be in $filename.part.0.
#
# Since divide can take a long time, it prints messages saying which part
# it is up to.

if test $# != 2
then
	echo "Usage: divide filename module_count"
	exit 1
fi

TERMCAP=/etc/termcap; export TERMCAP
cp $1 tmp

i=0
while test $i -lt $2
do
	ed - tmp > /dev/null << END
	/^MR_END_MODULE/
	1,.w $1.part.$i
	1,.d
	w
	q
END
	echo done part $i
	i=`expr $i + 1`
done
mv tmp $1.part.$i
echo done final part $i
