#!/bin/sh
#
# Run this script after the first cvs checkout to build
# makefiles and friends

vcheck (){
  perl <<PERL
@t = split /\./, "$1";
@v = map { int \$_ } split /\./, (split /\s+/, \`$2\`)[3];
print "$2 = ", (join ".",@v), "  (expected $1)\n";
\$v = \$t[0]*1000000+\$t[1]*1000+\$t[2] <= \$v[0]*1000000+\$v[1]*1000+\$v[2];
exit \$v
PERL
}

ERROR=0

if vcheck 1.5.14 "libtool --version"
then
  echo "get a copy of GNU libtool >= 1.5.14"
  ERROR=1
fi

if vcheck 1.9.5  "automake  --version"
then
  if vcheck 1.9.5  "automake-1.9 --version"
  then
    echo "get a copy of GNU automake >= 1.9.5"
    ERROR=1
  else
    automake=automake-1.9
    aclocal="aclocal-1.9  --acdir=/usr/share/aclocal-1.9 -I /usr/share/aclocal/"
  fi
else
    automake="automake"
    aclocal="aclocal"
#    aclocal="aclocal -I /usr/pack/libtool-1.5.14-to/share/aclocal"
fi

if vcheck 2.59 "autoconf --version"
then
  echo "get a copy of GNU autoconf >= 2.59"
  ERROR=1
fi

if [ $ERROR -ne 0 ]
then
  exit 1
fi

# cleanup
set -x
find . -name Makefile | xargs rm -f _UNKNONW_
find . -name "*.la" | xargs rm -f
find . -name Makefile.in | xargs rm -f _UNKNONW_
find . -name .libs | xargs rm -r 
find . -name .debs | xargs rm -r

$aclocal
libtoolize --copy --force
autoheader --warnings=all --force  
$aclocal
$automake --foreign --add-missing --force-missing --copy --warnings=all
autoconf --warnings=all --force
# one again to make every body happy
autoreconf
