#! /bin/sh
#
# This shell script takes care of building diff files for individual tools
# in the GNU source tree, using the baseline archives for each tool.
# It creates a temporary work directory in the location specified by
# the first argument, dearchives the baseline archive in that directory,
# and then builds a diff file in that directory using the baseline source
# and the current amiga source.  If everything goes well, then the diff
# file is moved to the correct location in the GNU source tree.

WORKDIR=$1/GNUdiffs

if test ! -d $WORKDIR
then
	echo "Making work directory $WORKDIR"
	mkdir -p $WORKDIR
	if test ! -d $WORKDIR
	then
		exit 1
	fi
fi

for name in /gnu/src/amiga/*
do
	if test -d $name
	then
		tool=`basename $name`
		archive=""
		if test ! -f $name/Product-Info
		then
			echo "WARNING: $name has no Product-Info file."
		fi
		if test -f /gnu/src/baseline/$tool.lha
		then
			archive="gnu:src/baseline/$tool.lha"
		elif test -f /gnu/src/baseline/$tool.tar.gz
		then
			archive="/gnu/src/baseline/$tool.tar.gz"
		fi
		if test ! -n "$archive"
		then
			echo "WARNING: no baseline archive found for $tool, skipped."
		else
			if test ! -d $WORKDIR
			then
				echo "mkdir -p $WORKDIR"
				mkdir -p $WORKDIR
			fi
			echo "=========="
			echo "(cd $WORKDIR; rm -rf $tool)"
			(cd $WORKDIR; rm -rf $tool)
			case "$archive" in
				*.lha) echo "(cd $WORKDIR; lha -mraxeq x $archive)"
				       (cd $WORKDIR; lha -mraxeq x $archive);;
				*.gz) echo "(cd $WORKDIR; gzip -d <$archive | tar -xf -)"
				      (cd $WORKDIR; gzip -d <$archive | tar -xf -);;
			esac
			echo "(cd $WORKDIR; diff -rc --new-file $tool $name >>$tool.diffs)"
			(cd $WORKDIR; diff -rc --new-file $tool $name >>$tool.diffs)
			echo "(cd $WORKDIR; rm -rf $tool)"
			(cd $WORKDIR; rm -rf $tool)
			echo "cp $WORKDIR/$tool.diffs /gnu/src/diffs/$tool.diffs"
			cp $WORKDIR/$tool.diffs /gnu/src/diffs/$tool.diffs
		fi
	fi
done
