#! /bin/sh
#============================================================================
# This shell script takes care of building diff files for individual products
# in the ADE tree, using the baseline archives for each product.
#
# Takes the following arguments:
#
#	--to=<dir>	The directory in which to create the diff archives,
#			typically something like "ADE-dist:951208".
#
#	--from=<dir>	The directory which contains the baseline and amiga
#			source archives.  Typically the same as --to.
#
#	-tmp=<dir>	A temporary directory in which to create temporary
#			subdirs used in creating the diffs.
#
# It creates a temporary work directory in the location specified by
# the --tmp argument, dearchives the baseline archive in that directory,
# dearchives the amiga source archive in that directory, builds a diff file
# in that directory, and then archives it in a compressed tar archive.
# If everything goes well, the diff archive is moved to the correct location
# as specified by the --to argument.
#
# FIXME:  We used to use a pipe below to feed the gzip output into tar
# however while finalizing FreshFish 10 this broke on dejagnu-1.2.tar.gz
# for some unknown reason.  Doing it in two steps with an intermediate
# temporary file works fine.  This should be investigated further. -fnf

to=
from=
tmp=
for option
do
	# If the previous option needs an argument, assign it.
	if test -n "$prev"; then
		eval "$prev=\$option"
		prev=
		continue
	fi

	case "$option" in
		-*=*) optarg=`/bin/echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
		*) optarg= ;;
	esac

	case "$option" in
		--to)		prev=to ;;
		--to=*)		to="$optarg" ;;
		--from)		prev=from ;;
		--from=*)	from="$optarg" ;;
		--tmp)		prev=tmp ;;
		--tmp=*)	tmp="$optarg" ;;
		-*) { echo "error: $option: invalid option" 1>&2; exit 1; } ;;
	esac
done

# Verify that we have all the options we expect to have.

if test -z "$to"; then
	echo "error: no --to=<dir> argument given" 1>&2
	exit 1
fi

if test -z "$from"; then
	echo "error: no --from=<dir> argument given" 1>&2
	exit 1
fi

if test -z "$tmp"; then
	echo "error: no --tmp=<dir> argument given" 1>&2
	exit 1
fi

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

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

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

WORKDIR="$tmp/ade-tmp"
if test ! -d "$WORKDIR"
then
	echo "mkdir -p $WORKDIR"
	mkdir -p $WORKDIR
fi

for srcarchive in $from/*-src.tgz
do
	tool=`basename $srcarchive .tgz | sed "s/-src$//"`
	if test -f $to/$tool-diffs.gz
	then
		continue
	fi
	basearchive="$from/$tool-base.tgz"
	if test ! -f $srcarchive.pi
	then
		echo "WARNING: $srcarchive has no Product-Info file."
	fi
	if test ! -f "$basearchive"
	then
		echo "WARNING: no baseline archive found for $tool, skipped."
	else
		echo "==== $srcarchive ====="
		# Remove any old baseline and amiga sources.
		echo "rm -rf $WORKDIR/baseline $WORKDIR/amiga $WORKDIR/*-diffs.gz"
		rm -rf $WORKDIR/baseline $WORKDIR/amiga $WORKDIR/*-diffs.gz
		echo "mkdir $WORKDIR/baseline $WORKDIR/amiga"
		mkdir $WORKDIR/baseline $WORKDIR/amiga
		# Extract the baseline and amiga sources
		echo "(cd $WORKDIR/baseline && gzip -d <$basearchive | tar -xpf -)"
		(cd $WORKDIR/baseline && gzip -d <$basearchive | tar -xpf -)
		echo "(cd $WORKDIR/amiga && gzip -d <$srcarchive | tar -xpf -)"
		(cd $WORKDIR/amiga && gzip -d <$srcarchive | tar -xpf -)
		# Create the diff file
		out=$WORKDIR/$tool-diffs
		echo >>$out "This file contains patches that transform the baseline version into" 
		echo >>$out "the amiga version.  Assuming that you have unarchived the baseline"
		echo >>$out "version in the current directory, just run the command:"
		echo >>$out ""
		echo >>$out "	patch -p1 -E -b .pbak <diff-file"
		echo >>$out ""
		echo >>$out "where 'diff-file' is this patch file.  After running patch you should"
		echo >>$out "remove all the generated *.pbak files, and look for any *.rej files"
		echo >>$out "that indicate a problem patching the baseline source."
		echo >>$out ""
		echo "(cd $WORKDIR && diff -rup --new-file baseline amiga) >>$out"
		(cd $WORKDIR && diff -rup --new-file baseline amiga) >>$out
		# Compress the diffs file and copy it to the destination.
		echo "(cd $WORKDIR && gzip -9 $tool-diffs && cp $tool-diffs.gz $to/$tool-diffs.gz)"
		(cd $WORKDIR && gzip -9 $tool-diffs && cp $tool-diffs.gz $to/$tool-diffs.gz)
		# Add the product info file.
		echo "cp $srcarchive.pi $to/$tool-diffs.gz.pi"
		cp $srcarchive.pi $to/$tool-diffs.gz.pi
	fi
done
