#!/bin/sh

prefix=/usr
exec_prefix=/usr
af_home=/usr/share/autofig

##
## Define usage()
##
usage()
{
        cat <<EOF
Usage: autofig [OPTIONS] [fig-script]
Options:
  --version     output autofig version information.
  --noinstall   run as if not yet installed.
EOF
        exit $1
}

##
## Process options
##
parse()
{
# we must be called with at least one argument
if test $# -eq 0; then
        usage 1 1>&2
fi

# grab all -- arguments
while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case $1 in
    --help)
      usage 0
      ;;
    --version)
      echo Autofig version 0.1 
      exit
      ;;
    --noinst*)
      echo "Looking for macros in /usr/src/RPM/BUILD/autofig-0.1"
      af_home=/usr/src/RPM/BUILD/autofig-0.1
      ;;
    --*) 
      usage 1>&2
      ;;
    *)
      break
      ;;
  esac
  shift
done

name=`echo $1 | sed 's/\.af$//'`
source=$name.af
target=$name.in

}

##
## Main
##

parse $*

if test ! -f $source; then
  echo "$source not found"
  exit 1
fi

if test ! -f $af_home/start.m4; then
  echo "Could not find required macro files"
  echo 1
fi

cat $af_home/start.m4 $source $af_home/end.m4 | m4  > $target

errors=`grep 'AF_.*(' $target | sed 's/^.*AF_/AF_/' | sed 's/(.*$//'`
if test "X$errors" != "X"; then
  echo "Possible undefined macros:"
  echo "$errors"
fi

