#!/bin/bash
# Script: svn2tar
# Script that creates distribution tar from the local svnroot instead via the normal "rear mkdist"
# Author: Schlomo Schapiro
# License: see COPYING file
# Last Update: $Id: svn2tar 55 2009-11-16 19:56:19Z schlomoschapiro $

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

echo "
******************************************
**	svn2tar script for rear      	**
******************************************
" 1>&2
# find the full path to this script
ME="$(cd $(dirname "$0") ; pwd)/$(basename "$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/rear
prod_ver="rear-$VERSION"
distarchive="/tmp/${prod_ver}.tar.gz"

BUILD_DIR=/tmp/rear.$$
trap "rm -rf $BUILD_DIR" 0

# extract version number from rear script
VERSION=$(grep ^VERSION= ${rear_root}/usr/sbin/rear | cut -d= -f2 | sed -e 's/"//g')

# 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="rear-$VERSION"
distarchive="/tmp/${prod_ver}.tar.gz"

BUILD_DIR=/tmp/rear.$$
trap "rm -rf $BUILD_DIR" 0

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

ProgressStart "Building dist tar for rear $VERSION" 1>&2


# use tar to copy the current rear while excluding development and obsolete files
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 1>&8

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

WORKFLOW_mkdist_postprocess 1>&2

popd >/dev/null
ProgressStop 1>&2

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