#!/bin/bash
# hb_mk_tar
# Since the split of qtcontribs to a separate vcs in Sept 2012
# this script will re-merge them into one tarball
##############################################################
# Run this script in SOURCES with ./hb_mk_tar
# Option -noclean will keep existing git/svn folders and update them. 
# Running without -noclean removes any pre-existing folders and
# deletes the new ones on completion.
##############################################################

# Deps
rpm -q git-core > /dev/null 2>&1 || urpmi git-core
rpm -q subversion > /dev/null 2>&1 || urpmi subversion
rpm -q tar > /dev/null 2>&1 || urpmi tar

# Remove any pre-existng folders
if [[ $1 != "-noclean" ]]; then
[[ -e harbour ]] && rm -rf harbour
[[ -e qtcontribs ]] && rm -rf qtcontribs
fi

# Clone Harbour git
if [[ -e harbour ]]; then
cd harbour
git pull || exit 1
cd ..
else
git clone http://github.com/harbour/core.git harbour || exit 1
fi

# Get git revision
cd harbour
hbrev=$(git rev-list HEAD | wc -l)
cd ..

# Checkout qtcontribs
if [[ -e qtcontribs ]]; then
cd qtcontribs
svn up || exit 1
cd ..
else
svn co svn://svn.code.sf.net/p/qtcontribs/code/trunk qtcontribs || exit 1
fi

# Get qtcontribs revision
cd qtcontribs
hbqtrev=$(svnversion)
cd ..
[[ ${#hbqtrev} = 0 ]] && exit 1

# Merge
cp -r qtcontribs/* harbour/contrib

# Create revision
tarev=$hbrev.$hbqtrev

# Create tarball with harbour.<harbour rev>.<qtcontribs rev>.tar.gz naming.
echo "Please wait creating tarball..."
[[ -f harbour-$tarev.tar.xz ]] && rm harbour-$tarev.tar.xz
tar -cJf harbour-$tarev.tar.xz --exclude-vcs harbour/
[[ $? = 0 ]] && chmod 644 harbour-$tarev.tar.xz && \
echo "Written harbour-$tarev.tar.xz"

if [[ $1 != "-noclean" ]]; then
rm qtcontribs -rf
rm harbour -rf
fi
