#! /bin/sh
# runs autoheader and autoconf with local macros
echo making configure in `pwd`
# Check on version of autoconf
#autoconf=autoconf
#autoheader=autoheader
#version=`autoconf --version | sed -e 's/Autoconf version //'`
#mainversion=`expr $version : '\([0-9]\)\..*'`
#subversion=`expr $version : '[0-9]\.\([0-9]*\)'`
#if [ "$mainversion" != "2" -o "$subversion" -lt 13 ] ; then
#    autoconf=/homes/gropp/bin/autoconf
#    autoheader=/homes/gropp/bin/autoheader
#fi
# Must use patched automake
if [ -z "$ARCH" ] ; then
    system="`uname -sr | sed -e 's/ /-/g'`"
    case "$system" in 
	SunOS-5*) ARCH=solaris-2 ;;
	Linux*)   ARCH=linux ;;
    esac
fi
larch=$ARCH
case "$larch" in
    linux*|Linux*|LINUX*) larch=linux ;;
esac
automake=/homes/gropp/bin/$larch/automake
aclocal=/homes/gropp/bin/$larch/aclocal
autoheader=/homes/gropp/bin/$larch/autoheader
autoconf=/homes/gropp/bin/$larch/autoconf
if [ ! -x $automake ] ; then
    echo "Warning: did not find a patched automake in $automake"
    # Try to use any available automake
    saveIFS="$IFS"
    IFS=":"
    automake=
    autoheader=
    autoconf=
    aclocal=
    for dir in $PATH ; do
	if [ -z "$automake" -a  -x $dir/automake ] ; then
	    automake=$dir/automake
	fi
        if [ -z "$autoheader" -a -x $dir/autoheader ] ; then
	    autoheader=$dir/autoheader
	fi
	if [ -z "$aclocal" -a -x $dir/aclocal ] ; then
	    aclocal=$dir/aclocal
        fi
	if [ -z "$autoconf" -a -x $dir/autoconf ] ; then
	    autoconf=$dir/autoconf
        fi
    done
    IFS="$saveIFS"
    if [ -z "$automake" -o -z "$autoheader" -o -z "$aclocal" -o \
	 -z "$autoconf" ] ; then
	echo "Could not find all of the needed automake/autoconf tools"
        exit 1
    fi
fi
# Change this if autoconf is version 2.52.
ac_version="2.13"
#
if [ ! -d config/confdb ] ; then
    (cd config ; cvs -d /homes/gropp/cvsMaster_z -r checkout confdb )
fi
#
# GRRRR.  aclocal reads *everything* out of the directory.  This
# breaks when the aclocal.m4 file includes different files depending on
# which mutually incompatible version of autoconf is being used.
# The problem is that automake needs aclocal.  We try to fix this 
# by hoping for autoconf 2.13 instead of 2.52 (this is yet another reason
# not to use automake)
if [ "$ac_version" = "2.13" ] ; then
    movefiles="libtool.m4 libtool-2.13.m4 aclangf90.m4 fortran90.m4"
else
    movefiles="libtool.m4 libtool-2.13.m4 aclocal_f90.m4 fortran90.m4"
fi
for file in $movefiles ; do
   mv config/confdb/$file config/confdb/$file.tmp
done
$aclocal -I config/confdb
for file in $movefiles ; do 
    mv config/confdb/$file.tmp config/confdb/$file
done
if [ `uname -s` = "SunOS" -a `uname -r` = "5.7" ] ; then
    echo "Dependency handling turned off"
    #$automake --no-dependencies
    $automake -i
elif [ "$MAKE_DIST_MAKEFILE" = "yes" ] ; then
    # This is a special entry point for building tar files without using
    # the make dist target (for example, for including this package within
    # MPICH)
    $automake --include-deps --foreign Makefile
else
    # Turn off dependencies even locally so that development builds that
    # use vpath will work
    if $automake -i ; then
	: # All is ok
    else
	echo "Automake failed!"
	echo "If using cygwin, the automake script may incorrectly convert"
	echo "a -i argument to --ignore-deps .  Try using the so-called"
	echo "stable version, if available."
	if [ -x /usr/autotool/stable/bin/automake ] ; then
	    if /usr/autotool/stable/bin/automake -i ; then
		echo "Found stable automake and ran successfully"
	    else
		echo "Failed to run automake"
		exit 1
	    fi
	else
	    echo "No automake available"
	    exit 1
        fi
    fi
fi
$autoheader 
$autoconf  
if grep AM_IGNORE configure >/dev/null 2>&1 ; then 
    # Grr.  Some versions of aclocal don't include all of the definitions,
    # so an AM_IGNORE may be left in the configure
    rm -f conftest
    sed '/AM_IGNORE/d' configure >conftest
    mv -f conftest configure
    chmod a+x configure
fi
if grep AM_INIT_AUTOMAKE configure >/dev/null 2>&1 ; then
    echo "Autoconf failed to handle AM_INIT_AUTOMAKE !"
    echo "Attempted to build on "
    hostname
    echo "on date" 
    date
    echo "with PATH"
    echo "$PATH"
    echo "using"
    echo "autoconf=$autoconf"
    echo "aclocal=$aclocal"
    echo "automake=$automake"
    echo "autoheader=$autoheader"
fi
rm -f config.cache config.system
