#!/bin/sh
# This scripts prepares the configure script for execution.
# Prior to building configure, it makes sure that at least
# the basic autotools are there and are an acceptable version.

# This is a part of the VegaStrike build system.
# This file is released under the GNU Public license.  A
# copy of this license should be available in the same
# directory as this file.

echo VegaStrike Configuration Preparation Tool
echo "  "Maintained by Brian A. Lloyd 
echo '  (balloyd1@users.sourceforge.net)'
echo Please send bug reports or comments about this script
echo to the email above or else visit
echo vegastrike.sourceforge.net for help in using this script
echo
echo The recommended way to change the behaviour of this script
echo is to modify the path so that the version of automake or
echo autoconf you want to use is found first.
echo Alternately, you can explicitly specify the program to be
echo used for each in the following variables.
echo '$ACLOCAL, $AUTOMAKE, $AUTOCONF, $AUTOHEADER'
echo and extra arguments/include paths in these variables:
echo '$ACLOCALFLAGS, $AUTOMAKEFLAGS, $AUTOCONFFLAGS, $AUTOHEADERFLAGS'
echo For proper functionality, both aclocal and automake must be the
echo same version, and autoconf and autoheader should be the same version.
echo

checkversion()
{
  ver_ok=NO
  read t1 t2 t3 t4 t5;
  if ( test `echo $t4 | cut -d. -f1` -lt `echo $ver_needed | cut -d. -f1`) then
    ver_ok=NO
  else
    if ( test `echo $t4 | cut -d. -f2` -lt `echo $ver_needed | cut -d. -f2`) then
      ver_ok=NO    
    else    
      ver_ok=OK
    fi
  fi
  while read TEMP; do
    true;
  done
  if (test x$ver_ok = xOK) then
    return 0;
  else
    return 1;
  fi
}

findversioned()
{
  version=
  vers=`echo -n $PATH | 
    awk 'BEGIN{RS=":"}$0==""{$0=".";}
      {printf "pushd %s >/dev/null; ls -1 aclocal-* 2>/dev/null; popd >/dev/null\n", $0}'| 
    $SHELL |
    awk 'BEGIN{FS="-"}{printf "%s ", $2}'`
  for x in $vers; do
    ACLOCAL=aclocal-$x
    AUTOMAKE=automake-$x
    "$ACLOCAL" --version 2>/dev/null |checkversion && 
    "$AUTOMAKE" --version 2>/dev/null |checkversion
    if (test $? -eq 0) then
      ver_ok=OK
      version=$x
      break;
    fi
  done
  echo -n $version 
}

# Set these variables only if they are not set in the environment.

if (test "x$ACLOCAL" = "x" ); then
    ACLOCAL=aclocal
fi

if (test "x$AUTOMAKE" = "x" ); then
    AUTOMAKE=automake
fi

if (test "x$AUTOCONF" = "x" ); then
    AUTOCONF=autoconf
fi

if (test "x$AUTOHEADER" = "x" ); then
    AUTOHEADER=autoheader
fi

vers_ok=OK

# AUTOMAKE Programs
ver_needed=1.6

AM_VER_NEEDED=$ver_needed
"$ACLOCAL" --version|checkversion && "$AUTOMAKE" --version|checkversion
if (test $? -ne 0) then
#  echo "automake tools (automake, aclocal) are not new enough.  Please upgrade to at least version $ver_needed"
  vers_ok=NO
fi

if (test $vers_ok = NO); then
  vers=`findversioned`
  if (test "x$vers" != x); then
    vers_ok=OK;
    ACLOCAL=aclocal-$vers
    AUTOMAKE=automake-$vers
  fi
fi

if (test $vers_ok = NO); then
  echo "Could not find new enough automake tools (automake, aclocal)."
  echo "Please install new versions that are at least version $ver_needed"
fi

# AUTOCONF Programs
# Versioned versions of these aren't available, making this scripts job easier.
ver_needed=2.53
"$AUTOHEADER" --version|checkversion 
if (test $? -ne 0) then
  echo "autoheader is not new enough.  Please upgrade to at least version $ver_needed"
  vers_ok=NO
fi

"$AUTOCONF" --version|checkversion
if (test $? -ne 0) then
  echo "autoconf is not new enough.  Please upgrade to at least version $ver_needed"
  vers_ok=NO
fi

# If we have working versions, now we can do the work this script 
# was designed to do.  Also had some 
if (test x$vers_ok = xOK) then
{
if (echo Running $ACLOCAL && "$ACLOCAL" -I m4scripts $ACLOCALFLAGS) then
  ( echo Running "$AUTOHEADER" && "$AUTOHEADER" -f $AUTOHEADERFLAGS && \
    echo Running "$AUTOCONF" && "$AUTOCONF" -f $AUTOCONFFLAGS && \
    echo Running "$AUTOMAKE" && "$AUTOMAKE" -acf $AUTOMAKEFLAGS && \
    echo "Bootstrap is complete. Run ./configure to configure the build system." ) || \
    echo "An error occurred while running the autotools.  Please correct."
else
  echo "aclocal execution failed."
  echo "Try running svn update"
  echo "in the directory that bootstrap-sh is in."
  echo "If this does not fix the problem, please verify that"
  echo "aclocal is at least version $AM_VER_NEEDED"
fi
}
fi
