#!/bin/sh
#
# makesrcdist - make a distribution of FLTK.
#
# There are 3 different modes of operation, dependent on commandline arguments:
#
#  (1)	Create snapshot:
#
#	makesrcdist [snapshot]
#
#	Use no arguments or "snapshot" (verbatim).
#
#  (2)	Create distribution tarballs for test and verification:
#
#	makesrcdist <version>
#
#	Use a version number as argument, e.g. "1.3.3" or "1.3.4rc2".
#	This can be used for local testing.
#
#  (3)	Create distribution tarballs (final):
#
#	makesrcdist <version> tag
#
#	Same as (2), but create subversion tag with version number.
#	Enter "tag" (verbatim) as 2nd argument.
#	This will create the subversion tag "release-<version>" for the
#	current revision in the FLTK subversion repository and export the
#	FLTK sources from this tag for creation of distribution files.
#
# Note: define FLTK_TAR if you want to use a different compatible tar
#	command than "tar", e.g. to use "gtar" (bash syntax):
#	$ export FLTK_TAR="gtar"
#

TAR="tar"
if test "x$FLTK_TAR" != "x"; then
  TAR="$FLTK_TAR"
fi

# these are the subversion and snapshot/download URL's currently in use:

SVN='http://seriss.com/public/fltk/fltk'
DOWNLOAD='http://fltk.org/pub/fltk'
SNAPSHOT='http://fltk.org/pub/fltk/snapshots'
VS=`cat VERSION | sed -e's/\([0-9]*\.[0-9]*\).*/\1/'`

echo "Getting distribution..."

if test $# = 0 -o "x$1" = "xsnapshot"; then
	echo Updating for snapshot...
	svn up
	rev=`svnversion . | sed -e '1,$s/[a-zA-Z]//g'`
	version="${VS}svn"
	fileversion="${VS}.x-r$rev"
	fileurl="$SNAPSHOT/fltk-$fileversion.tar.gz"
	echo "fileversion = $fileversion"
	echo "fileurl     = $fileurl"
	url="."
else
	if test ! -e "documentation/html/"; then
		echo "ERROR: Please generate the HTML documentation before distributing:"
		echo "  autoconf"
		echo "  ./configure"
		echo "  cd documentation; make dist"
		exit
	fi
	if test ! -e "documentation/fltk.pdf"; then
		echo "ERROR: Please generate the PDF documentation before distributing:"
		echo "  autoconf"
		echo "  ./configure"
		echo "  cd documentation; make dist"
		exit
	fi
	rev="1"
	version=$1
	fileversion=$1
	fileurl="$DOWNLOAD/$version/fltk-$fileversion-source.tar.gz"

	if test "x$2" != "xtag"; then
		url="."
	else
		echo "Creating subversion tag 'release-$version' ..."
		url="$SVN/tags/release-$version"
		svn copy $SVN/branches/branch-${VS} "$url" \
		-m "Tag $version" || exit 1
	fi
fi

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

if test $# != 0 -a "x$1" != "xsnapshot"; then
	echo "Copying HTML and PDF documentation..."
	cp -r documentation/html /tmp/fltk-$version/documentation/
	cp documentation/fltk.pdf /tmp/fltk-$version/documentation/
fi

echo Applying version number...
cd /tmp/fltk-$version

fileurl=`echo $fileurl | sed -e '1,$s/\\//\\\\\\//g'`
sed -e '1,$s/@VERSION@/'$version'/' \
	-e '1,$s/@RELEASE@/'$rev'/' \
	-e '1,$s/^Source:.*/Source: '$fileurl'/' \
	<fltk.spec.in >fltk.spec

echo Creating configure script...
autoconf -f

echo Creating config.guess and config.sub \(ignore any other errors\)...
automake --add-missing --copy

echo Cleaning developer files...
rm -rf OpenGL autom4te* bc5 config forms glut images packages themes
rm -f makesrcdist

cd ..

if test $# != 0 -a "x$1" != "xsnapshot"; then
	echo "Making HTML docs distribution..."
	$TAR czf fltk-$fileversion-docs-html.tar.gz fltk-$version/documentation/html/

	echo "Making PDF docs distribution..."
	$TAR czf fltk-$fileversion-docs-pdf.tar.gz fltk-$version/documentation/fltk.pdf
fi

echo "Removing documentation..."
rm -rf fltk-$version/documentation/html/
rm -f fltk-$version/documentation/fltk.pdf

echo "Making UNIX distribution..."
$TAR czf fltk-$fileversion-source.tar.gz fltk-$version

#echo "Making BZ2 distribution..."
#$TAR cjf fltk-$fileversion-source.tar.bz2 fltk-$version

#echo "Making Windows distribution..."
#rm -f fltk-$fileversion-source.zip
#zip -r9 fltk-$fileversion-source.zip fltk-$version

echo "Removing distribution directory..."

rm -rf fltk-$version

echo "Done!"
