#!/bin/sh

#
# $XORP: xorp/bootstrap,v 1.31 2008/04/30 19:31:36 pavlin Exp $
#
#
# A script of autoconf/automake/libtool commands to bring the system
# up to date if some of the configuration files are modified
#

#
# Allow the autotools commands used to be overridden using environment
# variables, to help off-site package maintainers.
#
ACLOCAL=${ACLOCAL:-"aclocal-1.10"}
AUTOCONF=${AUTOCONF:-"autoconf-2.61"}
AUTOHEADER=${AUTOHEADER:-"autoheader-2.61"}
AUTOM4TE=${AUTOM4TE:-"autom4te-2.61"}
AUTOMAKE=${AUTOMAKE:-"automake-1.10"}
LIBTOOLIZE=${LIBTOOLIZE:-"libtoolize"}

# Export autotools variables that might be used by some of the tools
export AUTOCONF
export AUTOM4TE

# Figure out if we were invoked with an autoconf name other than the
# default (as this chnages the name of the autom4te.cache directories).
AUTOM4TE_DIRS="autom4te"
if [ ${AUTOCONF} != "autoconf" ]; then
    tmp_autom4te=$( basename ${AUTOCONF} | sed 's/conf/m4te/' )
    AUTOM4TE_DIRS="${AUTOM4TE_DIRS} ${tmp_autom4te}"
fi

