#!/bin/sh
# $Source$
# $State$

# Set up default variables.

destdir=
srcdir=`pwd`
arch=/usr/local/bin/arch
delete=no
copy="ln"

# --- Options parsing -------------------------------------------------------

while [ "$1" != "" ]; do
	case "$1" in
		-s|--srcdir)
			srcdir="$2"
			shift
			;;
			
		-d|--destdir)
			destdir="$2"
			shift
			;;
			
		-x|--delete)
			delete=yes
			;;
			
		-c|--copy)
			copy="cp -Rp"
			;;
			
		-S|--symlink)
			copy="ln -s"
			;;
			
		-a|--arch)
			arch="$2"
			shift
			;;
			
		-h|--help)
			echo "mkdist [options]"
			echo "Options are:"
			echo "  -s --srcdir <path>   The CVS tree to read from. (default: CWD)"
			echo "  -d --destdir <path>  The directory to create the distribution in."
			echo "  -x --delete          Erase the destination directory first."
			echo "  -c --copy            Make physical copies of the files. (default: hardlink)"
			echo "  -S --symlink         Make symbolic links instead of copying or hardlinking."
			echo "  -a --arch <path>     Where the ACK 'arch' tool is."
			echo "  -h --help            Display this message."
			exit 0
			;;
			
		*)
			echo "Unrecognised option. Try --help for help."
			exit 1
	esac
	shift
done

if [ "$destdir" = "" ]; then
	echo "You must specify a destination directory. (Try --help for help.)"
	exit 1
fi

# --- Main routines ---------------------------------------------------------

# These two routines do the work of traversing the source tree and building
# the distribution tree.

addfile() {
	local f
	f="${1##$srcdir/}"
	mkdir -p $destdir/`dirname $f`
	$copy "$1" "$destdir/$f"
}

process_dir() {
	local path
	local archivename
	
	path=$1
	cd $path
	echo $PWD
	
	# Look for a LIST file and cache the first line.
	
	archivename=
	if [ -f LIST ]; then
		archivename=`head -1 LIST`
	fi

	for i in `cat $path/.distr`; do
		case "$i" in
			\#*)	# Comment. Do nothing.
					;;
					
			*)
					if [ -d $i ]; then
						# This is a directory. Recurse into it.
						
						( process_dir $path/$i )
					elif [ -f $i ]; then
						# This is a file.
						
						addfile $path/$i
					elif [ "$i" = "$archivename" ]; then
						# Build the named archive.
			
						$arch cDr `cat LIST`
						addfile $path/$archivename
					else
						echo "Don't know what to do with $i, listed in $PWD/.distr."
						exit 1
					fi
					;;
		esac
	done
}

# --- Main program ----------------------------------------------------------

# Test to make sure that $arch points to the right thing.

if !(strings $arch | grep archiver > /dev/null); then
	echo "$arch does not seem to point at the ACK archiver tool."
	echo "(Don't confuse this with the Linux tool for displaying your"
	echo "architecture.)"
	echo ""
	echo "Press RETURN to go ahead anyway, or CTRL+C to abort."
	read ignored
fi

# Actually do the work.

echo "Creating distribution from CVS tree: $srcdir"
echo "              into destination tree: $destdir"
echo ""

if [ -e $destdir ]; then
	if [ "$delete" = "yes" ]; then
		echo "Press RETURN to erase $destdir and its contents, or CTRL+C to abort."
		read
		echo "Erasing..."
		rm -rf "$destdir"
	else
		echo "$destdir exists. Aborting."
		exit 1
	fi
fi

echo "Working..."
mkdir -p $destdir
process_dir $srcdir
echo "Done."

# Revision history
# $Log$
# Revision 1.5  2007-04-24 19:48:41  dtrg
# Removed bashish.
#
# Revision 1.4  2007/02/25 20:56:41  dtrg
# Performed major renovations to make the script work on OpenBSD.
#
# Revision 1.3  2007/02/24 02:05:56  dtrg
# Removed some bashish; added comment support; removed the make
# distr functionality, as nothing was using it any more and it was
# causing problems.
#
# Revision 1.2  2005/06/24 23:19:23  dtrg
# Added new mkdist tool.
#
# Revision 1.1  2005/06/24 22:13:57  dtrg
# Created new tool to generate distributions.