#!/bin/sh
#
# dwww-build -- build HTML pages for dwww
#
# Part of the dwww package.  Written by Lars Wirzenius.
# "@(#)dwww:$Id: dwww-build,v 1.2 1996/09/25 18:43:07 liw Exp $"

dir=/tmp/dwww-build.$$
lib=/usr/lib/dwww

DWWW_HTMLDIR=/var/lib/dwww
if test -f /etc/dwww.conf
then
	. /etc/dwww.conf
fi

if test "$1" = "--default"
then
	tgt="$DWWW_HTMLDIR"
else
	tgt="$1"
fi

test -d "$tgt" || mkdir "$tgt" || exit 1

#
# Find all manual page files.
#
find_man() {
	for i in `manpath -q | tr : " "`
	do
		find "`cd $i; /bin/pwd`" -type f -o -type l
	done
}


#
# Output a list of sections.
#
find_sections() {
	sed 's#.*/##;s#\.gz$##;s#\.Z$##;s#.*\.##' $dir/manlist | sort -u
}


#
# Find all first characters in manual page names.
#
find_letters() {
	nawk '{	sub(/.*\//, ""); print substr($0,1,1) }' $dir/manlist |
		tr '[a-z]' '[A-Z]' | sort -u
}


#
# Build lists of manual pages according to first letter.
#
man_by_name() {
	for i in $LETTERS
	do
		sed "s/LETTER/$i/g" $lib/man-begins-with.start \
			> $dir/man-begins-with-$i.html
	done

	nawk '{
		base = $0
		sub(/.*\//, "", base)
		section = base
		sub(/.*\./, "", section)
		sub(/\.[^.]*/, "", base)
		print section, base, $0
	}' $dir/manlist |
	sort |
	nawk 'BEGIN {
		dir = "'$dir'"
		z = "abcdefghijklmnopqrstuvwxyz"
		Z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		for (i = 1; i <= length(z); ++i) {
			x = substr(z,i,1)
			X = substr(Z,i,1)
			a[x] = X
			have[X] = 0
		}
	}
	{
		section = $1
		base = $2
		filename = $3

		x = substr(base,1,1)
		if (a[x] != "") x = a[x]
		have[x] = 1
		if (fn[x] == "") fn[x] = dir "/man-begins-with-" x ".html"
		if (s[x] != section) { 
			s[x] = section
			print "<p><b>Section " section "</b>: " >> fn[x]
		}
		print \
		"<a href=\"/cgi-bin/dwww?type=man&location=" \
		filename "\">" base "</a>" >> fn[x]
	}
	END {
		for (i in have) {
			if (!have[i]) {
				print "No manual pages beginning with " i \
				>> fn[i]
			}
			print "<p>" >> fn[i]
		}
	}'
	
	for i in $LETTERS
	do
		sed "s/DATE/$DATE/g" $lib/man-begins-with.end \
			>> $dir/man-begins-with-$i.html
	done
}


#
# Build lists of manual pages for each section.
#
man_by_section() {
	for i in $SECTIONS
	do
		sed "s/SECTION/$i/g" $lib/man-in-section.start \
			> $dir/man-in-section-$i.html
	done

	nawk '{
		base = $0
		sub(/.*\//, "", base)
		sub(/\.gz$/, "", base)
		sub(/\.Z$/, "", base)
		section = base
		sub(/.*\./, "", section)
		sub(/\.[^.]*/, "", base)
		print section, base, $0
	}' $dir/manlist |
	sort |
	nawk 'BEGIN {
		dir = "'$dir'"
	}
	{
		section = $1
		base = $2
		filename = $3
		
		if (fn[section] == "") 
			fn[section] = dir "/man-in-section-" section ".html"

		x = substr(base,1,1)
		if (s[section] != x) {
			s[section] = x
			print "<p><b>" x "</b>: " >> fn[section]
		}
		print \
		"<a href=\"/cgi-bin/dwww?type=man&location=" \
		filename "\">" base "</a>" >> fn[section]
	}'
	
	for i in $SECTIONS
	do
		sed "s/DATE/$DATE/g" $lib/man-in-section.end \
			>> $dir/man-in-section-$i.html
	done
}


#
# Build list of GNU Info files.
#
make_info() {
	cat $lib/info.start > $dir/info.html
	i="`cd /usr/info; /bin/pwd`"
	sed 's#\* .*: (\(.*\))\.#<a hreF="/cgi-bin/dwww?type=info\&location='"$i"'/\1">&</a>#' \
		"$i" >> $dir/info.html
	
	cat $lib/info.end >> $dir/info.html
}


#
# Build list of copyright statements.
#
make_copyrights() {
	cat $lib/copyrights.start > $dir/copyrights.html
	i="`cd /usr/doc/copyright; /bin/pwd`"
	(cd "$i"; find -type f) |
	sed 's#^\./##;
s#.*#<a href="/cgi-bin/dwww?type=text\&location='"$i"'/&">&</a>#' \
>> $dir/copyrights.html
	sed "s/DATE/$DATE/g" $lib/copyrights.end >> $dir/copyrights.html
}


#
# Create list of package specific documentation directories.
#
make_package_dirs() {
	cat $lib/packagedoc.start > $dir/packagedoc.html
	i="`cd /usr/doc; /bin/pwd`"
	(cd "$i"; \
	find . -type d -maxdepth 1 ! -name copyright ! -name examples \
	) | sort | grep -v '^\.$' |
	sed 's#^\./##;s#.*#<a href="/cgi-bin/dwww?type=dir\&location='"$i"'/&">&</a>#' \
	>> $dir/packagedoc.html
	sed "s/DATE/$DATE/g" $lib/packagedoc.end >> $dir/packagedoc.html
}


#
# Create the front page.
#
make_front_page() {
	for i in $LETTERS
	do
		xx="$xx <a href=\"man-begins-with-$i.html\">$i</a>"
	done
	for i in $SECTIONS
	do
		yy="$yy <a href=\"man-in-section-$i.html\">$i</a>"
	done
	sed "s#DATE#$DATE#g;s#SECTIONS#$yy#g;s#LETTERS#$xx#g" \
		$lib/dwww.template > $dir/dwww.html
}



mkdir $dir || exit 1

echo "Building dwww pages (a-h):"

echo -n "  a) list of manual pages... "
find_man | sort > $dir/manlist
echo "done"

DATE="`date`"
SECTIONS="`find_sections`"
LETTERS="`find_letters`"

echo -n "  b) manual pages by name... "
man_by_name
echo "done"

echo -n "  c) manual pages by section... "
man_by_section
echo "done"

echo -n "  d) Info files... "
# make_info
echo "done"

echo -n "  e) package specific directories... "
make_package_dirs
echo "done"

echo -n "  f) copyrights... "
make_copyrights
echo "done"

echo -n "  g) front page... "
make_front_page
echo "done"

echo -n "  h) copying files... "
chmod u=rw,go=r $dir/*.html
cp $dir/*.html $lib/*.html $tgt/.
rm -rf $dir
echo "done"

echo "The dwww pages have been re-built."
