#! /bin/sh
##############################################################################
# $Id: bootstrap,v 1.4 2005/08/27 10:21:51 behdad Exp $
# $Author: behdad
# $Date: 2005/08/27 10:21:51 $
# $Revision: 1.4 $
# $Source: /cvs/fribidi/fribidi2/bootstrap,v $
##############################################################################
#                                               -*- Bootstrap -*-
# Run this file to produce a configure script.

DEFAULT_BOOTSTRAPFLAGS="--force --install --autoreconf=auto"
DEFAULT_CONFIGUREFLAGS="--enable-maintainer-mode --enable-compile-warnings"

OLDDIR=`pwd`

# move to source dir
DIR=`echo $0 | sed 's,[^/]*$,,'`
test -n "$DIR" && cd "$DIR"

if test "$1" = --write-configure; then
	test -f configure && \
	((echo '#! /bin/sh
	        DIR=`echo $0 | sed "s,[^/]*\$,,"`
	        exec ${DIR}bootstrap --configure -- "$@"' > configure \
	  && chmod a+x configure) || \
	 echo Failed writing to configure >&2)

	exit $?
fi

# check if we are invoked as configure, or bootstrap
BASE=`echo $0 | sed 's,.*/,,'`
if test "$BASE" = "configure"; then
	CONFIG=--config
	EXTRA_CONFIGUREFLAGS=$@
else # bootstrap
	EXTRA_BOOTSTRAPFLAGS=$*
fi

for arg in $DEFAULT_BOOTSTRAPFLAGS $BOOTSTRAPFLAGS $EXTRA_BOOTSTRAPFLAGS
do
if test -n "$DASHDASHFLAGS"; then
	DASHDASHFLAGS="$DASHDASHFLAGS $arg"
else
	case $arg in
		-h|--h|--he|--hel|--help)
			HELP=--help ;;
		-V|--vers|--versi|--versio|--version)
			VERSION=--version ;;
		-v|--verb|--verbo|--verbos|--verbose)
			VERBOSE=--verbose ;;
		-d|--de|--deb|--debu|--debug)
			DEBUG=--debug ;;
		-W|--w|--wa|--war|--warn|--warni|--warnin|--warning|--warnings|--warnings=yes|--warnings=all)
			WARNS=--warnings=all ;;
		--no-w|--no-wa|--no-war|--no-warn|--no-warni|--no-warnin|--no-warning|--no-warnings|--warn=no|--warning=no|--warnings=no)
			WARNS=--warnings=none ;;
		--w=*|--wa=*|--war=*|--warn=*|--warni=*|--warnin=*|--warning=*|--warnings=*)
			WARNS=$arg ;;
		-f|--f|--fo|--for|--forc|--force|--forced|--force=yes|--force-missing)
			FORCE=--force ;;
		--no-f|--no-fo|--no-for|--no-forc|--no-force|--no-forced|--force=no)
			FORCE=--no-force ;;
		-i|--i|--in|--ins|--inst|--insta|--instal|--install|--install=yes|-a|--add-missing)
			INSTALL=--install ;;
		--no-i|--no-in|--no-ins|--no-inst|--no-insta|--no-instal|--no-install|--install=no)
			INSTALL= ;;
		-s|--s|--sy|--sym|--syml|--symli|--symlin|--symlink|--symlink=yes|--no-copy|--copy=no)
			SYMLINK=--symlink ;;
		--no-s|--no-sy|--no-sym|--no-syml|--no-symli|--no-symlin|--no-symlink|--symlink=no|--copy|--copy=yes)
			SYMLINK= ;;
		-m|--m|--ma|--mak|--make|--make=yes)
			MAKE=--make
			CONFIG=--configure ;;
		--no-m|--no-ma|--no-mak|--no-make|--make=no)
			MAKE= ;;
		-c|--c|--co|--con|--conf|--confi|--config|--configu|--configur|--configure|--configure=yes)
			CONFIG=--configure ;;
		--no-c|--no-co|--no-con|--no-conf|--no-confi|--no-config|--no-configu|--no-configur|--no-configure|--configure=no)
			CONFIG= ;;
		-n|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
			DRYRUN=echo ;;
		--auto|--autoreconf=auto)
			AUTORECONF=auto ;;
		--autoreconf|--autoreconf=yes)
			AUTORECONF=yes ;;
		--no-autoreconf|--autoreconf=no)
			AUTORECONF= ;;
		--)
			DASHDASHFLAGS=" " ;;
		*)
			echo Ignoring unknown parameter $arg
	esac
fi
done

test -z "$SYMLINK" && COPY=--copy
test -n "$INSTALL" && ADDMISSING=--add-missing

# use autoreconf if possible, just check for version 2+
if test "$AUTORECONF" = auto; then
	case `autoreconf --version 2>/dev/null` in
		*"autoreconf (GNU Autoconf) 2."* )
			echo Usable autoreconf found, running
		;;
		*)
			AUTORECONF=
		;;
	esac
fi

if test -n "$AUTORECONF";  then
	$DRYRUN autoreconf $HELP $VERSION $VERBOSE $DEBUG $FORCE $INSTALL $SYMLINK $MAKE $WARNS $AUTORECONFFLAGS || exit $?
else

# add files 'config.guess', 'config.sub', 'ltconfig', 'ltmain.sh'
test -f Makefile.am && MY_LIBTOOLIZEFLAGS=--automake

if grep -q '^ *AC_PROG_LIBTOOL' configure.ac configure.in 2>/dev/null; then
	$DRYRUN libtoolize $HELP $VERSION $COPY $DEBUG $FORCE $MY_LIBTOOLIZEFLAGS $LIBTOOLIZEFLAGS || exit $?
fi

# generate 'aclocal.m4'
if test -f configure.ac -o configure.in; then
	$DRYRUN aclocal $HELP $VERSION $VERBOSE $FORCE $ACLOCALFLAGS || exit $?
fi

# generate 'config.h.in'
if test -f configure.ac -o configure.in; then
	$DRYRUN autoheader $HELP $VERSION $VERBOSE $DEBUG $FORCE $WARNS $AUTOHEADERFLAGS || exit $?
fi

#  generate Makefile.in's from Makefile.am's
if test -f Makefile.am; then
	$DRYRUN automake $HELP $VERSION $VERBOSE $ADDMISSING $COPY $FORCE $WARNS $AUTOMAKEFLAGS || exit $?
fi

# generate configure from configure.ac
if test -f configure.ac -o -f configure.in; then
	$DRYRUN autoconf $HELP $VERSION $VERBOSE $DEBUG $FORCE $WARNS $AUTOCONFFLAGS || exit $?
fi

fi # !AUTOCONF

# move to build dir
cd $OLDDIR

# configure
if test -n "$CONFIG"; then
	if test -f ${DIR}configure; then
		$DRYRUN ${DIR}configure $HELP $VERSION $DEFAULT_CONFIGUREFLAGS $CONFIGUREFLAGS $DASHDASHFLAGS $EXTRA_CONFIGUREFLAGS || exit $?
	fi
fi

# make
if test -n "$MAKE"; then
	if test -f Makefile; then
		$DRYRUN make $MAKEFLAGS || exit $?
	fi
fi
