#! /bin/sh
test -f ./init-env && . ./init-env

# Test for sanity checks.

trap 'rm -fr $tmpfiles' 1 2 3 15

: ${AUTOCONF=autoconf}
${AUTOCONF} --version >/dev/null 2>/dev/null \
  || { echo "Skipping test: autoconf not found"; exit 77; }

gettext_datadir=$(realpath $top_builddir/misc)
export gettext_datadir

tmpdir=$(TMPDIR="$PWD" mktemp -d)
tmpfiles="$tmpfiles $tmpdir"
cd "$tmpdir" || exit 1
test "`echo *`" = "*" || exit 1

cat <<\EOF >configure.ac
AC_PREREQ([2.60])
EOF

${AUTOCONF} >/dev/null 2>/dev/null \
  || { echo "Skipping test: autoconf version too old"; exit 77; }

rm -f configure.ac

# Check if sanity checks are actually working.

# no configure.ac
$gettext_datadir/autopoint 2>&1 | grep 'Missing configure.in or configure.ac' 2>&1 >/dev/null \
    || exit 1

test ! -d intl || exit 1
test ! -d m4 || exit 1
test ! -d po || exit 1

# configure.ac without AM_GNU_GETTEXT_VERSION
cat <<\EOF >configure.ac
AC_INIT
AC_CONFIG_SRCDIR(hello.c)

AC_PROG_CC

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
EOF

$gettext_datadir/autopoint 2>&1 | grep 'Missing version' 2>&1 >/dev/null \
    || exit 1

test ! -d intl || exit 1
test ! -d m4 || exit 1
test ! -d po || exit 1

# VERSION specified through intl/VERSION file, but in wrong format
cat <<\EOF >configure.ac
AC_INIT
AC_CONFIG_SRCDIR(hello.c)

AC_PROG_CC

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
EOF

test -d intl || mkdir intl
echo bogus-version > intl/VERSION

$gettext_datadir/autopoint 2>&1 | grep 'Missing version' 2>&1 >/dev/null \
    || exit 1

test ! -d m4 || exit 1
test ! -d po || exit 1

# VERSION specified through intl/VERSION file
cat <<\EOF >configure.ac
AC_INIT
AC_CONFIG_SRCDIR(hello.c)

AC_PROG_CC

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
EOF

test -d intl || mkdir intl

echo gettext-0.15 > intl/VERSION

# For further investigation, autopoint keeps autopoint.diff in $TMPDIR
# if there is a mismatch.  Set TMPDIR not to pollute /tmp.
TMPDIR="$PWD" $gettext_datadir/autopoint 2>&1 | grep 'locally modified' 2>&1 >/dev/null || exit 1

test ! -d m4 || exit 1
test ! -d po || exit 1

echo 'GNU gettext library from gettext-0.15' > intl/VERSION

TMPDIR="$PWD" $gettext_datadir/autopoint 2>&1 || exit 1

cd ..

rm -fr $tmpfiles

exit 0
