#! /bin/sh

# This script is used to generate the codeblocks orig tarball used for this
# package.
# It is based on ubuntus get-orig-source script.
# Modified by Jens Lody (jens@codeblocks.org) to fetch an orig tarball from codeblocks
# subversion archive.

# Some variables to make maintaining this script easier
CODEBLOCKS_BASE_VERSION="13.12"
CODEBLOCKS_SVN_URL="http://svn.code.sf.net/p/codeblocks/code/branches/release-13.12"
CODEBLOCKS_LOCAL_DIR="../codeblocks-$CODEBLOCKS_BASE_VERSION-download/codeblocks-$CODEBLOCKS_BASE_VERSION"
OLD_DIR=`pwd`

USAGE='This script is used to generate the orig tarball used in building 
Debian packages from codeblocks svn (HEAD). 
Usage: get-orig-source-from-svn [OPTION] 
 
 -h, --help                 Display this help message. 
 --remove-upstream-dir      Remove downloaded files.
'

while [ "$#" -gt "0" ]
do
    case "$1" in
        --remove-upstream-dir)
            REMOVE_UPSTREAM_DIR=1
            shift
            ;;
        -h|--help|*)
            echo >&2 "${USAGE}"
            exit 1
            ;;
    esac
done

set -e

# Function to download files. Takes two parameters, the directory name of the
# url to use, and the filename of the file.
download() {
    local url="$1"
    local target="$2"
    mkdir -p $target
    svn co $1 $2 --non-interactive
}

# The rest is our main functions.
#Download the files
echo "Now downloading svn sources"
download $CODEBLOCKS_SVN_URL $CODEBLOCKS_LOCAL_DIR

# cd to source-dir
echo "change directory to $CODEBLOCKS_LOCAL_DIR"
cd $CODEBLOCKS_LOCAL_DIR

# update Changelog
echo "update Changelog"
./updateChangeLog.sh

# run bootstrap
echo "update/create Makefile's and configure-script"
./bootstrap

## fetching actual svn-revision
#if svn --xml info >/dev/null 2>&1; then
#	SVN_REV=`svn --xml info | tr -d '\r\n' | sed -e 's/.*<commit.*revision="\([0-9]*\)".*<\/commit>.*/\1/'`
#elif svn --version --quiet >/dev/null 2>&1; then
#	SVN_REV=`svn info | grep "^Revision:" | cut -d" " -f2`
#else
#	SVN_REV=0
#fi
#
#if [ "$SVN_REV" -eq "0" ]; then
#    echo "Error retrieving svn version, can not continue !"
#    exit 1
#fi

#CODEBLOCKS_ORIG_TARBALL=codeblocks_$CODEBLOCKS_BASE_VERSION$SVN_REV.orig.tar.gz
CODEBLOCKS_ORIG_TARBALL=codeblocks_$CODEBLOCKS_BASE_VERSION.orig.tar.gz

if [ -f ../../$CODEBLOCKS_ORIG_TARBALL ]; then
    echo "We already have the newest revision in $CODEBLOCKS_ORIG_TARBALL ."
    exit 0
fi


# Create source tarball
echo "Create sources tarball"
#echo "pre-configuring sources"
#./configure --enable-keep-dlls=no
cd ..
#echo "creatingng tarball $CODEBLOCKS_ORIG_TARBALL "
tar zcf $CODEBLOCKS_ORIG_TARBALL --exclude=".svn" --exclude="*.dll" codeblocks-$CODEBLOCKS_BASE_VERSION

# Move tarball to right directory
mv $CODEBLOCKS_ORIG_TARBALL ..

cd $OLD_DIR
./update_revision.sh

# Perform cleanup
if [ ! -z "$REMOVE_UPSTREAM_DIR" ]; then
    echo "Removing upstream files."
    rm -rf $CODEBLOCKS_LOCAL_DIR
fi
