#! /bin/bash
AUTO_STABLE=${AUTO_STABLE:-/usr/autotool/stable}
AUTO_DEVEL=${AUTO_DEVEL:-/usr/autotool/devel}

me=`echo "$0" | sed -e 's,.*[\\/],,'`

help="\
Try \`$me --help' for more information."

exit_missing_arg="\
echo \"$me: option \\\`\$1' requires an argument\" >&2
echo \"\$help\" >&2
exit 1"

# Set up variables
unset autosense_input
unset opt_version
unset opt_help
unset opt_verbose
unset opt_libdir
unset opt_gnu
unset opt_gnits
unset opt_cygnus
unset opt_foreign
unset opt_std_options
unset opt_include_deps
unset opt_ignore_deps
unset opt_generate_deps
unset opt_no_force
unset opt_force_missing
unset opt_output_dir
unset opt_add_missing
unset opt_copy
unset opt_amdir
unset opt_build_dir
unset opt_srcdir_name
unset opt_warnings
unset warnings
unset opt_stop
# Parse command line.
while test $# -gt 0 ; do
  optarg=`expr "x$1" : 'x--[^=]*=\(.*\)' \| \
               "x$1" : 'x-.\(.*\)'`
  case $1 in

    --version | -V ) 
       opt_version="--version"
       shift ;;


    --help | -h )
       opt_help="--help"
       shift ;;


    --verbose | -v )
       opt_verbose="--verbose"
       shift ;;


    --libdir=* )
       opt_libdir="--libdir=$optarg"
       shift ;;
    --libdir )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_libdir="--libdir=$1"
       shift ;;


    --gnu )
       opt_gnu="--gnu"
       shift ;;


    --gnits )
       opt_gnits="--gnits"
       shift ;;


    --cygnus )
       opt_cygnus="--cygnus"
       shift ;;


    --foreign )
       opt_foreign="--foreign"
       shift ;;


    --std-options )
       opt_std_options="--std-options"
       shift ;;


    --include-deps )
       opt_include_deps="--include-deps"
       shift ;;


    --ignore-deps | -i )
       opt_ignore_deps="--ignore-deps"
       shift ;;


    --generate-deps )
       opt_generate_deps="--generate-deps"
       shift ;;


    --no-force )
       opt_no_force="--no-force"
       shift ;;


    --force-missing | -f )
       opt_force_missing="--force-missing"
       shift ;;


    --output-dir=* | -o?* )
       opt_output_dir="--output-dir=$optarg"
       shift ;;
    --output-dir | -o )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_output_dir="--output-dir=$1"
       shift ;;
    

    --add-missing | -a )
       opt_add_missing="--add-missing"
       shift ;;


    --copy | -c )
       opt_copy="--copy"
       shift ;;


    --amdir=* )
       opt_amdir="--amdir=$optarg"
       shift ;;
    --amdir )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_amdir="--amdir=$1"
       shift ;;


    --build-dir=* )
       opt_build_dir="--build-dir=$optarg"
       shift ;;
    --build-dir )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_build_dir="--build-dir=$1"
       shift ;;


    --srcdir-name=* )
       opt_srcdir_name="--srcdir-name=$optarg"
       shift ;;
    --srcdir-name )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_srcdir_name="--srcdir-name=$1"
       shift ;;


    --warnings=* | -W?* )
       warnings=$warnings,$optarg
       shift ;;
    --warnings | -W )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       warnings=$warnings,$1
       shift ;;


    -- )     # Stop option processing
       shift
       opt_stop="--"
       break ;;


    - )      # Use stdin as input.
       break ;;


    -* )
       exec >&2
       echo "$me: invalid option $1"
       echo "$help"
       exit 1 ;;


    * )
       break ;;
  esac
done

# Special handling for the options that can be called 
# multiple times.
if [ "x$warnings" != "x" ] ; then
  opt_warnings="--warnings=$warnings"
fi

# some duplicated code (from setpath.frag), but oh well

# Yes, this is very hackish.  But the main operational mode is
# to parse configure.in, but "AUTOMAKE_VER=2.57" is just WRONG.
# So, we allow automake version numbers in WANT_AUTOMAKE_VER,
# and then "translate" into autoconf/AC_PREREQ numbers.

unset VER
unset SHORTCUT_DEVEL
unset SHORTCUT_STABLE
if [ -n "${WANT_AUTOMAKE_VER}" ] ; then
  AM_MAJ=`echo ${WANT_AUTOMAKE_VER} |\
    sed -n -e 's/^\([[:digit:]]*\).*/\1/p'`
  AM_MIN=`echo ${WANT_AUTOMAKE_VER} |\
    sed -n -e "s/^${AM_MAJ}[^[:digit:]]*\([[:digit:]]*\).*/\1/p"`
  AM_PAT=`echo ${WANT_AUTOMAKE_VER} |\
    sed -n -e "s/^${AM_MAJ}[^[:digit:]]*${AM_MIN}[^[:digit:]]*\([[:digit:]]*\)/\1/p"`

  # if we failed to extract version info, default to parsing configure.ac
  if [ -n ${AM_MAJ} ] ; then
    if [ -n ${AM_MIN} ] ; then
      if [ ${AM_MAJ} -gt 1 ] ; then
        VER=2.50
	SHORTCUT_DEVEL="yes"
      elif [ ${AM_MIN} -gt 4 ] ; then 
        VER=2.50
	SHORTCUT_DEVEL="yes"
      else
        VER=2.13
	SHORTCUT_STABLE="yes"
      fi
    fi
    # if we failed to extract AM_MIN info, default to parsing configure.ac
  fi
  # if we failed to extract AM_MAJ info, default to parsing configure.ac
fi


