#! /bin/sh

prefix='@prefix@'
phpdir="$prefix/lib/php/build"
includedir="$prefix/include/php"
builddir="`pwd`"

FILES_BUILD="mkdep.awk scan_makefile_in.awk shtool"
FILES="acinclude.m4 Makefile.global"
CLEAN_FILES="$FILES *.lo *.la *.o .deps .libs/ build/ include/ modules/ install-sh \
    mkinstalldirs missing config.nice config.sub config.guess configure configure.in \
	aclocal.m4 config.h config.h.in conftest* ltmain.sh libtool config.cache \
	config.log config.status Makefile Makefile.fragments Makefile.objects confdefs.h"

case "$1" in 
  # Cleanup
  --clean)
    if test -r config.m4; then
      echo "Cleaning.."
      for i in $CLEAN_FILES; do
        test -e $i && rm -rf $i
      done
      exit 0
    else
      echo "Cannot find config.m4. "
      echo "Make sure that you run '$0 --clean' in the top level source directory of the module"
      echo 
      exit 1
    fi
    ;;
  # Usage
  --help)
    echo "Usage: $0 [--clean|--help]"
    exit 1
    ;;
  *)
    if test ! -r config.m4; then
      echo "Cannot find config.m4. "
      echo "Make sure that you run '$0' in the top level source directory of the module"
      echo 
      exit 1
    fi
    ;;
esac

test -d build || mkdir build

(cd $phpdir && cp $FILES_BUILD "$builddir"/build)
(cd $phpdir && cp $FILES "$builddir")

sed \
-e "s#@prefix@#$prefix#" \
< $phpdir/phpize.m4 > configure.in

touch install-sh mkinstalldirs missing

aclocal || exit 1
autoconf || exit 1
autoheader || exit 1
test -x $builddir/build/shtool || chmod +x $builddir/build/shtool
if test ! -x $builddir/build/shtool; then
  echo "shtool at '$builddir/build/shtool' not executable. "
  echo "Make sure that the file exists and is executable and then rerun this script. "
  echo 
  exit 1
fi
libtoolize=`$builddir/build/shtool path glibtoolize libtoolize`
$libtoolize -f -c || exit 1

# dumping API NOs:
PHP_API_VERSION=`egrep '#define PHP_API_VERSION' $includedir/main/php.h|sed 's/#define PHP_API_VERSION//'`
ZEND_MODULE_API_NO=`egrep '#define ZEND_MODULE_API_NO' $includedir/Zend/zend_modules.h|sed 's/#define ZEND_MODULE_API_NO//'`
ZEND_EXTENSION_API_NO=`egrep '#define ZEND_EXTENSION_API_NO' $includedir/Zend/zend_extensions.h|sed 's/#define ZEND_EXTENSION_API_NO//'`

echo "Configuring for:"
echo "  PHP Api Version:  "$PHP_API_VERSION
echo "  Zend Module Api No:  "$ZEND_MODULE_API_NO
echo "  Zend Extension Api No:  "$ZEND_EXTENSION_API_NO

exit 0
