#! /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.

toolsrcdir=src/amiga
manifests=manifests
bbsroot=$1
bbsgnudir=GNU
bbsdir=$bbsroot/$bbsgnudir
WORKDIR=$2/GNUpack

# 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 -cp -T $manifests/$tool -f $WORKDIR/$bbsgnudir/tar.out"
	tar -cp -T $manifests/$tool -f $WORKDIR/$bbsgnudir/tar.out
	echo "(cd $WORKDIR/$bbsgnudir && tar -xpf tar.out && rm tar.out)"
	(cd $WORKDIR/$bbsgnudir && tar -xpf 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"
	elif test -f $toolsrcdir/$srcname-src.lha.pi
	then
		pifilein="$toolsrcdir/$srcname-src.lha.pi"
	fi
	if test -z "$pifilein"
	then
		echo "WARNING: no product info file found to generate $pifileout"
	else
		echo "cp $pifilein $pifileout"
		cp $pifilein $pifileout
	fi
done
