#!/bin/sh -
#
#	find_RCS_dirs_and_files_for_web:
#	finds all the RCS directories, and files therein, which would be shown
#	in the "directory listing" web pages for my home directory
#	and its various sub-directories, were it not for the fact
#	that the current DoC server setup is to suppress
#	RCS directories and files from such listings.
#
#	usage: find_RCS_dirs_and_files_for_web [--html]
#
#	Writes to the standard output.
#
#	The RCS directories and files are given as relative pathnames from the
#	"gateway directory". Since they're not necessarily all actually
#	reachable from the gateway directory
#	(some being in public_html/ itself),
#	we have to traverse the directory structure from "..".
#	But (Gordon Bennett!!), to avoid going down the forbidden gateway
#	name "realdotdot" and getting an infinite recursion in the traversal,
#	we have to parse the forbidden gateway name away.
#	Hence the rather complicated traversal arrangements. So now you know!
#
#	Output format *without* --html flag is like this (per directory):
#	  RCS-dir-as-relative-pathname-from-gateway-directory [NEWLINE]
#	  [TAB] file-therein-as-ditto        [NEWLINE]
#	  [TAB] file-therein-as-ditto        [NEWLINE]
#	  [TAB] (etc, for each file therein) [NEWLINE]
#
#	Output format *with* --html flag is like this (per directory, and with
#	the pathnames massaged at least passably into "URL syntax"):
#	  <LI> <A HREF="[RCS-dir-as-relative-pathname-from-gateway-directory]">
#		  [RCS-dir-as-relative-pathname-from-gateway-directory]</A>
#	  [NEWLINE]
#	  [TAB] <UL> [NEWLINE]
#	  [TAB] <LI> <A HREF="[file-therein-as-ditto]">
#			[file-therein-as-ditto]</A>      </LI> [NEWLINE]
#	  [TAB] <LI> <A HREF="[file-therein-as-ditto]">
#			[file-therein-as-ditto]</A>      </LI> [NEWLINE]
#	  [TAB] <LI> <A HREF="(etc, for each file therein)">
#			(etc, for each file therein)</A> </LI> [NEWLINE]
#	  [TAB] </UL> [NEWLINE]
#	  </LI> [NEWLINE]
#
#	It's best to run this as someone else, e.g. a test account,
#	so that only RCS directories and files whose presence is
#	actually visible on the web are reported.
#	The environment variable $USER is used to determine who we're scanning
#	for RCS directories and files of.
#
progname=`basename $0`
gateway_dir="/homes/$USER/public_html/dotdot"
forbidden_gateway_name="realdotdot"

#
#	argument checking
#
html_flag="$*"
if [ "x$html_flag" != "x" -a "x$html_flag" != "x--html" ]; then
	echo "usage: $progname [--html]" >&2
	exit 1
fi

#
#	do we have a gateway directory in the expected location?
#
cd "$gateway_dir"
#
#	Amazingly, on our Linux and FreeBSD machines, the Bourne shell
#	"cd" command doesn't cause exit on failure - it just barges on!
#	Naturally, all hell can break loose in a shell script which is
#	in a different directory from where it thinks it is.
#	So, to avoid such chaos, we check explicitly for failure here.
#
if [ $? -ne 0 ]; then
	echo "$progname: no gateway dir '$gateway_dir', exiting" >&2
	exit 1
fi

#
#	OK, now do a recursive listing, and extract things that look like
#	RCS directories. (yeah, not an ideal method, I know!)
#
#	The "ls -ARFL $potential_dirs_to_traverse" below *will* catch "../RCS"
#	(if it exists) and report it as "../RCS:" in its output,
#	just as we want it to. One might worry that "../RCS" might be the only
#	item in $potential_dirs_to_traverse, causing ls to behave differently
#	(ls doesn't report the name of a directory when the directory is
#	the only argument);
#	but in fact that can't happen, since the gateway directory itself
#	will be an item in $potential_dirs_to_traverse as well.
#	(And a good thing too, since the traversal of the gateway directory
#	is likely to provide the great bulk of the output!)
#
potential_dirs_to_traverse=`ls -A .. | sed -e '/^'$forbidden_gateway_name'$/d' -e 's|^|../|'`
gateway_dir_basename=`basename $gateway_dir`
RCS_dirs=`ls -ARFL $potential_dirs_to_traverse | egrep '^RCS:$|^.*/RCS:$' | sed -e 's|:$|/|' -e 's|^../'$gateway_dir_basename'/||'`

#
#	Now produce appropriate output from this list of RCS directories.
#
if [ "x$html_flag" = "x" ]; then		#plain output

	for RCS_dir in $RCS_dirs ; do
		files_therein=`ls -A $RCS_dir`

		echo "$RCS_dir"
		for file_therein in $files_therein ; do
			echo "	$RCS_dir$file_therein"
		done #for file_therein
	done #for RCS_dir

elif [ "x$html_flag" = "x--html" ]; then		#fancy html output

	for RCS_dir in $RCS_dirs ; do
		files_therein=`ls -A $RCS_dir`

		RCS_dir_as_URL=`echo $RCS_dir | sed -e 's/%/%25/g' -e 's/+/%2B/g' -e 's/ /%20/g' -e 's/"/%22/g'`
		echo "<LI> <A HREF=\"$RCS_dir_as_URL\">$RCS_dir_as_URL</A>"
		echo "	<UL>"
		for file_therein in $files_therein ; do
			file_therein_as_URL=`echo $file_therein | sed -e 's/%/%25/g' -e 's/+/%2B/g' -e 's/ /%20/g' -e 's/"/%22/g'`
			echo "	<LI> <A HREF=\"$RCS_dir_as_URL$file_therein_as_URL\">$RCS_dir_as_URL$file_therein_as_URL</A> </LI>"
		done #for file_therein
		echo "	</UL>"
		echo "</LI>"
	done #for RCS_dir

else					#something's gone awfully wrong!

	echo "$progname: fatal error with flow of control, exiting" >&2
	exit 1

fi
