#! /bin/sh
# Copyright (C) 2011 Free Software Foundation, Inc.
#
# This program 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, 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, see <http://www.gnu.org/licenses/>.

# Test that we can define a distcheck-hook to diagnose m4 files missing
# from the dist tarball.
# See automake bug#9037.

. ./defs || Exit 1

set -e

cwd=`pwd`

cat > Makefile.am << 'END'
## The lack of `--install' here is meant.
ACLOCAL_AMFLAGS = -I m4

distcheck-hook:
	@fatal () { echo "$@: $$*" >&2; exit 1; }; \
	$(am__cd) $(distdir) && chmod u+w . && mkdir _m4 \
	  || fatal "cannot setup distdir"; \
	$(ACLOCAL) -I _m4 $(ACLOCAL_AMFLAGS) --install --output=_am.m4 \
	  || fatal "cannot regenerate aclocal.m4"; \
	lst=`ls _m4 | tr '\012\015' '  '`; \
	if test -n "$$lst"; then \
	  fatal "required m4 files not distributed: $$lst"; \
	fi; \
	rm -rf _m4 _am.m4 autom4te.cache && chmod a-w . \
	  || fatal "cannot reset distdir"
END

cat >> configure.in << 'END'
AC_OUTPUT
MY_FOO
MY_BAR
MY_BAZ
END

# We need absolute paths in dirlist.
mkdir acdir
cp "$testsrcdir/../m4/"*.m4 acdir
echo "$cwd/dirlist1" > acdir/dirlist
echo "$cwd/dirlist2" >> acdir/dirlist

mkdir m4 dirlist1 dirlist2
echo 'AC_DEFUN([MY_FOO], [:])' > m4/foo.m4
echo 'AC_DEFUN([MY_BAR], [:])' > dirlist1/bar.m4
echo 'AC_DEFUN([MY_BAZ], [:])' > dirlist1/baz.m4
echo 'AC_DEFUN([MY_QUX], [:])' > dirlist2/qux.m4

# FIXME: Gross hacks to work around the limitations of our wrapper scripts.
# FIXME: We should really improve those scripts ...
mkdir bin
cat > bin/aclocal-$APIVERSION <<END
#!/bin/sh
PATH='$PATH'; export PATH
case \$1 in
  -I)
    ACLOCAL_TESTSUITE_FLAGS="-I \$2"; export ACLOCAL_TESTSUITE_FLAGS
    shift; shift; 
    ;;
esac
exec $ACLOCAL --acdir "$cwd/acdir" \${1+"\$@"}
END
chmod a+x bin/aclocal-$APIVERSION
cp bin/aclocal-$APIVERSION bin/aclocal
PATH=$cwd/bin$PATH_SEPARATOR$PATH; export PATH
ACLOCAL=aclocal-$APIVERSION; export ACLOCAL
ACLOCAL_TESTSUITE_FLAGS="-I m4"; export ACLOCAL_TESTSUITE_FLAGS

# We don't use `--install' here.  Our distcheck-hook should catch this.
$ACLOCAL
$AUTOCONF
$EGREP 'MY_(FOO|BAR|BAZ)' configure && Exit 1 # Sanity check.
$AUTOMAKE

./configure

$MAKE distcheck >output 2>&1 && { cat output; Exit 1; }
cat output
$EGREP "required m4 file.*not distributed:.* bar.m4( |$)" output
$EGREP "required m4 file.*not distributed:.* baz.m4( |$)" output
# Check that we don't fail for spurious errors.
$EGREP -i 'mkdir:|autom4te.*\.cache|permission|denied' output && Exit 1

# Now we use `--install', and "make distcheck" should pass.
$ACLOCAL -I m4 --install
test -f m4/bar.m4 # Sanity check.
test -f m4/baz.m4 # Likewise.
using_gmake || $MAKE Makefile
$MAKE distcheck

# We start to use a new "third-party" macro, but forget to re-run
# "aclocal --install" by hand, relying on automatic remake rules.
# Our distcheck-hook should catch this too.
echo MY_QUX >> configure.in

$MAKE
$EGREP 'MY_(FOO|BAR|BAZ|QUX)' configure && Exit 1 # Sanity check.

$MAKE distcheck >output 2>&1 && { cat output; Exit 1; }
cat output
$EGREP "required m4 file.*not distributed:.* qux.m4( |$)" output
# Check that we don't fail for spurious errors.
$EGREP -i 'mkdir:|autom4te.*\.cache|permission|denied' output && Exit 1
# Check that we don't ciomplain for files that should have been found.
grep " ba[rz].m4" output && Exit 1

# Now we again use `--install', and "make distcheck" should pass.
$ACLOCAL -I m4 --install
test -f m4/qux.m4 # Sanity check.
using_gmake || $MAKE Makefile
$MAKE distcheck

:
