#! /bin/sh

# Copyright 1998 Alexandre Oliva <oliva@dcc.unicamp.br>

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# This script expects two or three command line arguments, namely,
# THAT, THIS and STAMP.  It will create a file named THIS, containing
# the line
#         #include "$THAT" 
# if THAT exists, or an empty line otherwise.

# If THIS already exists, and it contains the intended contents, its
# timestamp will not be updated; only the timestamp of the STAMP will.

# This script is useful for Make rules like this:
#   foo.c: stamp-h01
#	@:
#   stamp-h01: $(top_builddir)/config.status
#	$(SHELL) $(top_srcdir)/regen-forward $(mysrcdir)/myfoo.c foo.c $@
# or this:
#   bar.c:
#	$(SHELL) $(top_srcdir)/regen-forward $(mysrcdir)/mybar.c bar.c

# If STAMP is missing, regen-forward will always be updated.  This is
# useful if you don't have automatic dependency tracking, and want the
# local file to be updated every time THAT is updated, like in the
# second rule above.  Note, however, that if bar.c happens to exist,
# Make won't even run regen-forward.  This mechanism should only be
# used if THAT is very unlikely to change.

# A temporary file called STAMP-new (or THIS-new, if no STAMP is
# provided) will be created.

# Note that #include forwarders are quite different from plain links,
# because, if the included file includes any other file named within
# quotes (as opposed to angle brackets), the search for the included
# file will start at the directory of the included file.  Whether this
# is good or not depends on the use you make of it.

# This script could be extended so as to support other linking
# semantics (like soft-link, hard-link and copying, where none are
# available).  It could be incorporated in automake's `missing'
# script, so as to ease portable linking.

if test $# != 2 && test $# != 3; then
    echo 'usage: THAT THIS [STAMP]' >&2
    exit 1
fi

that="$1"
this="$2"
if test $# = 3; then
    stamp="$3"
else
    stamp="$2"
fi

trap "rm -f \"$stamp-new\"; exit 1" 1 2 15

rm -f "$stamp-new"
if test -f "$that"; then
    echo "#include \"$that\"" > "$stamp-new" || exit 1
else
    echo > "$stamp-new" || exit 1
fi

if cmp -s "$stamp-new" "$this" 2>/dev/null && test "$stamp" != "$this"; then
    echo $this is already up-to-date >&2
else
    rm -f "$this" || exit 1
    cp "$stamp-new" "$this" || exit 1
fi

rm -f "$stamp" || exit 1
mv "$stamp-new" "$stamp" || exit 1

exit 0
