#! /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_debug
unset opt_dry_run
unset opt_config
unset opt_features
unset opt_preserve_dup_deps
unset opt_finish
unset opt_mode
unset opt_quiet
unset opt_silent
unset opt_tag
unset opt_stop
# Parse command line.
while test $# -gt 0 ; do
  optarg=`expr "x$1" : 'x--[^=]*=\(.*\)' \| \
               "x$1" : 'x-.\(.*\)'`
  case $1 in

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


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


    --debug )
       opt_debug="--debug"
       shift ;;


    --dry-run | -n )
       opt_dry_run="--dry-run"
       shift ;;


    --config )
       opt_config="--config"
       shift ;;


    --features )
       opt_features="--features"
       shift ;;


    --opt-preserve-dup-deps )
       opt_preserve_dup_deps="--preserve-dup-deps"
       shift ;;


    --finish )
       opt_finish="--finish"
       shift ;;


    --mode=* )
       opt_mode="--mode=$optarg"
       shift ;;
    --mode )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_mode="--mode=$1"
       shift ;;


    --quiet )
       opt_quiet="--quiet"
       shift ;;


    --silent )
       opt_silent="--silent"
       shift ;;


    --tag=* )
       opt_tag="--tag=$optarg"
       shift ;;
    --tag )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_tag="--tag=$1"
       shift ;;


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


    * )
       break ;;
  esac
done

# Special handling for the options that can be called 
# multiple times.

# 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 "LIBTOOL_VER=2.57" is just WRONG.
# So, we allow libtool version numbers in WANT_LIBTOOLVER,
# and then "translate" into autoconf/AC_PREREQ numbers.

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

  # if we failed to extract version info, default to parsing configure.ac
 if [ -n ${LT_MAJ} ] ; then
    if [ -n ${LT_MIN} ] ; then
      if [ ${LT_MAJ} -gt 1 ] ; then
        VER=2.50
	SHORTCUT_DEVEL="yes"
      elif [ ${LT_MIN} -gt 4 ] ; then 
        VER=2.50
	SHORTCUT_DEVEL="yes"
      else
        VER=2.13
	SHORTCUT_STABLE="yes"
      fi
    fi
    # if we failed to extract LT_MIN info, default to parsing configure.ac
  fi
  # if we failed to extract LT_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_LIBTOOL_VER is set), then only print the "right" one.
  if [ -n "$SHORTCUT_DEVEL" ] ; then
    if [ -n "$opt_version" ] ; then
      ${AUTO_DEVEL}/bin/libtool --version
      exit 0
    elif [ -n "$opt_help" ] ; then
      ${AUTO_DEVEL}/bin/libtool --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/libtool --version
      exit 0
    elif [ -n "$opt_help" ] ; then
      ${AUTO_STABLE}/bin/libtool --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/libtool --version
      echo "--------------------------------------------------------"
      ${AUTO_STABLE}/bin/libtool --version
      echo "--------------------------------------------------------"
      echo "This is libtool-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/libtool --help $opt_mode
      echo ""
      echo "--------------------------------------------------------"
      echo "------ Above: 'devel' help.  Below: 'stable' help ------"
      echo "--------------------------------------------------------"
      echo ""
      ${AUTO_STABLE}/bin/libtool --help $opt_mode
      echo ""
      echo "--------------------------------------------------------"
      echo "------ Above: 'stable' help.  Below: 'wrapper' help ----"
      echo "--------------------------------------------------------"
      echo ""
      echo "This is libtool-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/libtool ] ; then
      echo "Can't find ${AUTO_STABLE}/bin/libtool" 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/libtool ] ; then
      echo "Can't find ${AUTO_DEVEL}/bin/libtool" 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/libtool ] ; then
    echo "Can't find ${AUTO_DEVEL}/bin/libtool" 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" libtool
exec libtool $opt_version $opt_help \
$opt_debug $opt_dry_run $opt_config \
$opt_features $opt_preserve_dup_deps \
$opt_finish $opt_mode $opt_quiet $opt_silent \
$opt_tag $opt_stop $*
