#! /bin/sh

# Time-stamp:        "2006-12-02 06:45:43 bkorb"
# Last Committed:    $Date: 2007/01/18 05:32:13 $

d=`dirname $0`
d=`cd $d ; pwd`
prog=${d}/`basename $0`

test -z "${CC}"     && CC="@CC@"
test -z "${CC}"     && `which cc`
test -z "${CFLAGS}" && CFLAGS="@CFLAGS@"
test -z "${LIBS}"   && LIBS="@LIBS@"
test -z "${MAKE}"   && MAKE=`which make`

INCLUDES="@DEFS@ "$(

    for d in . @srcdir@ @top_builddir@ @top_srcdir@ \
             @top_builddir@/autoopts @top_srcdir@/autoopts
    do
        (\cd ${d} ; pwd)
    done | \
    uniq | \
    sed s/^/-I/ )

# Give preference to configured SHELL
#
d=${SHELL}
SHELL=@SHELL@
test -z "${SHELL}"  && SHELL=$d

export CC CFLAGS LIBS MAKE SHELL INCLUDES

# Now auto-export variables:
#
set -a

top_builddir=`cd @top_builddir@ ; pwd`
top_srcdir=`cd @top_srcdir@ ; pwd`
srcdir=`cd @srcdir@ ; pwd`
PATH=${top_builddir}/columns:${PATH}
timer=`expr @AG_TIMEOUT@ '*' 5`

eval `egrep '^AG_[A-Z_]*' ${top_srcdir}/VERSION`

AGsrc=${top_srcdir}/agen5
AGexe=@AGexe@
GDexe=@GDexe@
CLexe=@CLexe@

LIBS=${LIBS}\ `find ${top_builddir}/autoopts -name libopts.a -print`
OPTIONS_DEF=${AGsrc}/opts.def
DIRECTIVESRC=${AGsrc}/defDirect.c
GETDEF_SRC=$(
  echo ${AGsrc}/func*.c ${AGsrc}/exp*.c ${AGsrc}/schemedef.scm ${DIRECTIVESRC} )

ADDON_MENU=$(
  for f in columns getdefs xml2ag
  do
    echo ${top_builddir}/${f}/${f}.menu
  done)\ ${top_srcdir}/doc/snprintfv.menu

ADDON_TEXI=$(
  for f in columns getdefs xml2ag
  do
    echo ${top_builddir}/${f}/${f}.texi
  done)\ ${top_srcdir}/doc/snprintfv.texi

DOC_INCLUDES=$(
  echo ${AGsrc}/defParse-fsm.c ${AGsrc}/opts.h
  echo ${top_builddir}/agen5/autogen.menu ${top_builddir}/agen5/autogen.texi
  echo ${top_builddir}/autoopts/libopts.texi )

DOC_TEMPLATE=${srcdir}/auto_gen.tpl

DOC_DEPENDS=$(
  echo ${DOC_TEMPLATE} ${OPTIONS_DEF} ${ADDON_MENU} ${ADDON_TEXI}
  echo ${DOC_INCLUDES} ${GETDEF_SRC} )

set +a

#  Validate everything:
#
bail_out() {
   set -x

   exec 2>&8
   cat texi$$.log
   echo >&2
   echo "$*" >&2
   exit 1
}

# Make sure the executables are there
#
test -x ${AGexe} || (cd `dirname ${AGexe}` ; ${MAKE}) || exit 0
test -x ${GDexe} || (cd `dirname ${GDexe}` ; ${MAKE}) || exit 0
test -x ${CLexe} || (cd `dirname ${CLexe}` ; ${MAKE}) || exit 0

exec 8>&2 2> texi$$.log
tmp_files=texi$$.log
printf '\n\n\n\n' >&2

set -x

# See to it that the .texi files have been generated, too.
#
for f in ${ADDON_TEXI} ${top_builddir}/agen5/autogen.texi \
         ${top_builddir}/autoopts/libopts.texi
do
  test -f ${f} || (
    cd `dirname ${f}`
    ${MAKE} `basename ${f}` >&2
  ) || bail_out MAKE of ${f} failed.
done

# We have our executables and texi's.  Collect the definitions:
#
exec 3> getdefs$$.cfg
tmp_files="${tmp_files} getdefs$$.cfg"
cat >&3 <<-  EOCat
	output      autogen.def
	copy        ${OPTIONS_DEF}
	srcfile
	linenum
	template    auto_gen.tpl
	EOCat

tmp_files="${tmp_files} autogen.def"
for f in ${GETDEF_SRC}
do
  echo "input      " ${f}
done >&3
exec 3>&-
echo + ${GDexe} load-opt=getdefs$$.cfg >&8
${GDexe} load-opt=getdefs$$.cfg || bail_out cannot run ${GDexe}

# Make sure we have all our sources and generate the doc
#
for f in ${DOC_DEPENDS}
do test -f ${f} || bail_out cannot find doc file: ${f}
done
opts=`echo -t${timer} -L${srcdir} -L${top_srcdir} -L${top_srcdir}/autoopts`
echo + ${AGexe} ${opts} autogen.def >&8
${AGexe} ${opts} autogen.def || bail_out could not regenerate doc

set +x
exec 2>&8

# dump the temporary files
#
rm -f ${tmp_files}

exit 0

## Local Variables:
## Mode: shell-script
## indent-tabs-mode: nil
## sh-indentation: 2
## sh-basic-offset: 2
## End:
