#!/bin/sh
# Run this to generate all the initial makefiles, etc.
#
# This script is merged from:
# * the cairo project (http://cairographics.org/) and
# * the curl project (http://curl.haxx.se/) and
# has been modified.
#
set -e

PACKAGE=dice

AUTOMAKE_FLAGS="--add-missing --foreign"
LIBTOOL_FLAGS="--force"
LIBTOOL_DIR=libltdl

automake_min_vers_maj=1
automake_min_vers_min=9
aclocal_min_vers_maj=$automake_min_vers_maj
aclocal_min_vers_min=$automake_min_vers_min
autoconf_min_vers_maj=2
autoconf_min_vers_min=50
autoheader_min_vers_maj=$autoconf_min_vers_maj
autoheader_min_vers_min=$autoconf_min_vers_min
libtool_min_vers_maj=1
libtool_min_vers_min=5

ARGV0=$0

# Allow invocation from a separate build directory; in that case, we change
# to the source directory to run the auto*, then change back
srcdir=`dirname $ARGV0`
test -z "$srcdir" && srcdir=.

ORIGDIR=`pwd`

cd $srcdir

#
# Test for autoconf version. Usually autoheader is distributed in the autoconf
# package, but we test for both of them -- you never know.
#
ac=${AUTOCONF:-autoconf}
ac_version=`$ac --version 2>/dev/null|head -1|sed -e 's/^[^0-9]*//' -e 's/[a-z]* *$//'`
if test -z "$ac_version"; then
	echo $ac: command not found
	echo
	echo "$ARGV0: ERROR: You must have \`autoconf' installed to compile $PACKAGE."
	echo "           (version $autoconf_min_vers_maj.$autoconf_min_vers_min or newer is required)"
	exit 1;
fi
IFS=.; set $ac_version; IFS=' '
if test "$1" = "$autoconf_min_vers_maj" -a "$2" -lt "$autoconf_min_vers_min" || test "$1" -lt "$autoconf_min_vers_maj"; then
	echo "$ARGV0: ERROR: \`$ac' is too old."
	$ac --version
	echo "           (version $autoconf_min_vers_maj.$autoconf_min_vers_min or newer is required)"
	exit 1;
fi

echo "$ARGV0: autoconf version $ac_version (ok)"

ah=${AUTOHEADER:-autoheader}
ah_version=`$ah --version 2>/dev/null|head -1|sed -e 's/^[^0-9]*//' -e 's/[a-z]* *$//'`
if test -z "$ah_version"; then
	echo $ah: command not found
	echo
	echo "$ARGV0: ERROR: You must have \`autoheader' installed to compile $PACKAGE."
	echo "           (version $autoheader_min_vers_maj.$autoheader_min_vers_min or newer is required)"
	exit 1;
fi
IFS=.; set $ah_version; IFS=' '
if test "$1" = "$autoheader_min_vers_maj" -a "$2" -lt "$autoheader_min_vers_min" || test "$1" -lt "$autoheader_min_vers_maj"; then
	echo "$ARGV0: ERROR: \`$ah' is too old."
	$ah --version
	echo "           (version $autoheader_min_vers_maj.$autoheader_min_vers_min or newer is required)"
	exit 1;
fi

echo "$ARGV0: autoheader version $ah_version (ok)"

#
# Test for existing versions of automake, which fulfill our minimum
# requirements. We check automake without extension first to allow it to be a
# newer version than we know about.
#
if test x"$AUTOMAKE" = x; then
	am_ver=""
	for ver in "" "-1.10" "-1.9"; do
		am="automake$ver"
		if ($am --version) < /dev/null > /dev/null 2>&1 ; then
			am_version=`$am --version 2>/dev/null|head -1|sed -e 's/^.* \([0-9]\)/\1/' -e 's/[a-z]* *$//' -e 's/\(.*\)\(-p.*\)/\1/'`
			if test -n "$am_version"; then
				IFS=.; set $am_version; IFS=' '
				if test "$1" = "$automake_min_vers_maj" -a "$2" -lt "$automake_min_vers_min" || test "$1" -lt "$automake_min_vers_maj"; then : ; else
					am_ver=$ver;
					break;
				fi
			fi
		fi
	done

	AUTOMAKE=${AUTOMAKE:-automake$am_ver}
fi

#
# Test for automake. Usually aclocal is distributed in the same package, but
# we test for both of them -- you never know
# 
am=${AUTOMAKE:-automake}
am_version=`$am --version 2>/dev/null|head -1|sed -e 's/^.* \([0-9]\)/\1/' -e 's/[a-z]* *$//' -e 's/\(.*\)\(-p.*\)/\1/'`
if test -z "$am_version"; then
	echo $am: command not found
	echo
	echo "$ARGV0: ERROR: You must have \`automake' installed to compile $PACKAGE."
	echo "           (version $automake_min_vers_maj.$automake_min_vers_min or newer is required)"
	exit 1;
fi
IFS=.; set $am_version; IFS=' '
if test "$1" = "$automake_min_vers_maj" -a "$2" -lt "$automake_min_vers_min" || test "$1" -lt "$automake_min_vers_maj"; then
	echo "$ARGV0: ERROR: \`$am' is too old."
	$am --version
	echo "           (version $automake_min_vers_maj.$automake_min_vers_min or newer is required)"
	exit 1;
fi

echo "$ARGV0: automake version $am_version (ok)"

#
# Test for existing versions of aclocal, which fulfill our minimum
# requirements. We check aclocal without extension first to allow it to be a
# newer version than we know about.
#
if test x"$ACLOCAL" = x; then
	acl_ver=""
	for ver in "" "-1.10" "-1.9"; do
		acl="aclocal$ver"
		if ($acl --version) < /dev/null > /dev/null 2>&1 ; then
			acl_version=`$acl --version 2>/dev/null|head -1|sed -e 's/^.* \([0-9]\)/\1/' -e 's/[a-z]* *$//' -e 's/\(.*\)\(-p.*\)/\1/'`
			if test -n "$acl_version"; then
				IFS=.; set $acl_version; IFS=' '
				if test "$1" = "$aclocal_min_vers_maj" -a "$2" -lt "$aclocal_min_vers_min" || test "$1" -lt "$aclocal_min_vers_maj"; then : ; else
					acl_ver=$ver;
					break;
				fi
			fi
		fi
	done

	ACLOCAL=${ACLOCAL:-aclocal$acl_ver}
fi

acl=${ACLOCAL:-aclocal}
acl_version=`$acl --version 2>/dev/null|head -1|sed -e 's/^.* \([0-9]\)/\1/' -e 's/[a-z]* *$//' -e 's/\(.*\)\(-p.*\)/\1/'`
if test -z "$acl_version"; then
	echo $acl: command not found
	echo
	echo "$ARGV0: ERROR: You must have \`aclocal' installed to compile $PACKAGE."
	echo "           (version $aclocal_min_vers_maj.$aclocal_min_vers_min or newer is required)"
	exit 1;
fi
IFS=.; set $acl_version; IFS=' '
if test "$1" = "$aclocal_min_vers_maj" -a "$2" -lt "$aclocal_min_vers_min" || test "$1" -lt "$aclocal_min_vers_maj"; then
	echo "$ARGV0: ERROR: \`$acl' is too old."
	$acl --version
	echo "           (version $aclocal_min_vers_maj.$aclocal_min_vers_min or newer is required)"
	exit 1;
fi

lt=${LIBTOOLIZE:-libtoolize}
lt_version=`$lt --version 2>/dev/null|head -1|sed -e 's/^.* \([0-9]\+\.[0-9]\+\.[0-9]\+\)/\1/'`
if test -z "$lt_version"; then
	echo $lt: command not found
	echo
	echo "$ARGV0: ERROR: You must have \`libtoolize' installed to compile $PACKAGE."
	echo "          (version $libtool_min_vers_maj.$libtool_min_vers_min or newer is required)"
	exit 1;
fi
IFS=.; set $lt_version; IFS=' '
if test "$1" = "$libtool_min_vers_maj" -a "$2" -lt "$libtool_min_vers_min" || test "$1" -lt "$libtool_min_vers_maj"; then
	echo "$ARGV0: ERROR: \`$lt' is too old."
	$lt --version
	echo "          (version $libtool_min_vers_maj.$libtool_min_vers_min or newer is required)"
	exit 1
fi

echo "$ARGV0: aclocal version $acl_version (ok)"

do_cmd() {
	echo "$ARGV0: running \`$@'"
	$@
}

if ! test -d $LIBTOOL_DIR; then
	echo
	echo "$ARGV0: WARNING: libltdl directory is missing."
	echo "        Try to install from local version."
	LIBTOOL_DLAGS=$LIBTOOL_FAGS --ltdl -c
fi
do_cmd $lt $LIBTOOL_FLAGS

do_cmd $acl $ACLOCAL_FLAGS

do_cmd $ah

do_cmd $am $AUTOMAKE_FLAGS

do_cmd $ac

# also run auto* tools in libtool subdir to have consistent configurations

if test -d $IBTOOL_DIR; then
	echo "$ARGV0: running autotools in subdir $LIBTOOL_DIR ..."
	cd $LIBTOOL_DIR

	do_cmd $acl $ACLOCAL_FLAGS
	do_cmd $ah
	do_cmd $am $AUTOMAKE_FLAGS
	do_cmd $ac

	cd -
	echo "$ARGV0: done in subdir $LIBTOOL_DIR."
fi

cd $ORIGDIR || exit 1

rm -f config.cache

echo "$ARGV0: now run configure and make."

# vim: set tabstop=4 :
# vim: set ft=sh :
