#! /bin/sh

##  boostrap file for vls -- Sam Hocevar <sam@zoy.org>
##  $Id: bootstrap,v 1.7 2003/10/28 00:14:12 sam Exp $

set -x
set -e

# Get a sane environment, just in case
LANG=C
export LANG
CYGWIN=binmode
export CYGWIN

# Check for automake
amvers="no"
if 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
ltvers="no"
if libtoolize --version >/dev/null 2>&1; then
  ltvers="`libtoolize --version | sed -e '1s/[^0-9]*//' -e q`"
  libtoolize="libtoolize"
elif glibtoolize --version >/dev/null 2>&1; then
  ltvers="`glibtoolize --version | sed -e '1s/[^0-9]*//' -e q`"
  libtoolize="glibtoolize"
fi

if expr "$ltvers" "<" "1.4" >/dev/null 2>&1; then
  ltvers="no"
fi

if test "$ltvers" = "no"; then
  set +x
  echo "$0: you need libtool version 1.4 or later"
  exit 1
fi

# Remove old cruft
rm -f aclocal.m4 configure config.guess config.log config.sub config.cache config.h.in config.h compile ltmain.sh libtool ltconfig missing mkinstalldirs depcomp install-sh INSTALL
(cd autotools && rm -f config.guess config.sub missing mkinstalldirs compile ltmain.sh depcomp install-sh)
rm -f src/core/defs.h.in src/core/defs.h

aclocal${amvers}
${libtoolize} --copy --force

# Workaround for a minor libtool bug
if test -f "ltmain.sh"; then
  mv ltmain.sh autotools/
fi

# Workaround for the -static flag that gets ignored
rm -f autotools/ltmain.sh.bak
sed -e '/build_old_libs=yes/q' < autotools/ltmain.sh > autotools/ltmain.sh.bak
echo 'build_libtool_libs=no' >> autotools/ltmain.sh.bak
sed -e '1,/build_old_libs=yes/d' < autotools/ltmain.sh >> autotools/ltmain.sh.bak
rm -f autotools/ltmain.sh && mv autotools/ltmain.sh.bak autotools/ltmain.sh

if expr "$ltvers" "<" "1.5" >/dev/null 2>&1; then
  # Workaround for libtool 1.4 which doesn't support C++
  rm -f aclocal.m4.bak
  sed -e '/^CC=.lt_CC/q' < aclocal.m4 > aclocal.m4.bak
  echo 'CXX=$CXX' >> aclocal.m4.bak
  sed -e '1,/^CC=.lt_CC/d' < aclocal.m4 >> aclocal.m4.bak
  rm -f aclocal.m4 && mv aclocal.m4.bak aclocal.m4
  rm -f autotools/ltmain.sh.bak
  sed -e '/link.*|.*relink.*)/q' < autotools/ltmain.sh > autotools/ltmain.sh.bak
  echo 'case $nonopt in *++*) CC=$CXX;; esac' >> autotools/ltmain.sh.bak
  sed -e '1,/link.*|.*relink.*)/d' < autotools/ltmain.sh >> autotools/ltmain.sh.bak
  rm -f autotools/ltmain.sh && mv autotools/ltmain.sh.bak autotools/ltmain.sh
elif expr "$ltvers" "<" "1.6" >/dev/null 2>&1; then
  # Workaround for http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=217834
  rm -f autotools/ltmain.sh.bak
  sed -e "s,/.# ### BEGIN,/^\\\\(build_libtool\\\\|build_old\\\\|prefer_static\\\\)_libs/d' -e '&," < autotools/ltmain.sh > autotools/ltmain.sh.bak
  rm -f autotools/ltmain.sh && mv autotools/ltmain.sh.bak autotools/ltmain.sh
fi

autoconf
autoheader
automake${amvers} --add-missing --copy

