#! /bin/sh
#
# Create archives for the BBS section of this CD from various parts of the
# GNU tree.  To help guide this process, the files in the manifests subdir
# list the contents of each archive.  The associated product info file is
# found either by looking for an appropriate *-pi file in the manifests
# subdir, or from the Product-Info in the appropriate source subdir.

workdir=/Scratch/packtemp
toolsrcdir=src/amiga
manifests=manifests
bbsgnudir=GNU
bbsdir=/BBS/$bbsgnudir

# Check to see that the output BBS directory exists.  If not, create it.

if test ! -d $bbsdir
then
	echo "mkdir -p $bbsdir"
	mkdir -p $bbsdir
fi
if test ! -d $workdir
then
	echo "mkdir -p $workdir"
	mkdir -p $workdir
fi

for tool in `(cd $manifests; echo *-src *-bin *-diffs)`
do
	echo "========="

	# Remove any old trash and create a fresh working directory.

	if test -d $workdir/$bbsgnudir
	then
		echo "rm -rf $workdir/$bbsgnudir"
		rm -rf $workdir/$bbsgnudir
	fi
	echo "mkdir -p $workdir/$bbsgnudir"
	mkdir -p $workdir/$bbsgnudir

	# Make a copy of all files that we want in the archive under
	# $workdir/$bbsgnudir.  The files listed in the manifests files
	# are relative to the current working directory (GNU).

	echo "tar -c -T $manifests/$tool -f $workdir/$bbsgnudir/tar.out"
	tar -c -T $manifests/$tool -f $workdir/$bbsgnudir/tar.out
	echo "(cd $workdir/$bbsgnudir && tar -xf tar.out && rm tar.out)"
	(cd $workdir/$bbsgnudir && tar -xf tar.out && rm tar.out)

	# Now make an lha archive in $workdir that is rooted in $bbsgnudir,
	# and if successful, copy it to the BBS/$bbsgnudir directory.

	echo "(cd $workdir && lha -mraxeq a $tool.lha $bbsgnudir && cp $tool.lha $bbsdir/$tool.lha)"
	(cd $workdir && lha -mraxeq a $tool.lha $bbsgnudir && cp $tool.lha $bbsdir/$tool.lha)

	# Locate an appropriate product info file and install a copy of the product
	# info file alongside the archive.

	pifilein=""
	pifileout="$bbsdir/$tool.lha.pi"
	srcname=`echo $tool | sed -e "s:-src::" -e "s:-bin::" -e "s:-diffs::"`
	if test -f $manifests/$tool-pi
	then
		pifilein="$manifests/$tool-pi"
	elif test -f $toolsrcdir/$srcname/Product-Info
	then
		pifilein="$toolsrcdir/$srcname/Product-Info"
	fi
	if test -n "$pifilein"
	then
		echo "cp $pifilein $pifileout"
		cp $pifilein $pifileout
	else
		echo "WARNING: no product info file found to generate $pifileout"
	fi
done
