#!/bin/bash
#Copyright 2002 William Stearns <wstearns@pobox.com>
#Released under the GPL.
#grep '^[A-Z]' to separate the uc/lc's.


unset CDPATH	#CDPATH screws up the directory listings - thanks Stephen!
POMListVer='0.2.2'
echo pomlist \(Patch-o-matic\) file lister, version $POMListVer. >>/dev/stderr

fail () {
	echo "$* Exiting." >>/dev/stderr
	exit 1
}

usage () {
	#FIXME - how how to set base filename once that is possible.
	echo 'pomlist, the patch-o-matic file html formatter, version '$POMListVer' .' >>/dev/stderr
	echo 'Usage: ' >>/dev/stderr
	echo '  pomlist [/path/to/p-o-m/directory] [fullpage] [http://baseurl/for/browsing]' >>/dev/stderr
	echo >>/dev/stderr
	echo 'The p-o-m directory is the directory containing "runme"' >>/dev/stderr
	echo 'If missing, it defaults to the current directory ' >>/dev/stderr
	echo '"fullpage" tells pomlist to construct the entire html page, ' >>/dev/stderr
	echo 'as opposed to the default html body text.' >>/dev/stderr
	echo 'The optional URL base is one that will be used in the hypertext ' >>/dev/stderr
	echo 'links to the actual files if specified.  Otherwise, links to the ' >>/dev/stderr
	echo 'local files will be used.' >>/dev/stderr
	echo >>/dev/stderr
	echo 'Examples: ' >>/dev/stderr
	echo '  pomlist /usr/src/iptables-1.2.6a/patch-o-matic fullpage' >>/dev/stderr
	echo '  pomlist /usr/src/iptables-1.2.6a/patch-o-matic fullpage http://cvs.samba.org/cgi-bin/cvsweb/~checkout~/netfilter/userspace/patch-o-matic/' >>/dev/stderr
	echo '  pomlist /usr/src/iptables-1.2.6a/patch-o-matic fullpage file:/usr/src/iptables-1.2.6a/patch-o-matic/' >>/dev/stderr
	exit 0
}

DoHeader () {
	if [ "$Fullpage" = "yes" ]; then
		cat <<EOHEADER
<html>
<head>
<title>Patch-o-matic $1 patch listing</title>
</head>
<body>
EOHEADER
	fi

	echo '<!-- This was created by running: "'$FullParams'" -->'	
	echo '<!-- pomlist (Patch-o-matic) file lister, version '$POMListVer'. -->'
	echo '<!-- http://www.stearns.org/pomlist/ -->'
	echo '<!-- For more information on netfilter, please see http://www.netfilter.org -->'
}


