#! /bin/sh

# Time-stamp:        "2010-09-07 17:35:48 bkorb"
#
##
##  This file is part of AutoGen.
##
##  AutoGen Copyright (c) 1992-2010 by Bruce Korb - all rights reserved
##
##  AutoGen is free software: you can redistribute it and/or modify it
##  under the terms of the GNU General Public License as published by the
##  Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  AutoGen is distributed in the hope that it will be useful, but
##  WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
##  See the GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License along
##  with this program.  If not, see <http://www.gnu.org/licenses/>.

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

die()
{
  exec 2>&8 1>&2 8>&-
  echo "mk-agen-texi FAILED: $*"
  echo
  cat ${LOG_FILE}
  trap : EXIT
  echo leaving ${tmp_dir} in place
  kill -TERM ${parent_pid}
  exit 1
}

set_config_values()
{
  tmp_dir=`pwd`/ag-texi-$$.d
  rm -rf ag-texi-*.d
  mkdir ${tmp_dir} || die "cannot make ${tmp_dir} directory"
  TMPDIR=${tmp_dir}
  export TMPDIR

  case "$-" in
  *x* ) trap "echo 'saved tmp dir:  ${tmp_dir}';chmod 777 ${tmp_dir}" EXIT
        VERBOSE=true ;;
  *   ) trap "rm -rf ${tmp_dir}" EXIT
        VERBOSE=false ;;
  esac
  LOG_FILE=${tmp_dir}/texi.log
  exec 8>&2 2> ${LOG_FILE}

  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 @top_builddir@ @top_srcdir@ \
             @top_builddir@/autoopts @top_srcdir@/autoopts
    do
      (\cd ${d} && pwd) 2>/dev/null
    done | \
      sort -u | \
      sed s/^/-I/ `

  CFLAGS="${INCLUDES} ${CFLAGS}"

  # Give preference to configured SHELL
  #
  for SHELL in @CONFIG_SHELL@ @SHELL@ `which bash` `which sh` /bin/sh
  do test -x ${SHELL} && break ; done
  export CC CFLAGS LIBS MAKE SHELL LOG_FILE tmp_dir
}

setup_exports()
{
  # 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`
  d=`find ${top_builddir}/autoopts -type f -name libopts.a`
  LIBS="$d ${LIBS}"

  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} `

  AGEN_TEXI=${top_builddir}/agen5/autogen.texi
  DOC_TEXT=${top_srcdir}/doc/autogen-texi.txt

  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
    echo ${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} \
         ${DOC_INCLUDES} ${GETDEF_SRC}  ${DOC_TEXT}`

  GEN_BASE=agdoc
  set +a
}

# We have our executables and texi's.  Collect the definitions:
#
run_getdefs()
{
  gd_cfg=${tmp_dir}/getdefs.cfg
  exec 3> ${gd_cfg}
  cat >&3 <<-  EOCat
	output      ${tmp_dir}/${GEN_BASE}.def
	copy        ${OPTIONS_DEF}
	srcfile
	linenum
	template    auto_gen.tpl
	assign      ag-texi = invoke-`basename ${AGEN_TEXI}`
	EOCat

  ln -s ${AGEN_TEXI} invoke-`basename ${AGEN_TEXI}`

  for f in ${ADDON_TEXI}
  do
    ln -s ${f} ./invoke-`basename ${f}`
    echo "assign      addon-texi = invoke-`basename ${f}`"
  done >&3

  for f in ${GETDEF_SRC}
  do
    echo "input      " ${f}
  done >&3
  exec 3>&-
  echo + ${GDexe} load-opt=${gd_cfg} >&8
  ${GDexe} load-opt=${gd_cfg} || die cannot run ${GDexe}
}

sanity_check()
{
  # 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

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

  # Make sure we have all our sources and generate the doc
  #
  for f in ${DOC_DEPENDS}
  do test -f ${f} || die cannot find doc file: ${f}
  done

  cmd=${AGexe}
  ${VERBOSE} && cmd=${cmd}" --trace=every --trace-out=>>${tmp_dir}/ag.log"
}

#  Validate everything:
#
set_config_values
setup_exports
printf '\n\n\n\n' >&2

sanity_check
run_getdefs

env >&2
exec 3> ${tmp_dir}/ag.ini
cat >&3 <<- _EOF_
	timeout     ${timer}
	templ-dirs  ${srcdir}
	templ-dirs  ${top_srcdir}
	templ-dirs  ${top_srcdir}/autoopts
	base-name   ${GEN_BASE}
	_EOF_
${VERBOSE} && {
  echo 'trace       every'
  echo "trace-out   >>${tmp_dir}/ag.log"
} >&3
exec 3>&-

opts=--load-opts=${tmp_dir}/ag.ini
cmd=`echo "${cmd}" ${opts} ${tmp_dir}/${GEN_BASE}.def`
echo "${PS4:-+} " ${cmd} >&8

${cmd} || die could not regenerate doc

cat > autogen.texi <<- _EOF_
	\input texinfo
	@include ${GEN_BASE}.texi
	_EOF_
test -f ${GEN_BASE}.texi || die "MISSING: ${GEN_BASE}.texi"

exec 2>&8 8>&-

exit 0

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