#!/bin/sh
#
# prcs-javadoc (prcs project list) /destination/path
#
# This program generates javadoc documentation for the latest files in
# a set of PRCS projects. It's a bit rough around the edges, but
# hopefully will prove of use to some people.
#
# (c) 1999
# Rafael Kitover (rkitover@io.com)
# Version 0.2: November 5th, 1999
#
# This program may be distributed under the same terms as PRCS itself.
#

set -e

## configuration

# variables you can set
#
# PRCS_REPOSITORY -- where projects are checked out, see prcs(1)
# JAVA		-- path to java executable
# JAVA_ARGS	-- arguments for java (heap size, etc)
# JAVA_COMPILER -- same as for java, the JIT to use
# JAVADOC	-- class name for javadoc, usually sun.tools.javadoc.Main
# JAVADOC_ARGS	-- arguments to pass to javadoc (see javadoc with no args)
# JAVADOC_IMAGES-- directory where images that are referenced by javadoc
#		   generated html are stored.

# path to your java executable
if [ -z "$JAVA" ]; then
	if [ -n "`which java`" ]; then
		JAVA="java"
	else
		JAVA="/usr/local/java/bin/java"
	fi
fi

# args for the java compiler
# you want a big heap because javadoc uses a tonne of memory
# I use 64megs and the JIT compiler "tya".
if [ -z "$JAVA_ARGS" ]; then JAVA_ARGS="-mx64m -ms64m"; fi

if [ -z "$JAVA_COMPILER" ]; then
	if [ -n "`$JAVA -version 2>&1 | head -1 | grep '1.1.'`" ]; then
		export JAVA_COMPILER=tya	# just guessing here, doesn't matter
	elif [ -n "`$JAVA -version 2>&1 | head -1 | grep '1.2.'`" ]; then
		export JAVA_COMPILER=sunwjit
	fi
else
	export JAVA_COMPILER	# make sure it's available to java
fi

# name of the javadoc package
if [ -z "$JAVADOC" ]; then JAVADOC=sun.tools.javadoc.Main; fi

# javadoc args
# -private will generate private methods and variables too
if [ -z "$JAVADOC_ARGS" ]; then JAVADOC_ARGS="-private"; fi

# directory where javadoc images can be found
if [ -z "$JAVADOC_IMAGES" ]; then JAVADOC_IMAGES=`dirname \`locate method-index.gif | head -1\``; fi

## end configuration

####### functions #######

Die ()
{
	echo $*
	Cleanup
	Usage
}

Usage ()
{
	cat <<- USAGE
		Usage:

		$0: (list of prcs projects) /destination/path
	USAGE

	exit 1
}

Cleanup ()
{
	PROGNAME=`basename ${0}`
	rm -rf /tmp/$PROGNAME.$$
}

#### code ####

# parse params, get $PROJECTS and $DESTDIR

if [ $# -lt 2 ]
then
	Usage
fi

PROJECTS=""

while [ $# -ne 1 ]
do
	PROJECTS="$PROJECTS $1"
	shift
done

DESTDIR=$1

# make working dir, cd to it
PROGNAME=`basename ${0}`
mkdir -p /tmp/$PROGNAME.$$
cd /tmp/$PROGNAME.$$

# checkout projects
mkdir -p $PROJECTS
for i in $PROJECTS
do
	cd $i
	prcs checkout $i || Die "Invalid project."
	cd ..
done

# make sure destination directory exists, or can be created
if [ ! -d $DESTDIR ]
then
	mkdir -p $DESTDIR || Die "Could not create directory: $DESTDIR"
fi

# delete everything there

rm -rf $DESTDIR/* || Die "Could not delete files in $DESTDIR"

# set classpath

for i in `find $PROJECTS -name '*.java' -printf '%h\n' | sort -u`
do
	CLASSPATH=$CLASSPATH:/tmp/$PROGNAME.$$/$i
done

# add current dir as well

CLASSPATH=$CLASSPATH:.

# create a list of packages

PACKAGES=""
for i in `find $PROJECTS -name '*.java' -exec egrep '^( |\t)*package( |\t)+(\.|\w)+( |\t)*;( |\t)*$' {} \; |
		sed -e 's/^ \+//' |
		sort -u |
		sed -e 's/^package\( \|\t\)\+//' -e 's/;//'`
do
	PACKAGES="$PACKAGES $i"
done 


# run javadoc, if is because of the set -e

if
	$JAVA $JAVA_ARGS $JAVADOC $JAVADOC_ARGS -d $DESTDIR $PACKAGES
then
	echo -n	# do nothing
fi

# put in a link to images

ln -s $JAVADOC_IMAGES $DESTDIR/images

# make tree.html the default starting point

ln -s $DESTDIR/tree.html $DESTDIR/index.html

Cleanup

exit 0
