#!/bin/sh
#
# Copyright 2004-2005 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# buildconf: Build the support scripts needed to compile from a
#            checked-out version of the source code.

perl="perl"
libtool="libtool"
aclocal="aclocal"
automake="automake"
autoconf="autoconf"
autoheader="autoheader"


while test $# -gt 0 
do
  # Normalize
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$1" in
  --with-perl=*)
  perl=$optarg
  ;;
  --with-libtool=*)
  libtool=$optarg
  ;;
  --with-aclocal=*)
  aclocal=$optarg
  ;;
  --with-automake=*)
  automake=$optarg
  ;;
  --with-autoconf=*)
  autoconf=$optarg
  ;;
  --with-autoheader=*)
  autoheader=$optarg
  ;;
  esac

  shift
done

libtoolize="${libtool}ize --automake --copy --force"

echo "removing stale config files"
touch .deps
rm -f aclocal.m4
rm -f generated_lists
rm -rf autom4te*.cache

echo "checking buildconf prereqs";
$perl build/version_check.pl libtool $libtool  || exit 1
$perl build/version_check.pl autoconf $autoconf  || exit 1
$perl build/version_check.pl automake $automake  || exit 1
$perl build/version_check.pl ExtUtils::XSBuilder || exit 1
#$perl build/version_check.pl Test::Inline || exit 1
PREREQ_PM=`$perl build/version_check.pl perl_prereqs`

echo "running $libtoolize" && $libtoolize && \
echo "running $aclocal"  && $aclocal && \
echo "running $autoconf" && $autoconf && \
echo "running $autoheader" && $autoheader && \
echo "running $automake -a -c" && $automake -a -c && \
echo "running xsbuilder" && (cd glue/perl && $perl ../../build/xsbuilder.pl) && \
echo "running cpan prereq fixups" && $perl -i -wpe "s/PREREQ_PM/$PREREQ_PM/" Makefile.in && \
echo "buildconf successful."

