#!/bin/bash

. ./devel.bash

. ./config-settings.bash

tar_file="selfcontained-$dist_build_ver-$GSDLOS.tar.gz"

# Test to make sure we are operating in a directory structure which
# was checked out as either:
#     /main/trunk/gs2-extensions/$gext_name
#     /main/trunk/gs3-extensions/$gext_name
#
# If it is, then this means 'source' is a subdir and therefore ../. is
# the location where the tarballs go and are commited

svn_prefix=$( cd .. && svn info | egrep "^Relative URL:" | awk -F': ' '{ print $2 }' )

if [ "x$svn_prefix" != "x^/main/trunk/gs2-extensions/$gext_name" ] \
       && [ "x$svn_prefix" != "x^/main/trunk/gs3-extensions/$gext_name" ] ; then
    echo "Parent directory is not under svn control as either:" >&2
    echo "  /main/trunk/gs2-extensions/$gext_name" >&2
    echo "or" >&2
    echo "  /main/trunk/gs3-extensions/$gext_name" >&2
    echo "" >&2
    echo "Unsafe to copy tarball to parent directory and commit" >&2
    echo "=> Stopping!" >&2
    exit 1
fi

# Check if we regenerated an existing tarball or a newly named tarball
needs_svnadd=0
if [ ! -f ../$tar_file ] ; then
    # First version ever => needs 'svn add' before doing 'svn commit'
    needs_svnadd=1
fi

/bin/cp dist/$tar_file  ../.

commit_msg=$1
if [ "x$commit_msg" = "x" ] ; then
    commit_msg="Latest dist generated version of $gext_name"
fi

if [ "$needs_svnadd" = "1" ] ; then
    (cd .. && svn add $tar_file )
fi
(cd .. && svn commit -m "$commit_msg" $tar_file )
