#! /bin/sh
###=====================================================================
### Convert one or more .tar.gz files in the current directory to .bz2, 
### .jar, .zip, and .zoo format, if they do not already exist in those
### formats.  If any of these extra archives are created, then unpack
### any <programname>.html file and copy it to the current directory as
### well.  If no .html file is found, try to convert one or more manual
### page files to HTML, and copy them.  Created HTML files are always
### renamed <programname>.html, so that if programname contains a
### version number, the HTML file will as well.
###
### Usage:
###	./convert-tar-gz-to-others.sh *.tar.gz
###
### The directory into which each .tar.gz file unpacks must match its
### basename.  If not, an error message will be issued.
###
### Care should be taken to manually repair any confusion from multiple
### manual page files in the unpacked directory tree; the last one
### converted will be the one named <programname>.html, but that might
### be incorrect.
###
### [23-Dec-2002] -- add support for creation of .tar.bz2 files
### [18-Jan-2000]
###=====================================================================

### Make sure that we get GNU versions of utilities (e.g., for tar cfj)
PATH=/usr/local/bin:$PATH
export PATH

TMPDIR=/tmp/convert-tar-gz-to-others.$$
TOPDIR=`pwd`
### [11-Oct-2001] Use environment variable to avoid getting long
### NFS machine-specific mount paths.
TOPDIR=$PWD

trap '/bin/rm -r -f ${TMPDIR}' 0 1 2 3 15

errors=0

for f in "$@"
do
	d=`dirname $f`
	if test $d = "."
	then
		d=$TOPDIR
	fi
	g=`basename $f .tar.gz`
	if test -f $g.tar.gz -a \( ! -f $g.tar.gz2 -o ! -f $g.jar -o ! -f $g.zip -o ! -f $g.zoo \)
	then
		echo ""
		echo Packaging $g
		mkdir $TMPDIR
		cd $TMPDIR
		tar xfz $TOPDIR/$g.tar.gz
		if test -d $g
		then
			################################################
			## Try to find, or create dynamically, HTML
			## files for the program.
			################################################

			htmlfiles=` find $g/*.html  2>/dev/null | sort `
			manfiles=`  find $g/*.man   2>/dev/null | sort `
			numfiles=`  find $g/*.[0-9nl] 2>/dev/null | sort `
			program=`   echo $g | sed -e 's/[-_0-9.]*$//g' `
			##?? echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
			##?? echo DEBUG: g=$g htmlfiles=$htmlfiles manfiles=$manfiles numfiles=$numfiles program=$program
			##?? echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
			if test -f $g/$program.html
			then
				cp $g/$program.html $TOPDIR/$g.html && ls -l $TOPDIR/$g.html
			elif test -n "$htmlfiles"
			then
				echo "WARNING: Found, but did not copy, these HTML files: $htmlfiles"
			elif test -n "$manfiles"
			then
				for m in $manfiles
				do
					man2html $m >/dev/null	# NB: this creates .html file in CURRENT directory
					h=`basename $m .man`.html
					h=`basename $h`
					##?? echo DEBUG 3: h=$h htmlfiles=`ls *.html`  m=$m
					test -s $h && cp $h $TOPDIR/$g.html && echo New HTML from $m: && ls -l $TOPDIR/$g.html
				done
			elif test -n "$numfiles"
			then
				for m in $numfiles
				do
					man2html $m >/dev/null # NB: this creates .html file in CURRENT directory
					h=`echo $m | sed -e 's/[.][0-9nl]$//'`.html
					##?? echo DEBUG 4.0: h=$h htmlfiles=`ls *.html` m=$m
					h=`basename $h`
					##?? echo DEBUG 4: h=$h htmlfiles=`ls *.html` m=$m
					test -s $h && cp $h $TOPDIR/$g.html && echo New HTML from $m: && ls -l $TOPDIR/$g.html
				done
			else
				echo WARNING: $g: No HTML files available, sigh...
			fi

			################################################
			## Java archive
			################################################
			if test ! -f $TOPDIR/$g.jar
			then
				jar cf $TOPDIR/$g.jar $g 2>/dev/null
				jar tvf $TOPDIR/$g.jar >$TOPDIR/$g.jar-lst 2>&1
				ls -l $TOPDIR/$g.jar*
			fi

			################################################
			## bzip2 archive
			################################################
			if test ! -f $TOPDIR/$g.bz2
			then
				BZIP2=-9 GZIP=-9 tar cfj $TOPDIR/$g.tar.bz2 $g 2>/dev/null
				ls -l $TOPDIR/$g.tar.bz2
			fi

			################################################
			## InfoZip archive
			################################################
			if test ! -f $TOPDIR/$g.zip
			then
				cd $g
				zip $TOPDIR/$g.zip `find . -type f | sort` >/dev/null
				unzip -v $TOPDIR/$g.zip >$TOPDIR/$g.zip-lst
				ls -l $TOPDIR/$g.zip*
				cd ..
			fi

			################################################
			## Zoo archive
			################################################
			if test ! -f $TOPDIR/$g.zoo
			then
				cd $g
				zoo a $TOPDIR/$g.zoo `find . -type f | sort` >/dev/null
				zoo v $TOPDIR/$g.zoo >$TOPDIR/$g.zoo-lst
				ls -l $TOPDIR/$g.zoo*
				cd ..
			fi

			################################################
			## Final check for HTML
			################################################
			if test ! -f $TOPDIR/$g.html
			then
				echo MISSING: $TOPDIR/$g.html
			fi
		else
			echo "ERROR: $g.tar.gz erroneously unpacked into unexpected directory `ls`: skipping"
			errors=1
		fi
		cd $TOPDIR
		rm -rf $TMPDIR
	else
		echo Skipping $g: all archive formats already exist
	fi
done

exit $errors
