#!/bin/sh
# MkProto v1.1  (c) 7.7.92 by Andreas Ley  (u) 16.12.93
# Adds prototypes for the given *.c files to the appropriate *.h files

if test $# -eq 0; then
	echo "Usage: `basename $0` filenames" >&2
	exit 1
fi

prototype()
{
	echo
	echo "/* Functions from $2 */"
	echo
	egrep '^[^#(	]+\(.*\)$' $1 | sed 's/(.*)/();/;s/^/extern /'
}

tmpname=/tmp/`basename $0`$$
trap "rm -f ${tmpname}; exit 0" 0 1 2 15

touch=""
if test "$1" = "touch"; then
	touch=true
	shift
fi

for file in $*; do
	cfile=`basename ${file}`
	hfile=`dirname ${file}`/`basename ${file} .c`.h
	(if test -f ${hfile}; then
		if grep "^\/\* Functions from ${cfile} \*\/\$" ${hfile} >/dev/null; then
			sed "/^\/\* Functions from ${cfile} \*\/\$/,\$d" ${hfile}
		else
			cat ${hfile}
			echo
		fi
	fi
	prototype ${file} ${cfile} | sed '1d') >${tmpname} || exit 1
	test -z "${touch}" -a -f ${hfile} && cmp ${tmpname} ${hfile} 2>/dev/null && break
	mv ${tmpname} ${hfile}
done

exit 0
