#!/bin/sh
#
# "$Id: makesrcdist 11158 2013-07-17 18:31:56Z msweet $"
#
# makesrcdist - make a source distribution of CUPS.
#

# Make sure we are running in the right directory...
if test ! -f tools/makesrcdist; then
        echo "Run this script from the top-level CUPS source directory, e.g.:"
        echo ""
        echo "    tools/makesrcdist $*"
        echo ""
        exit 1
fi

if (svn st | grep -v makesrcdist | grep -qv '^\?'); then
        echo Local changes remain:
        svn st | grep -v makesrcdist | grep -v '^\?'
        exit 1
fi

if test $# = 0; then
	echo Updating for snapshot...
	svn up

	# Compute version for snapshot
	rev=`svn info . | grep Revision: | awk '{print $2}'`
	version="1.7svn"
	revision="-r$rev"
	fileversion="1.7svn-r$rev"
	fileurl="http://www.cups.org/software/test/cups-$fileversion-source.tar."
	url="."
else
	# Use version from command-line
	rev="1"
	version=$1
	revision=""
	fileversion=$1
	fileurl="http://www.cups.org/software/$version/cups-$fileversion-source.tar."
	url="svn+ssh://src.apple.com/svn/cups/cups.org/tags/release-$version"

	echo Validating sources...
	cupsversionpatch=`echo $version | awk -F. '{if (NF == 3) { print $3 } else { print "0" } }'`
	cupsversion=`printf "1.07%02d" $cupsversionpatch`

	temp=`grep CUPS_VERSION cups/cups.h | grep -v CUPS_VERSION_ | awk '{print $4}'`
	if test "$temp" != $cupsversion; then
		echo "Still need to update CUPS_VERSION to $cupsversion in cups/cups.h (saw $temp)"
		exit 1
	fi

	temp=`grep CUPS_VERSION_PATCH cups/cups.h | awk '{print $4}'`
	if test "$temp" != $cupsversionpatch; then
		echo "Still need to update CUPS_VERSION_PATCH to $cupsversionpatch in cups/cups.h (saw $temp)"
		exit 1
	fi

	temp=`head -1 README.txt | awk '{print $4}'`
	if test "$temp" != "v$version"; then
		echo "Still need to update version to v$version in README.txt (saw $temp)"
		exit 1
	fi

	temp=`head -1 INSTALL.txt | awk '{print $4}'`
	if test "$temp" != "v$version"; then
		echo "Still need to update version to v$version in INSTALL.txt (saw $temp)"
		exit 1
	fi

	temp=`head -4 CHANGES.txt | grep "CHANGES IN" | awk '{print $4}'`
	if test "$temp" != "V$version"; then
		echo "Still need to add CHANGES IN V$version in CHANGES.txt (saw $temp)"
		exit 1
	fi

	echo Creating tag for release...
	svn copy svn+ssh://src.apple.com/svn/cups/cups.org/trunk "$url" \
		-m "Tag $version" || exit 1
fi

fileurl=`echo $fileurl | sed -e '1,$s/\\//\\\\\\//g'`

echo Exporting $fileversion...
rm -rf /tmp/cups-$version
svn export $url /tmp/cups-$version

echo Updating version information...
cd /tmp/cups-$version/config-scripts

sed -e '1,$s/^CUPS_VERSION=.*/CUPS_VERSION='$version'/' \
	-e '1,$s/^CUPS_REVISION=.*/CUPS_REVISION='$revision'/' \
	<cups-common.m4 >cups-common.m4.new
mv cups-common.m4.new cups-common.m4
cd ..

echo Configuring...
autoconf -f
rm -rf autom4te*.cache
rm -rf tools
cd ..

echo -n Archiving...gz
sed -e '1,$s/@CUPS_VERSION@/'$version'/' \
	-e '1,$s/^Release:.*/Release: '$rev'/' \
	-e '1,$s/^Source:.*/Source: '$fileurl'gz/' \
	<cups-$version/packaging/cups.spec.in \
	>cups-$version/packaging/cups.spec
tar czf cups-$fileversion-source.tar.gz cups-$version

echo -n ...bz2
sed -e '1,$s/@CUPS_VERSION@/'$version'/' \
	-e '1,$s/^Release:.*/Release: '$rev'/' \
	-e '1,$s/^Source:.*/Source: '$fileurl'bz2/' \
	<cups-$version/packaging/cups.spec.in \
	>cups-$version/packaging/cups.spec
tar cjf cups-$fileversion-source.tar.bz2 cups-$version
echo "..."

if test -x /usr/bin/md5sum; then
	(cd /tmp; md5sum cups-$fileversion-source.tar.* | awk '{print $1, "'$fileversion' '$fileversion'/" $2}')
elif test -x /sbin/md5; then
	(cd /tmp; md5 cups-$fileversion-source.tar.* | awk '{print $4, "'$fileversion' '$fileversion'/" substr($2, 2, length($2) - 2)}')
fi

echo Removing temporary files...
rm -rf cups-$version

echo "Done."

#
# End of "$Id: makesrcdist 11158 2013-07-17 18:31:56Z msweet $".
#