# Prototype: check_necessary <this_script_name>
check_necessary()
{
    if [ $# -ne 1 ] ; then
	echo "check_necessary: incorrect argument count"
	exit 1
    fi

    checkfile=./Makefile.in
    if [ ! -e ${checkfile} ] ; then
	checkfile=$1
    fi

    if [ ! -f ${checkfile} ] ; then
	echo "${checkfile} does not exist or is not an ordinary file"
	exit 1
    fi

    echo -n "Checking configuration files..."

    NEWER_AM_FILES=`find . -name 'Makefile.am' -a -newer ${checkfile}`

    # Look files included or used by Makefile.am files
    SPECIAL_AM_FILES="docs/mk/Makefile.doc.am"
    for s in ${SPECIAL_AM_FILES} ; do
	if [ ${s} -nt ${checkfile} ] ; then
	    NEWER_AM_FILES="${NEWER_AM_FILES} ${s}"
	fi
    done

    NEWER_CONFIG_MACROS=`find config -name 'ac*.m4' -a -newer ${checkfile}`
    NEWER_CONFIG_IN=`find . -name 'configure.in' -a -newer ${checkfile}`

    echo "done."

    NEWER_FILES="${NEWER_AM_FILES}${NEWER_CONFIG_MACROS}${NEWER_CONFIG_IN}"

    if [ "${NEWER_FILES}"X = "X" ] ; then

	cat <<EOF

Neither configure.in, nor any m4 macro files, nor any Makefile.am files
have been edited.  You almost certainly do not want to run this script
which will regenerate automake/autoconf output files.

Proceed at your own risk (y/n)?
EOF

	read carry_on
	if [ "${carry_on}X" != "yX" ] ; then
	    echo "Okay, stopping here."
	    exit 1
	fi
    fi
}

run()
{
	echo $@
	if ! $@ ; then
		echo "Error running '$@'"
		exit 1
	fi
}

#
# Prototype: program_flags <prog_name> <threshold_version>
#			   <flags_if_newer> <flags_if_older>
# On output prints the program flags
program_flags()
{
    if [ $# -ne 4 ] ; then
	echo "program_flags: incorrect argument count"
	exit 1
    fi
    prog_name=$1
    threshold_version=$2
    flags_if_newer=$3
    flags_if_older=$4

    prog_maj_version=`${prog_name} --version | head -n 1 | awk '{print $NF}' | cut -f 1 -d.`
    prog_min_version=`${prog_name} --version | head -n 1 | awk '{print $NF}' | cut -f 2 -d.`
    threshold_maj_version=`echo ${threshold_version} | cut -f 1 -d.`
    threshold_min_version=`echo ${threshold_version} | cut -f 2 -d.`

    if [ ${prog_maj_version} -ge ${threshold_maj_version} \
	-a ${prog_min_version} -ge ${threshold_min_version} ] ; then
	echo ${flags_if_newer}
    else
	echo ${flags_if_older}
    fi
}

#
# Change to directory of script.  This script lives at the top of the source
# tree and it's often useful to be able to run it from another location.
#
cd `dirname $0`
if [ ! -d config ] ; then
    echo "The \"config\" does not appear to exist"
    exit 1
fi

check_necessary $0

#
# Cleanup
#
run rm -f config.cache
run rm -f aclocal.m4
run rm -f config/aclocal.m4
run rm -f mibs/config.cache
run rm -f mibs/aclocal.m4

#
# If necessary, use "-I DIR" again to add more directories to search list
# for .m4 files.
#
# XXX: note that the output file (aclocal.m4) should be generated
# in the local directory (the default behavior), otherwise newer
# versions of automake (e.g., automake-1.5) won't work anymore.
#
run ${ACLOCAL} -I config

# XXX: does mibs need its own aclocal?
run cd mibs
run ${ACLOCAL} -I ../config
run cd ..

#
# XXX: we don't use flag --force because with it libtoolize always
# overwrites our config.guess and config.sub files, even though our
# version is probably newer.
#
run ${LIBTOOLIZE} --automake --copy

# XXX: does mibs need its own libtool?
run cd mibs
run ${LIBTOOLIZE} --automake --copy
run cd ..

#
# If we are using newer version of autoconf/autoheader (e.g., autoconf-2.5*),
# we need to add the new flag "--include=config".
#
autoheader_options=`program_flags ${AUTOHEADER} 2.50 "--include=config" ""`
run ${AUTOHEADER} ${autoheader_options}

# XXX: mibs does not need its own autoheader run.
#run cd mibs
#run ${AUTOHEADER} ${autoheader_options}
#run cd ..

#
# XXX: in case of non-GNU 'make', we must use flag "-i".
# XXX: we may have to use "-i" before distributing the source code (?)
# run ${AUTOMAKE} -i --add-missing --copy
#
run ${AUTOMAKE} --add-missing --copy --foreign 
run cd mibs
run ${AUTOMAKE} --add-missing --copy --foreign
run cd ..

#
# If we are using newer version of autoconf (e.g., autoconf-2.5*),
# use the new flag "--include=config" instead of "-l config".
# If necessary, use it again to add more directories to search list
# for .m4 files.
#
autoconf_options=`program_flags ${AUTOCONF} 2.50 "--include=config" "-l config"`
run ${AUTOCONF} ${autoconf_options}

# Run autoconf in each subdirectory that has configure.in
run cd cli/libtecla
autoconf_options=`program_flags ${AUTOCONF} 2.50 "--include=../../config" "-l ../../config"`
run ${AUTOCONF} ${autoconf_options}
run cd ../..

run cd mibs
autoconf_options=`program_flags ${AUTOCONF} 2.50 "--include=../config" "-l ../config"`
run ${AUTOCONF} ${autoconf_options}
run cd ..

#
# Cleanup autom4te.cache directories
#
autom4te_cache_dirs=""
for dir in ${AUTOM4TE_DIRS} ; do
    if [ "${autom4te_cache_dirs}" != "" ] ; then
	autom4te_cache_dirs="${autom4te_cache_dirs} "
    fi
    autom4te_cache_dirs="${autom4te_cache_dirs}${dir}.cache"
done
echo "Removing ${autom4te_cache_dirs} cache directories..."
remove_prefix_dirs=". cli/libtecla mibs"
for prefix_dir in ${remove_prefix_dirs} ; do
    for suffix_dir in ${autom4te_cache_dirs} ; do
	dir="${prefix_dir}/${suffix_dir}"
	if [ -d  ${dir} ] ; then
	    run rm -rf ${dir}
	fi
    done
done

#
# Cleanup aclocal.m4 auto-generated files
#
echo "Removing auto-generated aclocal.m4 files..."
run rm -f aclocal.m4
run rm -f config/aclocal.m4
run rm -f mibs/aclocal.m4

exit 0
