#! /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_macrodir
unset opt_stop
# Parse command line.  We have to do this in case the user
# specifies the input file or uses stdin.  If so, then
# we must accommodate...but we can only do that if we
# parse ALL possible options.  This gets ugly if the
# two versions of autoconf accept different options.
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 ;;


    --macrodir=* | -m?* )
       opt_macrodir="--macrodir=$optarg"
       shift ;;
    --macrodir | -m )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_macrodir="--macrodir=$1"
       shift ;;


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


    -* ) # Use stdin as input.
      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 test $# -ne 0; then
  exec >&2
  echo "$me: invalid number of arguments"
  echo "$help"
  exit 1
fi


# Trap on 0 to stop playing with `rm'.
$debug ||
{
  trap 'status=$?; rm -rf $tmp && exit $status' 0
  trap '(exit 1); exit 1' 1 2 13 15
}


unset VER
unset SHORTCUT_DEVEL
unset SHORTCUT_STABLE
if [ -n "${WANT_AUTOCONF_VER}" ] ; then
  VER=${WANT_AUTOCONF_VER}

  # some duplicated code (from setpath.frag), but oh well
  VER_MAJ=`echo $VER | awk -F. '{print $1}'`
  VER_MIN=`echo $VER | awk -F. '{print $2}'`

  if [ "x${VER_MIN}" != "x" ] ; then
    if (( ${VER_MIN} <= 13 )) ; then
      SHORTCUT_STABLE="yes"
    else
      SHORTCUT_DEVEL="yes"
    fi
  fi
fi


# Find the input file.
case $# in
  0)
    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_AUTOCONF_VER is set), then only print the "right" one.
      if [ -n "$SHORTCUT_DEVEL" ] ; then
        if [ -n "$opt_version" ] ; then
          ${AUTO_DEVEL}/bin/ifnames --version
          exit 0
        elif [ -n "$opt_help" ] ; then
          ${AUTO_DEVEL}/bin/ifnames --help
          exit 0
        else
          echo "$me: Couldn't find configure.ac nor configure.in file" >&2
          echo "run /usr/auto*/bin/ifnames directly" >&2
          exit 1
        fi
      elif [ -n "$SHORTCUT_STABLE" ] ; then
        if [ -n "$opt_version" ] ; then
          ${AUTO_STABLE}/bin/ifnames --version
          exit 0
        elif [ -n "$opt_help" ] ; then
          ${AUTO_STABLE}/bin/ifnames --help
          exit 0
        else
          echo "$me: Couldn't find configure.ac nor configure.in file" >&2
          echo "run /usr/auto*/bin/ifnames directly" >&2
          exit 1
        fi
      else
        if [ -n "$opt_version" ]; then
          ${AUTO_DEVEL}/bin/ifnames --version
          echo "--------------------------------------------------------"
          ${AUTO_STABLE}/bin/ifnames --version
          echo "--------------------------------------------------------"
          echo "This is ifnames-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/ifnames --help
          echo ""
          echo "--------------------------------------------------------"
          echo "------ Above: 'devel' help.  Below: 'stable' help ------"
          echo "--------------------------------------------------------"
          echo ""
          ${AUTO_STABLE}/bin/ifnames --help
          echo ""
          echo "--------------------------------------------------------"
          echo "------ Above: 'stable' help.  Below: 'wrapper' help ----"
          echo "--------------------------------------------------------"
          echo ""
          echo "This is ifnames-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
          echo "run /usr/auto*/bin/ifnames directly" >&2
          exit 1
        fi
      fi
      ;;
    esac;;
  *) exec >&2
     echo "$me: invalid number of arguments."
     echo "$help"
     (exit 1); exit 1 ;;
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/ifnames ] ; then
      echo "Can't find ${AUTO_STABLE}/bin/ifnames" 2>&1
      exit 1
    fi
    export PATH=${AUTO_STABLE}/bin:${PATH} 
    export M4PATH=${AUTO_STABLE}/share/autoconf:${M4PATH}
  else
    if [ ! -x ${AUTO_DEVEL}/bin/ifnames ] ; then
      echo "Can't find ${AUTO_DEVEL}/bin/ifnames" 2>&1
      exit 1
    fi
    export PATH=${AUTO_DEVEL}/bin:${PATH}
    export M4PATH=${AUTO_DEVEL}/share/autoconf:${M4PATH}
  fi
else
  if [ ! -x ${AUTO_DEVEL}/bin/ifnames ] ; then
    echo "Can't find ${AUTO_DEVEL}/bin/ifnames" 2>&1
    exit 1
  fi
  export PATH=${AUTO_DEVEL}/bin:${PATH}
  export M4PATH=${AUTO_DEVEL}/share/autoconf:${M4PATH}
fi


# exec the "real" ifnames
exec ifnames $opt_version $opt_help $opt_stop 

