#! /bin/sh
# for "lib4th", (C) 5/2001 berlin h-peter recktenwald <phpr@snafu.de>
# MK-h - generate html glossary, toc & labels ref. files
# requires <MK-hx-defs> for inclusion

# collect word names from "lib4words.inc" and asm sources, re below, ${s}
# no arguments required, execute from next directory level below main dir.
# working on tmp. files, thus the sources may be editted after invocation.

# "verbose" output, if any ${3} non-empty,

# the resultant html files biased to the "lynx" browser, the reason of
# which is it's orderly appearance, by the lynx specific <tab> tag, in
# the console display as well as in printed text and, fast rendering.
# other, graphic browsers were tested and, despite their very slow text
# (i.e. long tables') processing, were found almost equally 'readable'.
# formatting done for 132 chars/line.

# pse, mailto:phpr@snafu.de for supplemental information, if required...

# though higly specialized for the "lib4th" files the general approach
# might be useable for other asm sources, too. the main data structures:
#
#1) which references to generate:
#	names filtering depends on structure of args to the <crea> macro, 
#	generating references to words as listed in <l4words.inc>.
#	roughly:
#<type><..>crea<..> '{NAME}',<..>LABEL,<..>WORDLIST,<..>;<..>COMMENT
#
# wherein NAME, LABEL, WORDLIST, COMMENT are the rsp. entries, 
#<..> denoting zero or any number of blank spaces or <tab>s,
# "crea" entry may also read "acrea" or, "cvoc".
# everything else expected _exactly_ as noted above.
#<type> doesn't develop any activity, should be an empty %define in the
# asm source but, may optionally introduce a line, as an aid to further 
# index generation, e.g. wrt scope of words, &c.
#
# NOTE: substitutes required if {'} or {\} chars in NAME (re below).
#
#2) asm files comments pattern:
# .-line start ( here explantion, text will not be included )
#------------------------------------------------------------------------#
# ;-( {;-} 1st chars in line! nothing but <eol> code may follow)
# ;-<tab>NAME (any other text may follow)
# ;-(more text)
# LABEL:(mandatory, trailing {:} after NAME, any other text may follow)
#------------------------------------------------------------------------#
# (following text will not be included)

# # # -------------------------------------------------------------- # # #

unset d u hr hu

# xec from top-dir
[ "$(pwd -P)" = "$(dirname ${0})" ]&&cd ..

# whether echoing to the terminal, "progress monitor", somehow...
u=hu;[ -n "${3}" ]||u=hr
# whether by topic ref's generating, output to ${oq}
[ "${3}" = "topx" ]&&{ xx=xx;echo -e "\n\tgenerating sorted by topics list">/dev/tty; }||xx=

# # # pre-requisites tests
# ------------------------

# the actual top dir
cd ..
d=$(pwd|sed -e "s/hd[abcd][0-9a-f][0-9a-f]\?-[vxj]\///g")
# specific to actual host:
# used to cut off leading, linked dir from path names like </hddN-x/usr/src/...>

# PATH should include all files' and utils' directories
PATH=${PATH}:${d}/util:${d}/admin:${d}/inc:${d}/src

# fetch file names, text, common functions, substitution strings
source MK-hx-defs

# cleanup, in case of interrupted, previous html generation
rm -f ${y}* 2>/dev/null||true

