#!/usr/bin/env bash
# Make a mega-tarball for a given release.
# Must be run from the Release directorty.
if [ $# -ne 1 ]
then	echo >&2 "usage: $0 release-num"
	exit 1
fi
if [ ${PWD##*/} != Release ]
then	echo >&2 "must be run from the Release directory"
	exit 1
fi
set -e
#set -x
if [[ "$1" = *p? ]]
then	typeset -i patch=${1##*p}
	echo "Is patch $patch."
	rev=${1%p*}
else	echo "Is a base release."
	typeset -i patch=0
	rev=$1
fi
td=/tmp/mt$$
rm -rf $td
mkdir $td
cd $td
for i in x3270 c3270 wc3270 s3270 ws3270 tcl3270 pr3287 wpr3287
do	typeset -i pp=$patch
	while (( pp >= 0 ))
	do	if [ $pp -eq 0 ]
		then	f=~-/$i-$rev-src.tgz
		else	f=~-/$i-${rev}p$pp-src.tgz
		fi
		[ -f $f ] && break
		if [ $pp -eq 0 ]
		then	echo >&2 "Can't find a matching rev of $i"
			exit 1
		fi
		pp=pp-1
	done

	echo "Using $f"
	tar -xzf $f
done
tar -czf ~-/suite3270-$1-src.tgz *
chmod -w ~-/suite3270-$1-src.tgz
cd -
rm -rf $td
