#!/bin/sh
#
# $0 <which>
#

# Check parameters
if [ "x$1" = "x" -o "x$2" = "x" ]; then
    echo "Usage: $0 <which> <archive>"
    echo "For example: $0 src AROS-1.10"
    exit 1
fi

# Find the name of the directory I'm in
cdn=`pwd | sed 's:/.*/::'`

# Basename of the archive
archive="$cdn/dist/$2"

# Move up one level
cd ..

# Find the names of the files which contain the names of the files
# to distribute
distfiles=`find $cdn -name dist.$1 -print`

# Name of the file with the resulting list of all files to distribute
filenames=/tmp/fn.$$

# Empty list
rm -f $filenames
find $cdn -name contents -print > $filenames

# For each file with a list of files...
for dist in $distfiles ; do
    # Get the base directory
    dir=`dirname $dist`

    # Show where we are
    echo "$dir"

    # Now read the list of files one by one
    cat $dist | while read line ; do
	# Replace patterns
	if [ "x$line" != "x" ]; then
	    ls $dir/$line >> $filenames
	    if [ "x$3" != "x" ]; then
		ls $dir/$line
	    fi
	fi
    done
done

# Sort the names
sort $filenames | uniq > $filenames.srt

if [ "x$3" = "x" ]; then
    # Create compressed tar archive
    rm -f $archive.tgz
    tar -cvvz --files-from=$filenames.srt -f "$archive.tgz"

    # Create LhA archive
    rm -f $archive.lha
    lha a "$archive.lha" `cat $filenames.srt`

    # Remove list of filenames
    rm -f $filenames $filenames.srt
fi