# generate topic variables from wordlist, l4words.inc
h=
while read v
do
	[ "${v}" = ';glos>' ]&&break||{ [ -n "${h}" ]||{ [ "${v}" = ';<glos' ]&&h=xxx;continue; }; }
	r=${v#*[ 	]};r=${r%%[ 	]*};r=${r%%[ 	]*};g=${v##*;};g=${g//\'/&#039;}
	echo "k_${r}='<a name=\"#${r}\">${g//\</&#060;}</a>'">>${dc}/t$$
	echo "<li><a href=\"#${r}\">${g}</a></li>"
done<${i}|sort -t\> -k3,3>${dc}/h$$
source ${dc}/t$$

# index source, modify w/ NAMEs substitutions -> delayed until actual processing, re below
# in:	${i} abs path wordlist source-file
# out:	${r} list of word names, substitutions applied
r=${dc}/r$$
cp -f ${i} ${r}

# labels' sources, generate ${x}.l temp lbl ref; from ${s} list of (copied) asm source files
# and, references sources w/ text subst., files sizes into rsp "w${filename}"-vari
# in:	${s} -re- above
# out:	${i} reversed list of names; minimal, required substitutions applied
rm -f ${x}.l
for i in ${s}
do
	tac ${ds}${i}|sed -e 's/\&/\&amp;/g;s/</\&lt;/g;s/\"/\&quot;/g;s/*/\&#042;/g'>${dc}${i}
	grep -n "^[0-9a-zA-Z]\+:" ${dc}${i}|while read c;do echo "${c}:${i}";done
done>${x}.l

# # # auxilary shell functions
# ----------------------------

# additional voc & toc tables' header; no input, text to stdout; using ${og} for url
ht () {
echo "
<blockquote>
${cg}
<tr><td><tab to=\"i1\">Name</td><td><tab to=\"i3\">Asm-Label</td><td><tab to=\"i5\">Wordlist</td><td><tab to=\"i7\">Comment</td></tr>
<tr><td>&nbsp;</td><td><tab to=\"i3\">(<a href=\"${og}#immediate\">imed</a>)<br>&nbsp;<br></td><td>&nbsp;</td><td>&nbsp;</td></tr>
"
}

# html reference, leading comment field for <sort>, cut off from final file
#
# ${u}: whether echoing, variant selected w/ ${u}, dependent on 3rd arg to ${0}
# args:  "l4gls"#<2:label> <1:4thname> <5:label> <3:wordlist> <4:comment> <6:topic marker>
# (don't delete <tab> char after comment-field! used to cutting off after sorting)
hr () { echo "<!--${1} ${3} ${6} -->	<tr><td><tab to=\"i1\"><a name=\"${2}\" href=\"${og}#${2}\">${1//\</&lt;}</a></td><td><tab to=\"i3\">${5}</td><td><tab to=\"i5\">${3}</td><td><tab to=\"i7\">${4}</td></tr>"; }
# ditto, w/o echo
hu () { echo -n " ${1}">/dev/tty;hr "${@}"; }

# # # ref. files generation
# -------------------------
# file names -re- <MK-hx-defs>

# # # html glossaries
# -------------------

hh 'short reference, sorted by topic (preliminary)'>${y}
ht >>${y}
echo '<tr><td colspan="4"><ul>'>>${y}
cat ${dc}/h$$>>${y};#|sort -t\> -k3,3>>${y}
echo '</ul></td></tr>'>>${y}

# from ${r} collect $t (toc) - n:=name b:=label v:=vocabulary c:=comment u:=toc writing fu
echo >/dev/tty
grep -v '^;' ${r}|grep "\<crea\|\<acrea\|\<cvoc"|sed -e "s/';/'X8R/;s/;'/X8R'/;s/;code/X8Rcode/"|\
while read i;do
c=${i#*;};[ -n "${c}" ]||c="&nbsp;"
i=${i%%;*};i=${i//\ /};i=${i//\	/};z=${i::2};i=${i:2};i=${i//X8R/;};n=${i%\}*};n=${n#*\{};k=${n};n=${n#\'};n=${n%\'}
[ "${k}" != "${n}" ]||{ n=${n#\"};n=${n%\"}; }
k=${i##*\},};b=${k%%,*};[ -n "${b}" -a -n "${n}" ]||continue
k=${k:${#b}};k=${k:1};v=${k%%,*};k=${k:${#v}};k=${k:1};k=${k##*,};k=${k%;*};l=
[ -z "${k}" ]||l="$(grep -n "^[0-9]\+:${k}:\>" ${x}.l)"
[ -z "${l}" ]&&l="$(grep -n "^[0-9]\+:${b}:" ${x}.l)"||{ b=${k};[ -n "${l}" ]||l="${b}:${s%%\ *}"; }
f=${l##*:};l=${l%:*};i=${i%%,*}
m=$(echo ${i}|grep "imed\|ihed\|locvf\|ixec")&&m='<a name="'${b}'"href="#'${b}'">'${b}'</a>'||m=${b}
[ -n "${l}" ]&&${u} "${n}" ${b} "${v}" "${c//\</&lt;}" "${m}" "${z}"
done|sort -bf -k3,3 -k1,1 -k2,2|while read i
do
    k=k_"$(echo "${i}"|cut -d\  -f3)";[ -n "${!k}" ]||{ echo "K:${k}=${!k} I:${i}">/dev/tty; eval ${!k}='- n.n. -'; }
    [ "${k}" = "${j}" ]||{ echo "</table>${cg}<tr><td colspan=\"4\"><h4>&nbsp;<a name=\"${k:2:2}\"></a>${!k}</h4></td></tr>";j="${k}"; }
    echo "${i}"
done|cut -f2->>${y}
he "</table>" >>${y}
echo >/dev/tty

bell
echo -e "\t\a\"$(basename ${0})\" done,$(cat ${y}|wc -l) words">/dev/tty

sync
rm -f ${dc}/*$$
( 
    cd ./doc
    chmod 444 l4*html
    rm -f ${x}.l ${s} ${r} *$$
    [ -n "${hz}" ]&&${hz}&&wait&&{ mv l4*html.gz /hpg5/up-sna/ ;chmod 444 /hpg5/up-sna/l4*html.gz; }
    chmod 644 l4*html
)

# MK-h <eof>
