#! /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_verbose
unset opt_force
unset opt_localdir
unset opt_autoconf_dir
unset opt_macrodir
unset opt_include
unset includes
unset opt_prepend_include
unset prepend_includes
unset opt_warnings
unset warnings
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 ;;


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


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


    --force | -f )
       opt_force="--force"
       shift ;;


    --localdir=* | -l?* )
       opt_localdir="--localdir=$optarg"
       shift ;;
    --localdir | -l )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_localdir="--localdir=$1"
       shift ;;


    --autoconf-dir=* | -A?* )
       opt_autoconf_dir="--autoconf-dir=$optarg"
       shift ;;
    --autoconf-dir | -A )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       opt_autoconf_dir="--autoconf-dir=$1"
       shift ;;


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


    --include=* | -I?* )
       includes="$includes --include=$optarg"
       shift ;;
    --include | -I )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       includes="$includes --include=$1"
       shift ;;


    --prepend-include=* | -B?* )
       prepend_includes="$prepend_includes --prepend-include=$optarg"
       shift ;;
    --prepend-include | -B )
       test $# = 1 && eval "$exit_missing_arg"
       shift
       prepend_includes="$prepend_includes --prepend-include=$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$includes" != "x" ] ; then
  opt_include="$includes"
fi
if [ "x$prepend_includes" != "x" ] ; then
  opt_prepend_include="$prepend_includes"
fi
if [ "x$warnings" != "x" ] ; then
  opt_warnings="--warnings=$warnings"
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/autoheader --version
	  exit 0
	elif [ -n "$opt_help" ] ; then
          ${AUTO_DEVEL}/bin/autoheader --help
	  exit 0
        else
          echo "$me: no input file" >&2
          exit 1
        fi
      elif [ -n "$SHORTCUT_STABLE" ] ; then
        if [ -n "$opt_version" ] ; then
          ${AUTO_STABLE}/bin/autoheader --version
	  exit 0
	elif [ -n "$opt_help" ] ; then
          ${AUTO_STABLE}/bin/autoheader --help
	  exit 0
        else
          echo "$me: no input file" >&2
          exit 1
        fi
      else
        if [ -n "$opt_version" ]; then
          ${AUTO_DEVEL}/bin/autoheader --version
          echo "--------------------------------------------------------"
          ${AUTO_STABLE}/bin/autoheader --version
          echo "--------------------------------------------------------"
          echo "This is autoheader-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/autoheader --help
          echo ""
          echo "--------------------------------------------------------"
          echo "------ Above: 'devel' help.  Below: 'stable' help ------"
          echo "--------------------------------------------------------"
          echo ""
          ${AUTO_STABLE}/bin/autoheader --help
          echo ""
          echo "--------------------------------------------------------"
          echo "------ Above: 'stable' help.  Below: 'wrapper' help ----"
          echo "--------------------------------------------------------"
          echo ""
          echo "This is autoheader-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: no input file" >&2
          exit 1
        fi
      fi
      ;;
    esac
    autosense_input=true ;;
  1) infile=$1 ;;
  *) exec >&2
     echo "$me: invalid number of arguments."
     echo "$help"
     (exit 1); exit 1 ;;
esac


# Create a (secure) tmp directory for tmp files.
: ${TMPDIR=/tmp}
{
  tmp=`(umask 077 && mktemp -d -q "$TMPDIR/acXXXXXX") 2>/dev/null` &&
  test -n "$tmp" && test -d "$tmp"
} ||
{
  tmp=$TMPDIR/ac$$
  (umask 077 && mkdir $tmp)
} ||
{
   echo "$me: cannot create a temporary directory in $TMPDIR" >&2
   (exit 1); exit 1;
}


# We need an actual file.
if test z$infile = z-; then
  infile=$tmp/stdin
  cat >$infile
elif test ! -r "$infile"; then
  echo "$me: $infile: No such file or directory" >&2
  (exit 1); exit 1
fi


# 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/autoheader ] ; then
      echo "Can't find ${AUTO_STABLE}/bin/autoheader" 2>&1
      exit 1
    fi
    export PATH=${AUTO_STABLE}/bin:${PATH} 
    export M4PATH=${AUTO_STABLE}/share/autoconf:${M4PATH}
  else
    if [ ! -x ${AUTO_DEVEL}/bin/autoheader ] ; then
      echo "Can't find ${AUTO_DEVEL}/bin/autoheader" 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/autoheader ] ; then
    echo "Can't find ${AUTO_DEVEL}/bin/autoheader" 2>&1
    exit 1
  fi
  export PATH=${AUTO_DEVEL}/bin:${PATH}
  export M4PATH=${AUTO_DEVEL}/share/autoconf:${M4PATH}
fi


# exec the "real" autoheader
if [ "z$infile" == "z$tmp/stdin" ] ; then
  autoheader $opt_version $opt_help \
       $opt_debug $opt_verbose $opt_localdir \
       $opt_include $opt_prepend_include $opt_autoconf_dir $opt_macrodir 
       $opt_warnings $opt_force \
       $opt_stop - < $infile
  rm -rf $tmp
elif [ "z$autosense_input" == "ztrue" ] ; then
  rm -rf $tmp 
  exec autoheader $opt_version $opt_help \
       $opt_debug $opt_verbose $opt_localdir \
       $opt_include $opt_prepend_include $opt_autoconf_dir $opt_macrodir \
       $opt_warnings $opt_force \
       $opt_stop 
else
  rm -rf $tmp 
  exec autoheader $opt_version $opt_help \
       $opt_debug $opt_verbose $opt_localdir \
       $opt_include $opt_prepend_include $opt_autoconf_dir $opt_macrodir 
       $opt_warnings $opt_force \
       $opt_stop $infile
fi
