#!/bin/bash
# hb_mk_core
##############################################################
# Run this script in SOURCES with ./hb_mk_core
# 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 tar > /dev/null 2>&1 || urpmi tar

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

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

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

tarev=$hbrev

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

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