#! /bin/sh
set -x
set -e

# Check for automake
amvers="no"
if automake-1.11 --version >/dev/null 2>&1; then
  amvers="-1.11"
elif automake-1.10 --version >/dev/null 2>&1; then
  amvers="-1.10"
elif automake-1.9 --version >/dev/null 2>&1; then
  amvers="-1.9"
elif automake-1.8 --version >/dev/null 2>&1; then
  amvers="-1.8"
elif automake-1.7 --version >/dev/null 2>&1; then
  amvers="-1.7"
elif automake-1.6 --version >/dev/null 2>&1; then
  amvers="-1.6"
elif automake-1.5 --version >/dev/null 2>&1; then
  amvers="-1.5"
elif automake --version > /dev/null 2>&1; then
  amvers="`automake --version | sed -e '1s/[^0-9]*//' -e q`"
  if expr "$amvers" "<" "1.5" > /dev/null 2>&1; then
    amvers="no"
  else
    amvers=""
  fi
fi

if test "$amvers" = "no"; then
  set +x
  echo "$0: you need automake version 1.5 or later"
  exit 1
fi

# Check for libtool
libtoolize="no"
if glibtoolize --version >/dev/null 2>&1; then
  libtoolize="glibtoolize"
elif libtoolize --version >/dev/null 2>&1; then
  libtoolize="libtoolize"
fi

if test "$libtoolize" = "no"; then
  set +x
  echo "$0: you need libtool"
  exit 1
fi

# Remove old cruft
set +x; for x in aclocal.m4 configure config.guess config.log config.sub config.cache config.h.in config.h compile libtool.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 ltmain.sh libtool ltconfig missing mkinstalldirs depcomp install-sh; do rm -f $x autotools/$x; done; rm -Rf autom4te.cache; set -x
if test ! -d autotools; then mkdir autotools; fi

# Bootstrap package
${libtoolize} --copy --force
if test -f "ltmain.sh"; then
  echo "$0: working around a minor libtool issue"
  mv ltmain.sh autotools/
fi

aclocal${amvers} -I autotools
autoconf
autoheader
#add --include-deps if you want to bootstrap with any other compiler than gcc
#automake${amvers} --add-missing --copy --include-deps
automake${amvers} --add-missing --copy
