#!/bin/sh

builddir=build/

this="`basename $0`"
here="`dirname $0`"

usage="\
Usage: $this [--fast] [--keep] [--clear] [--rebuild]
\t[--configure] [--make] [--install] [--all]
\t[--builddir=dirname]

$this runs all necessary programs to build a configure script. It can
also run the configure script, make, and install the program. If you are not a
developer, follow the instrutions in INSTALL instead of using $this.

Use  $this             to create the configure script
Use  $this --all       to build and install with default settings
Use  $this --clear     to delete all files generated by the autotools
Use  $this --fast ...  if configure need not be rebuilt

--builddir specifies where compilation should occur [default=$builddir]"

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

  case $1 in
		--fast)
			# Only do fast build if configure and Makefile.in already exist
			if test -f configure -a -f Makefile.in; then
				fast=yes
			fi;;
		--clear) clear=yes fast=yes;;
		--rebuild) clear=yes;;
		--configure) configure=yes;;
		--make) make=yes;;
		--install) install=yes;;
		--all) configure=yes; make=yes;;

		--builddir=*) builddir=$optarg;;

		--help) echo -e "$usage" && exit 0
  esac
  shift
done

if test $clear; then
	echo "$this: Deleting files created by automake and autoconf"
	rm -f aclocal.m4 config.* configure depcomp install install-sh missing mkinstalldirs
	rm -f *.lnk
	rm -f -R autom4te.cache/
fi

if test -z $fast; then
	echo "$this: Running aclocal..." && \
	aclocal && \
	echo "$this: Running autoconf..." && \
	autoconf && \
	echo "$this: Running autoheader..." && \
	autoheader && \
	echo "$this: Running automake..." && \
	automake --add-missing || exit
fi

if test ! -d $builddir; then
	echo "$this: Making and entering $builddir directory..."
	mkdir -p $builddir
fi

cd $builddir

if test $configure; then
	echo "$this: Running configure..."
	../configure -C || exit
fi

if test $make; then
	echo "$this: Running make..."
	make || exit
fi

if test $install; then
	echo "$this: Installing..."
	make install || exit
fi
