#! /bin/sh
# for "lib4th", (C) 11/2001 berlin h-peter recktenwald <phpr@snafu.de>
# MK-h - generate html glossary, toc & labels ref. files 
#	 requires <MK-hx-defs> for inclusion
# for main html reference files, sorted by topic and, cross-ref. list 

# 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)

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

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

unset d u hr hu

# whether echoing to the terminal, "progress monitor", somehow...
u=hu;[ -n "${3}" ]||u=hr
# whether cross ref's generating, output to ${oq}
[ "${1}" = "html" -a -n "${3}" ]&&{ xx=xx;echo -e "\n\tgenerating sorted by names 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

# clear working files, in case of interrupted, previous html generation
rm -f ${g}* ${t}* ${e}* ${x}* 2>/dev/null||true

# index source, modify w/ NAMEs substitutions -> re below, main loop
# in:	${i} abs path wordlist source-file
# out:	${r} list of word names, substitutions applied
ii=${i}

# references sources, files sizes into rsp "w${filename}"-vari
# in:	${s} -re- above
# out:	${i} reversed list of names; minimal, required subset of substitutions applied
#	${w$(basename ${s})} the rsp. files' size
for i in ${s}
do
	tac ${ds}${i}|sed -e 's/\&/\&amp;/g;s/</\&lt;/g;s/\"/\&quot;/g;s/*/\&#042;/g;s!\\!\&#092;!g'>${dc}${i}
	wi=${i##*/};wi=w${wi%\.*};let ${wi}="$(($(cat ${dc}${i}|wc -l)))"
	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>
"
}

# labels reference additional header; no input, text to stdout; no vari
hl () {
echo "
<blockquote>
${cg}
<tr><td><tab to=\"i1\">Asm-Label</td><td><tab to=\"i4\">Source</td><td><tab to=\"i7\">Ref'd</td></tr>
"
}

# html reference, leading comment field for <sort>, cut off from final file
#
# ${u}: echoing or not 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 "${@}"; }

# html glossary entry (text generated last line 1st)
# mist: <while>-schleife separater job, wert "$ne" kommt auszerhalb nicht an!
# $1:name $2:label $3:lines $4:dir+file
hg () {
    echo "</pre><pre><a href=\"${oq}#${2}\">=></a>"
    ne=0;wi=w${4##*/};wi=${wi%\.*};la=${3%%:*}
    tail -n$((${!wi}-${la})) ${4}|while read w
    do [ -n "$(echo ${w}|grep '^;')" ]&&ne=$((${ne}+1))||{ [ $((${ne})) -gt 0 ]&&head -n$((${la}+${ne})) ${4}|tail -n${ne}|grep '^;-'|cut -d- -f2-||echo -e " ${1}\t\t\t\t\t\t(k)";break; };done
    echo "
<a name=\"${2}\"></a>"
}

# label ref line
# label-name (for simplicity, all linked to $ot -l4toc.html- whether existent or not), source-file name
# in:	$1 label, $2 source-file
# vari:	$ot (toc), $ds (src dir), $oq (xrf)
hx () {
echo "<!--${1} -->	<tr><td><tab to=\"i1\"><a name=\"${1}\" href=\"${ot}#${1}\">${1}</a></td>\
<td><tab to=\"i4\"><a href=\"file://localhost${ds}${2}\">${2}</a></td>\
<td><tab to=\"i7\"><a href=\"${oq}#${1}\">=></a></td></tr>"
}

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

# labels' sources, generate ${x}.t temp lbl ref; from ${s} list of (copied) asm source files
hh "Labels' Reference" >${x}
hl >>${x}
while read "c";do c=${c#*:};i=${c##*:};c=${c%%:*}; hx ${c} ${i};done<${x}.l|sort -bf -k1,1|cut -f2->>${x}
he "</table>">>${x}

# generate glossary of annotations
c=$(($(grep -n 'BGN for the html docs' ${p}|cut -d: -f1)))
i=$(($(grep -n 'END for the html docs' ${p}|cut -d: -f1)-1))
hh 'Annotations Glossary'>${o}
echo '<blockquote><pre>'>>${o}
head -n${i} ${p}|tail -n$((${i}-${c}))|sed -e 's/;-//g;s/</\&lt;/g'>>${o}
he "</pre>" >>${o}

# from ${ii} collect $g (gls) & $t (toc)
# n:=name b:=label v:=vocabulary l:lbl-defn-line k:pfax c:=comment z:=topic key ; u:=toc writing fu
echo >/dev/tty
grep -v '^;' ${ii}|grep "\<crea\|\<acrea\|\<cvoc"|while read "i"
do
c=${i#*; };c=${c//&/&amp;};c=${c//</&lt;}
[ -z "${ANSDIR}" ]||\
    [ -n "${c}" ]&&{
    k="${c##*dpans\ }"
    [ "${k}" = "${c}" ]||{ k="${k%%\ *}";c="${c%dpans\ *} <a href=\"${ANSDIR}/dpansf.htm#${k}\">${k}</a> ${c##*dpans\ }"; }
}||c="&nbsp;"
i=${i%%; *};i=${i//\ /};i=${i//\	/};z=${i::2};i=${i:2};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 "^[0-9]\+:${k}:\>" ${x}.l)"
[ -z "${l}" ]&&l="$(grep "^[0-9]\+:${b}:" ${x}.l)"||{ b=${k};[ -n "${l}" ]||l="${b}:${s%%\ *}"; }
f=${l##*:};l=${l%:*};i=${i//\"/\&quot;};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}" "${m}" "${z}">>${t}.t;hg "${n}" ${b} "${l}" "${dc}${f}"; }||echo "N:${n}: B:${b}: V:${v}: M:${m}: Z:${z}: F:${f}:">>${e}
done>${g}.t
echo >/dev/tty

# generate ${y}: tic (by topic)
echo -e "\n\tref by topic">/dev/tty
hh 'short reference, sorted by topic (preliminary)'>${y}
ht >>${y}
echo '<tr><td colspan="4"><ul>'>>${y}
h=
f=${dc}/t$$
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>'">>${f}
	echo "<li><a href=\"#${r}\">${ft}${g}${tf}</a></li>"
done<${ii}|sort -t\> -k3,3>>${y}
echo '</ul></td></tr>'>>${y}

source ${f}
rm -f ${f}
# topics ref. list: ${t}.t raw names input file, ${k}:topic marker vari name
sort -bf -k3,3 -k1,1 -k2,2 ${t}.t|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>${ft}${!k}${tf}</h4></td></tr>";j="${k}"; }
    echo "${i}"
done|cut -f2->>${y}
he "</table>" >>${y}
echo >/dev/tty

# generate ${g}: gls (asm comments extract)
hh 'glossary'>${g}
echo "
<pre><small>re \"<a href=\"${p}#Nomina\">Nomina</a>\" in system description file &lt;${pf}>, for abbreviations and special terms.
Words marked \"(k)\" or, where \"Vocabulary\" column empty, are not accessible by Name.</small>

Name		Stack( in -- out)		(<a href=\"${on}\">note</a>)		Asm Label	Vocabulary
<hr width=\"100%\">
</pre><blockquote>
" >>${g}
tac ${g}.t|sed -e 's/\&#042;/*/g'>>${g}
he >>${g}

# generate ${t}: toc (by names)
hh 'short reference, sorted by words'>${t}
ht >>${t}
#sort -bf -k1,1 ${t}.t|cut -f2-|sed -e 's/\&#042;/*/g'>>${t}
sed -e 's/\&#042;/*/g'|sort -bf -k1,1 ${t}.t|cut -f2->>${t}
he "</table>" >>${t}

# generate ${a}: voc (by vocs)
hh 'short reference, sorted by vocabularies'>${a}
ht >>${a}
sort -bf -k2,2 -k1,1 ${t}.t|cut -f2-|sed -e 's/\&#042;/*/g' >>${a}
he "</table>" >>${a}
echo -e "\t\a\"$(basename ${0})\" done,$(cat ${t}.t|wc -l) words">/dev/tty
sync

# modify <dpansf.htm> ref. lines e.g.
# from	<A href=dpans6.htm#6.1.0010>.6.1.0010</A> ...
# to	<A name="6.1.0010" href=dpans6.htm#6.1.0010>.6.1.0010</A> ...
[ -n "${ANSDIR}" ]&&\
{
    grep '<A name="6.1.0010" ' ${ANSDIR}/dpansf.htm>/dev/null||
    {
	cp ${ANSDIR}/dpansf.htm d.$$
	mv --backup=t ${ANSDIR}/dpansf.htm  ${ANSDIR}/dpansf.htm.bup
	BIFS=${IFS};IFS=
	while read i
	do
	    n=${i#*<A\ href=dpans*};[ "${n}" != "${i}" ]||{ echo ${i};continue; }
	    n=${i#*#};[ "${n}" != "${i}" ]||{ echo ${i};continue; }
	    n=${n%%>*};[ "${n}" != "${i}" ]||{ echo ${i};continue; }
	    [ -n "${n}" ]&&echo -e "${i%%<A\ *}<a name=\"${n}\" ${i#*<A\ }"||echo ${i}
	done<d.$$>${ANSDIR}/dpansf.htm;wait
	IFS=${BIFS};rm -f d.$$
    }
}

(
    cd ./doc
    chmod 444 l4*html
    rm -f ${g}.t ${x}.l ${x}.t ${t}.t ${s}
    [ -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>
