#!/bin/sh -e
#
# Script to regenerate a file created by configure. Given a file "foo.in"
# (default Makefile.in) and an old version of Makefile, generate the new version
# of "foo" by substituting accordingly. This is faster than running
# config.status. Note: doesn't work for Makedefs.
#
# Usage: vorp [file]
#   (the file is specified without the .in suffix)
#
if test $# = 0
then
  f=Makefile
else
  f=$1
fi

eval `grep '^TOP *=' Makefile | \
    sed -e 's/ = */="/g' -e 's/$/"/'`
eval `grep '^srcdir *=' Makefile | \
    sed -e 's/ = */="/g' -e 's/$/"/'`

sf=$srcdir/$f.in

if test ! -r $sf -o ! -r Makefile
then
  echo "Cannot re-create $f; either Makefile or $sf is missing"
  exit 1
fi
echo "Re-creating $f" >&2

cat << XXX > /tmp/vorp.1.$$
while (<>) {
    if (/CEOF/) {
	\$seen = \$seen + 1;
    } else {
       if (\$seen == 1) {
	  print \$_;
       }
    }
}
XXX
perl /tmp/vorp.1.$$ $TOP/config.status > /tmp/vorp.2.$$
rm /tmp/vorp.1.$$
(
    echo "# Generated by $0 using Makefile and $sf"
    sed -f /tmp/vorp.2.$$ $sf | sed -e s%@srcdir@%$srcdir%g 
    rm /tmp/vorp.2.$$
) > $f
