#!/bin/sh
#
echo "Generating build information..."

# Touch the timestamps on all the files since CVS messes them up
directory=`dirname $0`
touch $directory/configure.in

# Debian allows to have several different versions in the system
use_autoconf=autoconf
use_autoheader=autoheader
( which autoconf2.50 | grep "^/" >/dev/null ) && use_autoconf=autoconf2.50
( which autoheader2.50 | grep "^/" >/dev/null  ) && use_autoheader=autoheader2.50

DIE=0

($use_autoconf --version) < /dev/null > /dev/null 2>&1 || {
  echo
  echo "**Error**: You must have \`autoconf' installed ."
  DIE=1
}

(automake --version) < /dev/null > /dev/null 2>&1 || {
  echo
  echo "**Error**: You must have \`automake' installed."
  DIE=1
  NO_AUTOMAKE=yes
}

(aclocal --version) < /dev/null > /dev/null 2>&1 || {
  echo
  echo "**Error**: Missing \`aclocal'."
  DIE=1
}

(libtool --version) < /dev/null > /dev/null 2>&1 || {
  echo
  echo "**Error**: You must have \`libtool' installed."
  DIE=1
}


if [ -d /usr/local/share/aclocal ]; then
  if [ "`aclocal --print-ac-dir`" != "/usr/local/share/aclocal" ]; then
    ACLOCAL_FLAGS="-I /usr/local/share/aclocal $ACLOCAL_FLAGS"
  fi
fi

# Regenerate configuration files
ok=0

rm -f configure ltmain.sh config.guess condig.sub

echo "Running libtoolize..."
libtoolize --force --copy >/dev/null || ok=1
echo "Running aclocal..."
aclocal $ACLOCAL_FLAGS || ok=1
# echo "Running autoupdate..."
# autoupdate || ok=1
echo "Running autoheader..."
$use_autoheader || ok=1
echo "Running autoconf..."
$use_autoconf || ok=1
echo "Running automake..."
automake --add-missing --copy --foreign || ok=1

if [ "${ok}" -eq 0 ]; then
	echo "Now you are ready to run ./configure"
    else
	echo "Problems detected. Please investigate."
        echo "If generated 'configure' script is useless try following:"
        echo "   rm -f configure ltmain.sh config.guess condig.sub"
	echo "   cvs update"
        echo "   automake"
        echo "   configure"
fi
