#!/bin/sh
#
# Päivitä sfnetin ryhmälista (*.input -> *.html).

rm -f /tmp/sfnet_temp* sfnet.kaikki.input

cat *.input | grep '^<dt>' | sed 's/^<dt>//' > /tmp/sfnet_temp1.$$
mv /tmp/sfnet_temp1.$$ sfnet.checkgroups

rm -f sfnet.kaikki.input
cat sfnet.kaikki.alku > /tmp/sfnet_temp1.$$
for i in *.input
do
	sed '1,/<dl>/d' $i | sed '/<\/dl>/,$d' >> /tmp/sfnet_temp1.$$
done
cat sfnet.kaikki.loppu >> /tmp/sfnet_temp1.$$
mv /tmp/sfnet_temp1.$$ sfnet.kaikki.input

for i in *.input
do
	sed '/%%%LISTA%%%/,$d' $i > /tmp/sfnet_temp1.$$

	echo '<p>' >> /tmp/sfnet_temp1.$$
	
	grep '^<dt>' $i | sed 's/^<dt>\([^ ]*\) .*/\1/' | 
	awk '	BEGIN { FS="." }
		{ y[NR] = $2; x[NR] = $0; 
		  for (i=3; i<=NF; ++i) z[NR] = z[NR] "." $i }
		END {
			print "<ul>"
			for (i = 1; i <= NR; ++i) {
				if (y[i] != y[i-1]) \
					printf "<li> <strong>sfnet.%s</strong>:", y[i]
				if (z[i] == "") z[i] = y[i]
				printf " &lt;<a href=\"#%s\">%s</a>&gt;", x[i], z[i]
				printf "\n"
			}
			print "</ul>"
		}
	' >> /tmp/sfnet_temp1.$$

	sed '1,/%%%LISTA%%%/d' $i |
	sed 's/^<dt>\([^ ]*\)/<hr><dt><a name="\1"><strong>\1<\/strong><\/a>/' \
		>> /tmp/sfnet_temp1.$$
	mv /tmp/sfnet_temp1.$$ `basename $i .input`.html
done

lynx -nolist -force_html -dump sfnet.kaikki.input | grep -v '%%%' > /tmp/sfnet_temp1.$$
mv /tmp/sfnet_temp1.$$ sfnet.kaikki.txt

rm -f /tmp/sfnet_temp*.$$

chmod a+r *
chmod g+w *
