1#! /bin/sh
2
3##  This file is part of AutoGen.
4##
5##  AutoGen Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
6##
7##  AutoGen is free software: you can redistribute it and/or modify it
8##  under the terms of the GNU General Public License as published by the
9##  Free Software Foundation, either version 3 of the License, or
10##  (at your option) any later version.
11##
12##  AutoGen is distributed in the hope that it will be useful, but
13##  WITHOUT ANY WARRANTY; without even the implied warranty of
14##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15##  See the GNU General Public License for more details.
16##
17##  You should have received a copy of the GNU General Public License along
18##  with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20PS4='+mtx=${FUNCNAME:-=}-$LINENO> '
21exec 2> ${0%.sh}.log
22
23set -x
24
25typeset -r prog=$(basename "$0" .sh)
26typeset -r progdir=$(\cd $(dirname "$0") && pwd -P)
27typeset -r program=${progdir}/$(basename "$0")
28typeset -r progpid=$$
29
30builddir=`pwd`
31. ${top_builddir:-..}/config/shdefs
32
33die()
34{
35  exec 1> ${TMPDIR}/err-report.txt 2>&1
36  echo "mk-agen-texi FAILED: $*"
37  echo
38  cat ${LOG_FILE}
39  exec 2>&8 1>&2 8>&-
40  cat ${TMPDIR}/err-report.txt
41  trap : EXIT
42  echo leaving ${TMPDIR} in place
43  kill -TERM ${progpid}
44  exit 1
45}
46
47set_config_values()
48{
49  TMPDIR=`pwd`/ag-texi-$$.d
50  rm -rf ag-texi-*.d
51  mkdir ${TMPDIR} || die "cannot make ${TMPDIR} directory"
52  export TMPDIR
53
54  case "$-" in
55  *x* ) trap "echo 'saved tmp dir:  ${TMPDIR}';chmod 777 ${TMPDIR}" EXIT
56        VERBOSE=true ;;
57  *   ) trap "rm -rf ${TMPDIR}" EXIT
58        VERBOSE=false ;;
59  esac
60
61  LOG_FILE=${TMPDIR}/texi.log
62
63  exec 8>&2 2> ${LOG_FILE}
64
65  nl='
66' ht='	'
67  . ${top_builddir}/config/shdefs
68  : ${MAKE=`which make`}
69  : ${srcdir=`pwd`}
70  srcdir=`cd ${srcdir} >/dev/null ; pwd`
71  INCLUDES="${DEFS} "`
72
73    for d in ${top_srcdir} ${top_builddir} \
74             ${top_builddir}/autoopts ${top_srcdir}/autoopts
75    do
76      (\cd ${d} && pwd) 2>/dev/null
77    done | \
78      sort -u | \
79      sed s/^/-I/ `
80
81  CFLAGS="${INCLUDES} "`echo ${CFLAGS} | \
82    ${SED:-sed} -e "s/-Werror[^ ${ht}]*//g;s/-Wextra//g"`
83
84  LIBS=-L`
85    test -d ${top_builddir}/autoopts/.libs \
86      && echo ${top_builddir}/autoopts/.libs \
87      || echo ${top_builddir}/autoopts
88    `" $LIBS"
89
90  export CC CFLAGS LIBS MAKE LOG_FILE TMPDIR
91}
92
93setup_exports()
94{
95  # Now auto-export variables:
96  #
97  set -a
98
99  PATH=${top_builddir}/columns:${PATH}
100  timer=`expr ${AG_TIMEOUT} '*' 5`
101  d=`find ${top_builddir}/autoopts -type f -name libopts.a -print`
102  test -f "$d" || die "Cannot locate libopts.a"
103  LIBS="$d ${LIBS}"
104
105  eval `${EGREP} '^AG_[A-Z_]*' ${top_srcdir}/VERSION`
106
107  AGsrc=${top_srcdir}/agen5
108  OPTIONS_DEF=${AGsrc}/opts.def
109  GETDEF_SRC=`${FGREP} -l '/*=' ${AGsrc}/*.[ch] ${AGsrc}/*.scm`
110
111  for d in "${top_builddir}" "${top_srcdir}"
112  do test -f "$d/agen5/invoke-autogen.texi" || continue
113     AGEN_TEXI=${d}/agen5/invoke-autogen.texi
114     break
115  done
116  DOC_TEXT=${top_srcdir}/doc/autogen-texi.txt
117
118  ADDON_TEXI="
119    ${top_srcdir}/doc/bitmaps.texi
120    ${top_builddir}/columns/invoke-columns.texi
121    ${top_builddir}/getdefs/invoke-getdefs.texi
122    ${top_builddir}/xml2ag/invoke-xml2ag.texi
123    ${top_srcdir}/doc/snprintfv.texi"
124
125  DOC_INCLUDES="
126    ${AGsrc}/defParse-fsm.c
127    ${AGsrc}/opts.h
128    ${top_builddir}/autoopts/libopts.texi
129    ${top_srcdir}/doc/autogen-intro.texi
130    ${AGEN_TEXI}"
131
132  DOC_TEMPLATE=${top_builddir}/doc/auto_gen.tpl
133
134  DOC_DEPENDS=`
135    echo ${DOC_TEMPLATE} ${OPTIONS_DEF} ${ADDON_MENU} ${ADDON_TEXI} \
136         ${DOC_INCLUDES} ${GETDEF_SRC}  ${DOC_TEXT}`
137
138  set +a
139}
140
141# We have our executables and texi's.  Collect the definitions:
142#
143run_getdefs()
144{
145  gd_cfg=${TMPDIR}/getdefs.cfg
146  exec 3> ${gd_cfg}
147  cat >&3 <<-  EOCat
148	output      ${TMPDIR}/${GEN_BASE}.def
149	copy        ${OPTIONS_DEF}
150	srcfile
151	linenum
152	template    auto_gen.tpl
153	assign      ag-texi = invoke-autogen.texi
154	subblock    exparg  = arg_name,arg_desc,arg_optional,arg_list
155	EOCat
156
157  tf=invoke-autogen.texi
158  test -f ${tf} || ln -s ${AGEN_TEXI} ${tf}
159
160  for f in ${ADDON_TEXI}
161  do
162    tf=`basename ${f}`
163    case "$tf" in
164    invoke-* ) : ;;
165    * ) tf=invoke-$tf ;;
166    esac
167    test -f ${tf} || ln -s ${f} ${tf}
168    echo "assign      addon-texi = ${tf}"
169  done >&3
170
171  for f in ${GETDEF_SRC}
172  do
173    echo "input      " ${f}
174  done >&3
175  exec 3>&-
176  echo + ${GDexe} load-opt=${gd_cfg} >&8
177  ${GDexe} load-opt=${gd_cfg} || die cannot run ${GDexe}
178}
179
180sanity_check()
181{
182  # Make sure the executables are there
183  #
184  test -x ${AGexe} || (cd `dirname ${AGexe}` ; ${MAKE}) || exit 0
185  test -x ${GDexe} || (cd `dirname ${GDexe}` ; ${MAKE}) || exit 0
186  test -x ${CLexe} || (cd `dirname ${CLexe}` ; ${MAKE}) || exit 0
187  PATH="`dirname ${AGexe}`:`dirname ${CLexe}`:$PATH"
188
189  # See to it that the .texi files have been generated, too.
190  #
191  for f in ${ADDON_TEXI} ${AGEN_TEXI} \
192           ${top_builddir}/autoopts/libopts.texi
193  do
194    test -f "${f}" || (
195      cd `dirname "${f}"`
196      ${MAKE} `basename "${f}"` >&2
197      test $? -ne 0 && die MAKE of ${f} failed.
198    )
199  done
200
201  # Make sure we have all our sources and generate the doc
202  #
203  for f in ${DOC_DEPENDS}
204  do test -f "${f}" || die cannot find doc file: ${f}
205     test -f `basename $f` || ln -s "${f}" .
206  done
207}
208
209build_agdoc() {
210  #  Validate everything:
211  #
212  set_config_values
213  setup_exports
214  sanity_check
215  run_getdefs
216
217  {
218    sh -c set | sed '/^BASH/d'
219  } >&2
220  {
221    cat <<- _EOF_
222	timeout     ${timer}
223	templ-dirs  ${srcdir}
224	templ-dirs  ${top_srcdir}/autoopts/tpl
225	base-name   ${GEN_BASE}
226	make-dep    F ${GEN_BASE}.dep
227	make-dep    P
228	_EOF_
229    ${VERBOSE} && {
230      echo 'trace       every'
231      echo "trace-out   >>${TMPDIR}/ag.log"
232      export VERBOSE
233    }
234  } > ${TMPDIR}/ag.ini
235
236  opts="--load-opts=${TMPDIR}/ag.ini"
237  cmd=`echo ${AGexe} ${opts} ${TMPDIR}/${GEN_BASE}.def`
238  echo "${PS4:-+} " ${cmd} >&8
239
240  timeout ${timer}s ${cmd} || {
241    head -n999999 ${TMPDIR}/ag.ini ${TMPDIR}/*.log
242    die could not regenerate doc
243  } >&2
244
245  test -f ${GEN_BASE}.texi || die "MISSING: ${GEN_BASE}.texi"
246
247  exec 2>&8 8>&-
248}
249
250build_gnudocs()
251{
252  local sedcmd='/^@author @email/ {
253    s/.*{//
254    s/}.*//
255    s/@@*/@/g
256    p
257    q
258  }'
259
260  case "X$-" in
261    *x* ) local dashx=-x ;;
262    *   ) local dashx=   ;;
263  esac
264
265  title=`sed -n 's/^@title  *//p' agdoc.texi`
266  email=--email' '`sed -n "$sedcmd" agdoc.texi`
267  opts="--texi2html ${email}"
268  PS4='+gd=${FUNCNAME:-=}-$LINENO> ' ${SHELL} ${dashx} \
269    ${top_srcdir}/config/gendocs.sh $opts autogen "$title"
270}
271
272mk_autogen_texi() {
273  tfile=autogen.texi
274  page_style=\
275'\internalpagesizes{46\baselineskip}{6in}{-.25in}{-.25in}{\bindingoffset}{36pt}%'
276
277  cat > ${tfile}$$ <<- _EOF_
278	\\input texinfo
279	@ignore
280	${page_style}
281	@end ignore
282	@c %**start of header
283	@setfilename ${tfile%.texi}.info
284	@include ${GEN_BASE}.texi
285	_EOF_
286
287  if test -f ${tfile} && cmp -s ${tfile} ${tfile}$$
288  then rm -f ${tfile}$$
289  else mv -f ${tfile}$$ ${tfile}
290  fi
291}
292
293PS4='+agt=${FUNCNAME:-=}-$LINENO> '
294set -x
295GEN_BASE=agdoc
296test "X$1" = X--force && {
297  rm -f agdoc.texi
298  shift
299}
300test -f agdoc.texi || build_agdoc
301mk_autogen_texi
302
303case "$1" in
304gnudocs | gnudoc ) build_gnudocs ;;
305* )
306esac
307
308exit 0
309
310## Local Variables:
311## mode: shell-script
312## indent-tabs-mode: nil
313## sh-indentation: 2
314## sh-basic-offset: 2
315## End:
316