#!/bin/bash
# Script: svn2tar
# Script that creates distribution tar from the local svnroot instead via the normal "rear mkdist"
# We build our dist releases from that!
# Author: Schlomo Schapiro
# License: see COPYING file
# Last Update: $Id: svn2tar 736 2011-11-22 09:21:35Z gdha $

##################### M A I N ####################

echo "
******************************************
**	svn2tar script for rear      	**
******************************************
" >&2

# find the full path to this script
ME="$(readlink -f "$0")"
# Find the svnroot path for the initial tar-ball
rear_root="${ME%/usr/share/rear/contrib/svn2tar}"
### echo The root of rear SVN directory is $rear_root

# set -x

SHARE_DIR=/usr/share/rear
CONFIG_DIR=/etc/rear
VAR_DIR=/var/lib/rear

# extract version number from rear script or use the one given
if ! test "$VERSION" ; then
	VERSION=$(grep ^VERSION= ${rear_root}/usr/sbin/rear | cut -d= -f2 | sed -e 's/"//g')
fi

if [ "$VERSION" = 0.0.0 -a -d "$rear_root/.svn" ] ; then
	# try to set version from SVN
	svn up "$rear_root" >&/dev/null
	read junk rev < <(svn info "$rear_root" | grep ^Revision)
	if ! test "$rev" -a "$rev" -gt 0 ; then
		echo "ERROR: Could not determine version from svn". >&2
		exit 1
	fi
	VERSION="0.0.$rev"

fi

# source required ReaR components
## *mkdist* functions
. "$rear_root$SHARE_DIR/lib/mkdist-workflow.sh"
## Progress* functions, the < hack disables the fancy progress stuff
. "$rear_root$SHARE_DIR/lib/_input-output-functions.sh" </dev/null

prod_ver="${prod_name:-rear}-$VERSION"
distarchive="${DESTDIR:-/tmp}/${prod_ver}.tar.gz"


BUILD_DIR=/tmp/rear.$$
AddExitTask "rm -rf $BUILD_DIR"

mkdir -p $BUILD_DIR/$prod_ver 
echo "Will be using a temporary BUILD_DIR=${BUILD_DIR}" >&2



# use tar to copy the current rear while excluding development and obsolete files
if type -p svn >/dev/null && test -d $rear_root/.svn && svn info $rear_root >/dev/null ; then
	# use svn export
	ProgressStart "Building dist tar for rear $VERSION using svn export" >&2
	svn export --force $rear_root $BUILD_DIR/$prod_ver >&8
else
	ProgressStart "Building dist tar for rear $VERSION" >&2
	tar --exclude=hpasmcliOutput.txt --exclude=\*~ --exclude=\*.rpmsave\* \
     		 --exclude=\*.rpmnew\* --exclude=.\*.swp --exclude .svn \
		 -c -C $rear_root usr etc | tar -C $BUILD_DIR/$prod_ver -x -v >&8
fi

# cleaning up the $BUILD_DIR/$prod_ver directory before starting rpmbuild
pushd $BUILD_DIR/$prod_ver >/dev/null

WORKFLOW_mkdist_postprocess >&2

popd >/dev/null
ProgressStop >&2

echo Creating final $distarchive of rear version $prod_ver >&2
tar -C $BUILD_DIR -czf $distarchive $prod_ver 
echo $distarchive
