#! /bin/sh

# checking for the GLOBUS_LOCATION

if test "x$GLOBUS_LOCATION" = "x"; then
    echo "ERROR Please specify GLOBUS_LOCATION" >&2
    exit 1
fi

error()
{
    echo ""
    echo "ERROR: bootstrap failed!"
    echo ""
    exit 1
}


# check for autotools

for program in automake aclocal autoconf autoheader
    do
    ($program --version ) > /dev/null 2>&1 || { 
    echo "ERROR: $program not found" >&2
    exit 1
    }
done

config=

# test for local macros
if test -d "./config"; then
    config="$config -I ./config"
fi

# test to see if GPT macros are in a seperate location
if test ! -f "$GLOBUS_LOCATION/share/globus_aclocal/gpt_autoconf_macros.m4"; then
    if test "x$GPT_LOCATION" = "x"; then
        echo "ERROR Globus Packaging Tools not found" >&2
        echo "ERROR either set GPT_LOCATION or install them in $GLOBUS_LOCATION" >&2
        exit 1
    else
        config="$config -I $GPT_LOCATION/share/globus_aclocal"
    fi
else
    config="$config -I $GLOBUS_LOCATION/share/globus_aclocal"
fi

if test "x$GPT_LOCATION" = "x"; then
    GPT_LOCATION=$GLOBUS_LOCATION
fi

echo "running aclocal $config"
aclocal $config || error

if test -f acconfig.h ; then
echo "running autoheader"
    autoheader || error
fi

echo "running libtoolize --copy --force"
libtoolize --copy --force || \
  libtoolize --copy --force || error

echo "running automake --copy -add-missing --foreign"
automake --copy --add-missing --foreign || \
  automake --copy --add-missing --foreign  || error


echo "running gpt_create_automake_rules"
$GPT_LOCATION/sbin/gpt_create_automake_rules || error


echo "running autoconf"
autoconf || error

# update stamp.h.in

if test -f "stamp-h.in" ; then
    touch stamp-h.in
fi
