#!/bin/sh
#
# bootstrap template
#
# This script should be used to configure how to generate ("strap") all
# autotools-automagically generated files (and a way to safely get rid of them).
#
# This is useful for users of the CVS that do not know or want to know what
# cryptic auto* they need to call, and in what order, for this project. Also,
# this is a canonical way to strap a CVS project for automated builds.
#
# Example use:
#
#  $ cvs co PROJECT && .strap_auto all && ./configure && make install
#
# See "bootstrap --help" for usage information.
#

usage () {
	echo "usage: $0 [--all|--clean|--strap]"
	echo " --all   : clean and bootstrap"
	echo " --clean : clean only"
	echo " --strap : bootstrap only (default)"
}

#
# Generate files:
#
auto_strap () {
	aclocal || exit $?
	autoheader || exit $?
	automake --add-missing || exit $?
	autoreconf || exit $?
}

#
# Remove files:
#
auto_clean () {
	# run distclean if available
	make distclean 2>/dev/null || true
	# Delete automatically created symlinks
	for f in missing install-sh mkinstalldirs config.guess config.sub \
	depcomp INSTALL COPYING ltmain.sh; do
		test -h "$f" && rm -f -v "$f"
	done
	# Always delete these files
	rm -f -v *~ configure config.h.in config.h stamp-h.in stamp-h1* \
		config.cache config.log config.status aclocal.m4 libtool
	rm -r -f -v autom4te.cache
}

#
# parse options
#

DO_AUTOCLEAN=0
DO_AUTOSTRAP=1
while test ! $# = 0; do
	case "$1" in
	-h|-?|--help)
		usage
		exit 0
		;;
	clean|--clean)
		DO_AUTOCLEAN=1
		DO_AUTOSTRAP=0
		;;
	all|--all)
		DO_AUTOCLEAN=1
		DO_AUTOSTRAP=1
		;;
	strap|--strap)
		DO_AUTOCLEAN=0
		DO_AUTOSTRAP=1
		;;
	*)
		usage
		echo "error: unknown option $1" 1>&2
		exit 1
		;;
	esac
	shift
done

#
# main body
#

test $DO_AUTOCLEAN = 1 && auto_clean
test $DO_AUTOSTRAP = 1 && auto_strap