DoBody () {
#Parameters:
#$1: Name of the Suite being worked on, or 'combined' or "summary' for the top level

	if [ "$1" = "summary" ] || [ "$1" = "combined" ]; then
		Wildcard='*'
		for OneDir in * ; do
			if [ -d $OneDir ] && [ "$OneDir" != "CVS" ]; then
				DirList="$DirList $OneDir"
			fi
		done
	else
		Wildcard="$1"
		DirList="$1"
	fi

	#echo '<h2><a name="top">'$1 'patch listing</a></h2>'	#Netfilter web site generates its own - thanks Stephen!
	echo '<a name="top"> </a>'

	echo '<p>Individual Suite pages:</p>'

	echo '[<a href="'${OutputBase##*/}-summary.html'">summary</a>] ' 
	echo '[<a href="'${OutputBase##*/}-combined.html'">combined</a>] ' 
	for DirLink in * ; do
		if [ -d $DirLink ] && [ "$DirLink" != "CVS" ]; then
			echo '[<a href="'${OutputBase##*/}-$DirLink.html'">'$DirLink'</a>] ' 
		fi
	done
	echo

	#It isn't quite the case that all uppercase modules are targets, all
	#lower are matches.  We'll just do a raw list.
	#Besides, bash on my system isn't letting me list or case upper vs. lower correctly.  *sigh*


	if [ "$1" != "summary" ]; then
		echo '<h3>ipv4 patches</h3>'

		if [ -n "`ls -A1 $Wildcard/*.patch.help  2>/dev/null | sed -e 's@.*/@@' | sort`" ]; then
			for OnePatch in `ls -A1 $Wildcard/*.patch.help  2>/dev/null | sed -e 's@.*/@@' | sort` ; do
				PatchName=`echo $OnePatch | sed -e 's/.patch.help//'`
				PatchPage=''
				echo -n '[<a href="'$PatchPage'#'$PatchName'">'$PatchName'</a>] ' 
			done
		else
			echo '<i>None</i>'
		fi
		echo

		echo '<h3>ipv6 patches</h3>'

		if [ -n "`ls -A1 $Wildcard/*.patch.ipv6.help 2>/dev/null | sed -e 's@.*/@@' | sort`" ]; then
			for OnePatch in `ls -A1 $Wildcard/*.patch.ipv6.help 2>/dev/null | sed -e 's@.*/@@' | sort` ; do
				PatchName=`echo $OnePatch | sed -e 's/.patch.ipv6.help//'`
				PatchPage=''
				echo -n '[<a href="'$PatchPage'#'$PatchName'">'$PatchName'</a>] ' 
			done
		else
			echo '<i>None</i>'
		fi
		echo
	fi

	echo '<hr>'

	for OneDir in $DirList ; do
		echo '<h2><a name="'$OneDir'">'$OneDir'</a></h2>'
		if [ "$1" = "combined" ]; then
			echo -n '[<a href="#top">Top</a>] '
			for DirLink in $DirList ; do
				if [ "$DirLink" != "$OneDir" ]; then
					echo '[<a href="#'$DirLink'">'$DirLink'</a>] ' 
				else
					echo '['$DirLink'] '
				fi
			done
		fi
		cd $OneDir
		if [ -e "SUITE" ]; then
			Suite=`cat SUITE | sed -e "s/$OneDir//" | grep -v '^\W*$'`
			if [ -n "$Suite" ]; then
				echo '<p>'$OneDir' depends on: '$Suite'</p>'
			fi
		fi
		if [ -f DESCRIPTION ]; then
			echo -n '	<p>'
			cat DESCRIPTION
			echo '</p>'
		fi

		if [ "$1" = "summary" ]; then
			echo '<h3>'$OneDir 'ipv4 patches</h3>'

			if [ -n "`ls -A1 *.patch.help  2>/dev/null | sed -e 's@.*/@@' | sort`" ]; then
				for OnePatch in `ls -A1 *.patch.help  2>/dev/null | sed -e 's@.*/@@' | sort` ; do
					PatchName=`echo $OnePatch | sed -e 's/.patch.help//'`
					PatchPage=${OutputBase##*/}-$OneDir.html
					echo -n '[<a href="'$PatchPage'#'$PatchName'">'$PatchName'</a>] ' 
				done
			else
				echo '<i>None</i>'
			fi
			echo

			echo '<h3>'$OneDir 'ipv6 patches</h3>'

			if [ -n "`ls -A1 *.patch.ipv6.help 2>/dev/null | sed -e 's@.*/@@' | sort`" ]; then
				for OnePatch in `ls -A1 *.patch.ipv6.help 2>/dev/null | sed -e 's@.*/@@' | sort` ; do
					PatchName=`echo $OnePatch | sed -e 's/.patch.ipv6.help//'`
					PatchPage=${OutputBase##*/}-$OneDir.html
					echo -n '[<a href="'$PatchPage'#'$PatchName'">'$PatchName'</a>] ' 
				done
			else
				echo '<i>None</i>'
			fi
			echo
			echo '<hr>'
		else
			echo '<hr>'
			echo '<dl>'
			for OnePatch in `ls *.patch.help *.patch.ipv6.help 2>/dev/null` ; do
				PatchName=`basename $OnePatch | sed -e 's/.patch.help//' -e 's/.patch.ipv6.help//'`
				echo -n '<dt><b><a name="'$PatchName'">'$PatchName'</a></b> '
				for OneExt in patch patch.config.in \
				 patch.configure.help patch.help patch.ipv6 \
				 patch.ipv6.config.in patch.ipv6.configure.help \
				 patch.ipv6.help patch.ipv6.makefile patch.makefile ; do
					if [ -f $PatchName.$OneExt ]; then
						#FIXME urlbase
						echo -n '[<a href="'$BaseURL/$OneDir/$PatchName.$OneExt'">'$PatchName.$OneExt'</a>] '
					fi
				done
				echo
				echo '<dd>'
				echo '<pre>'
				cat $OnePatch | sed \
				 -e 's@\(Author.*\)<\([^\>]*\)>@\1<a href="mailto:\2">\&lt;\2\&gt;</a>@' \
				 -e 's@\(http:[^ ]*\)@<a href="\1">\1</a>@' \
				 -e 's@\(ftp:[^ ]*\)@<a href="\1">\1</a>@'
				echo '</pre>'
				echo '<hr>'
			done
			echo '</dl>'
		fi
		cd ..
	done
}


DoFooter () {
	echo '<hr>'
	echo '<p>Generated '`date`' by <a href="http://www.stearns.org/pomlist/">pomlist</a> version '$POMListVer'.</p>'
	echo '<hr>'

	if [ "$Fullpage" = "yes" ]; then
		cat <<EOFOOTER
</body>
</html>
EOFOOTER
	fi
}



FullParams="$0 $*"

POMDir='./'	#Default if not specified
Extension='body'

while [ -n "$1" ]; do
	case "$1" in
	[Hh][Ee][Ll][Pp])
		usage
		;;
	[Ff][Uu][Ll][Ll][Pp][Aa][Gg][Ee])
		Fullpage="yes"
		Extension='html'
		;;
	[Hh][Tt][Tt][Pp]*|[Ff][Tt][Pp]*|[Ff][Ii][Ll][Ee]*)
		BaseURL="$1"
		;;
#FIXME - a way of specifying OutputBase
#	/*)
#		OutputBase="$1"
#		;;
	*)
		POMDir="$1/"
		;;
	esac
	shift
done

#BaseURL="file:`pwd`/"
[ -n "$BaseURL" ] || BaseURL="http://cvs.samba.org/cgi-bin/cvsweb/~checkout~/netfilter/userspace/patch-o-matic/"

#OutputBase="/path/to/output"
[ -n "$OutputBase" ] || OutputBase="`pwd`/pom"

[ -e "$POMDir/runme" ] || fail 'Not in the patch-o-matic directory, please specify the directory that contains "runme" on the command line.'
cd $POMDir || fail 'Could not change to patch-o-matic directory'

rm -f $OutputBase-combined.$Extension
( DoHeader 'combined' ; DoBody 'combined' ; DoFooter ) >$OutputBase-combined.$Extension

rm -f $OutputBase-summary.$Extension
( DoHeader 'summary' ; DoBody 'summary' ; DoFooter ) >$OutputBase-summary.$Extension


for SuiteDir in * ; do
	if [ -d $SuiteDir ] && [ "$SuiteDir" != "CVS" ]; then
		rm -f $OutputBase-$SuiteDir.$Extension
		( DoHeader "$SuiteDir" ; DoBody "$SuiteDir" ; DoFooter ) >$OutputBase-$SuiteDir.$Extension
	fi
done