#!/bin/bash

. ./config-settings.bash

export PATH="$PWD/dist/perl-$version/bin:$PATH"

# Test to make sure we are operating in a directory structure which was checked out as
#  /gs2-extensions/perl-with-cpan/trunk
# meaning 'src' 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 ^/gs2-extensions/perl-with-cpan/trunk" ] ; then
    echo "Parent directory is not under svn control as:" >&2
    echo "  /gs2-extensions/perl-with-cpan/trunk" >&2
    echo "Unsafe to copy Perl tarball to parent directory and commit" >&2
    echo "Stopping!" >&2
	exit 1
fi

perl_archname=$( echo 'use Config ; print "$Config{archname}"' | ./dist/perl-$version/bin/perl - )

# Check if we regenerated an existing tarball or a newly named tarball
needs_svnadd=0
if [ ! -f ../perl-$version-$perl_archname.tar.gz ] ; then
    needs_svnadd=1
fi

/bin/cp dist/perl-$version-$perl_archname.tar.gz  ../.

commit_msg=$1
if [ "x$commit_msg" = "x" ] ; then
    commit_msg="Latest version of a relocatable perl for linux with CPAN packages used by Greenstone."
fi

if [ "$needs_svnadd" = "1" ] ; then
    (cd .. && svn add perl-$version-$perl_archname.tar.gz )
fi
(cd .. && svn commit -m "$commit_msg" perl-$version-$perl_archname.tar.gz )