# Find the input file.
case `ls configure.ac configure.in 2>/dev/null` in
  *ac*in )
    echo "$me: warning: both \`configure.ac' and \`configure.in' are present." >&2
    echo "$me: warning: proceeding with \`configure.ac'." >&2
    infile=configure.ac;;
  *ac ) infile=configure.ac;;
  *in ) infile=configure.in;;
  * )
  # if --version is supplied but no file is found, generate generic
  # version message.  First, call DEVEL --version, echo divider,
  # then call STABLE --version, another divider, and finally
  # an explanatory message.  That way, programs that try to
  # parse the output of --version won't be confused: they will
  # see DEVEL's version information and will discard everything else
  # (since they already discard all the license gobbledygook)
  #
  # On the other hand, if VER is already established (e.g. 
  # WANT_AUTOMAKE_VER is set), then only print the "right" one.
  if [ -n "$SHORTCUT_DEVEL" ] ; then
    if [ -n "$opt_version" ] ; then
      ${AUTO_DEVEL}/bin/automake --version
      exit 0
    elif [ -n "$opt_help" ] ; then
      ${AUTO_DEVEL}/bin/automake --help
      exit 0
    else
      echo "$me: Couldn't find configure.ac nor configure.in file" >&2
      exit 1
    fi
  elif [ -n "$SHORTCUT_STABLE" ] ; then
    if [ -n "$opt_version" ] ; then
      ${AUTO_STABLE}/bin/automake --version
      exit 0
    elif [ -n "$opt_help" ] ; then
      ${AUTO_STABLE}/bin/automake --help
      exit 0
    else
      echo "$me: Couldn't find configure.ac nor configure.in file" >&2
      exit 1
    fi
  else
    if [ -n "$opt_version" ]; then
      ${AUTO_DEVEL}/bin/automake --version
      echo "--------------------------------------------------------"
      ${AUTO_STABLE}/bin/automake --version
      echo "--------------------------------------------------------"
      echo "This is automake-wrapper, which will hand off execution"
      echo "to one of the two real versions listed above, depending"
      echo "on the contents of configure.in/configure.ac.  Since the"
      echo "wrapper was called from within a directory in which those"
      echo "files are not found, this generic 'version' message is"
      echo "displayed."
      exit 0
    elif [ -n "$opt_help" ]; then
      ${AUTO_DEVEL}/bin/automake --help
      echo ""
      echo "--------------------------------------------------------"
      echo "------ Above: 'devel' help.  Below: 'stable' help ------"
      echo "--------------------------------------------------------"
      echo ""
      ${AUTO_STABLE}/bin/automake --help
      echo ""
      echo "--------------------------------------------------------"
      echo "------ Above: 'stable' help.  Below: 'wrapper' help ----"
      echo "--------------------------------------------------------"
      echo ""
      echo "This is automake-wrapper, which will hand off execution"
      echo "to one of the two real versions listed above, depending"
      echo "on the contents of configure.in/configure.ac.  Since the"
      echo "wrapper was called from within a directory in which those"
      echo "files are not found, this generic 'help' message is"
      echo "displayed."
      exit 0
    else
      echo "$me: Couldn't find configure.ac nor configure.in file" >&2
      exit 1
    fi
  fi
  ;;
esac

# Find the version

if [ -z "${VER}" ] ; then 
  VER=`sed -n -e '/AC_PREREQ/s/[^(]*([[]*\([[:digit:]\.]*\)[]]*).*$/\1/p' < $infile`
fi

VER_MAJ=`echo $VER | awk -F. '{print $1}'`
VER_MIN=`echo $VER | awk -F. '{print $2}'`

# Set the PATH appropriately
if [ "x${VER_MIN}" != "x" ] ; then
  if (( ${VER_MIN} <= 13 )) ; then
    if [ ! -x ${AUTO_STABLE}/bin/automake ] ; then
      echo "Can't find ${AUTO_STABLE}/bin/automake" 2>&1
      exit 1
    fi
    export PATH=${AUTO_STABLE}/bin:${PATH} 
    export M4PATH=${AUTO_STABLE}/share/aclocal:/usr/share/aclocal:${AUTO_STABLE}/share/automake:${AUTO_STABLE}/share/autoconf:${M4PATH}
  else
    if [ ! -x ${AUTO_DEVEL}/bin/automake ] ; then
      echo "Can't find ${AUTO_DEVEL}/bin/automake" 2>&1
      exit 1
    fi
    export PATH=${AUTO_DEVEL}/bin:${PATH}
    export M4PATH=${AUTO_DEVEL}/share/aclocal:/usr/share/aclocal:${AUTO_DEVEL}/share/automake:${AUTO_DEVEL}/share/automake/am:${AUTO_DEVEL}/share/autoconf:${M4PATH}
  fi
else
  if [ ! -x ${AUTO_DEVEL}/bin/automake ] ; then
    echo "Can't find ${AUTO_DEVEL}/bin/automake" 2>&1
    exit 1
  fi
  export PATH=${AUTO_DEVEL}/bin:${PATH}
  export M4PATH=${AUTO_DEVEL}/share/aclocal:/usr/share/aclocal:${AUTO_DEVEL}/share/automake:${AUTO_DEVEL}/share/automake/am:${AUTO_DEVEL}/share/autoconf:${M4PATH}
fi


# exec the "real" automake
exec automake $opt_version $opt_help \
$opt_verbose $opt_libdir $opt_gnu \
$opt_gnits $opt_cygnus $opt_foreign \
$opt_std_options $opt_include_deps $opt_ignore_deps \
$opt_no_force $opt_force_missing \
$opt_output_dir $opt_add_missing \
$opt_copy $opt_werror $opt_wno_error \
$opt_stop $@
