#! /bin/sh -e

nls=--disable-nls
clean=no
include_deps=yes

while true; do
  if [ "$1" = "--enable-nls" ]; then
    nls=
  elif [ "$1" = "--disable-nls" ]; then
    nls=--disable-nls
  elif [ "$1" = "--clean" ]; then
    clean=yes
  elif [ "$1" = "--not-clean" ]; then
    clean=no
  elif [ "$1" = "--no-include-deps" ]; then
    include_deps=no
  elif [ "$1" = "--include-deps" ]; then
    include_deps=yes
  elif [ "$1" = "--help" ]; then
    echo "Usage: $0 [--enable-nls] [--clean] [--include-deps] ..."
    exit 0
  else
    break
  fi
  
  shift
done

# Extract PSPP version number.
VERSION=`sed -ne 's/^.*\[//;s/].*$//;/^[0-9]*\.[0-9]*\.[0-9]*$/p' < configure.in`

if [ "$clean" = "no" ]; then
  echo "Generating a Makefile for cleaning..."
  if [ ! -f Makefile ]; then
    aclocal
    autoheader
    make -f Makefile.am docfiles VERSION=$VERSION
    automake --foreign
    autoconf
    ./configure $nls $*
  fi
  echo "Cleaning the distribution..."
  make -k maintainer-clean || true
fi

echo "Configuring..."
aclocal
autoheader
make -f Makefile.am docfiles VERSION=$VERSION
automake
autoconf
./configure $nls $*
make mostlyclean || true	# This causes dependencies to be generated

if [ "$include_deps" = "yes" ]; then
  echo "Configuring with included dependencies..."
  automake --include-deps
  autoconf
  ./configure $nls $*
fi
