#!/bin/bash

. ./config-settings.bash

export GSDLOS=linux

# Work out the bitness for the environment we compiled subversion for
true_bitness=`getconf LONG_BIT`

echo "@@@ In TARUP.sh: tarring up subversion for True bitness $true_bitness"

# remove the packages not used to compile subversion $version (1.10.2)
# which were experimental or used for compiling an older version of subversion
# TODO:
# We may one day be able to remove them from SVN itself. Some of these packages
# are possibly newer versions than we are using? But perhaps we still need not
# leave them lingering about on SVN if not used in compiling subversion?
delfilelist=("apr-1.6.5.tar.gz" "openssl-1.0.0b.tar.gz" "openssl-1.0.2u.tar.gz" "serf-1.3.1.tar.bz2" "sqlite-3.7.17.tar.gz" "subversion-1.8.1.tar.gz" "subversion-1.8.x.tar.gz")

for file in ${delfilelist[@]}; do
    if test -f "packages/$file"; then 
	echo "Not used in subversion $version. Deleting from packages subfolder: $file"
	rm -f "packages/$file"
    fi
done
echo ""


pushd ..
echo "Generating tarballs of subversion $version's binary and of its source"

# But unable to store for reuse a variable like TAR_OPTIONS=--transform='s,^oldname/,newname/,'
# as the quotes around the regex get undone by the time bash runs the full tar commands
# stored as
# cmd="tar -cvzf subversion-src.tar.gz source/*.sh source/*.bash source/cascade-make source/packages --transform=$TAR_OPTIONS"
# cmd="tar -cvzf subversion-$GSDLOS.tar.gz source/setup.ba* source/linux $TAR_OPTIONS"
# The cmds look write when printed to the screen but are not what gets run (value of transform loses quotes)
# Gemini solution is to store and run the full tar commands as arrays, which will be the commands as echoed.

# Want the source folder to remain as "source"xs in the source tarball generated
# but renamed as "subversion" in the binary tarball generated

# Get name of toplevel (formerly trunk) directory
trunkdirname=$(basename "$(pwd)")
TRANSFORM_EXPR="s,^$trunkdirname/,subversion-trunk/,"

# Go up into trunk folder to tar up trunk, and select contents of its subfolder src
pushd ..
# Command to tar up the source code of all trunk folder. Command is in array form to preserve the regex of the transform option
#TAR_COMMAND=(tar --exclude="$trunkdirname/src/cascade-make/.svn" -cvzf subversion-src.tar.gz \
TAR_COMMAND=(tar -cvzf subversion-src.tar.gz \
		 $trunkdirname/source/*.sh \
		 $trunkdirname/source/*.bash \
		 $trunkdirname/source/cascade-make \
		 $trunkdirname/source/packages \
		 $trunkdirname/.svn \
		 --transform="$TRANSFORM_EXPR")
# There's no .svn folder in $trunkdirname/source/.svn 
echo "*** Running: ${TAR_COMMAND[@]}"
# execute the command array
"${TAR_COMMAND[@]}" 2>&1
/bin/mv subversion-src.tar.gz $trunkdirname/.
popd

# Tar up the binary next. We're now in the toplevel (formerly trunk) directory
TRANSFORM_EXPR='s,^source/,subversion/,'
TAR_COMMAND=(tar -cvzf subversion-linux${true_bitness}.tar.gz source/setup.ba* source/linux --transform="$TRANSFORM_EXPR")
echo "*** Running: ${TAR_COMMAND[@]}"
"${TAR_COMMAND[@]}" 2>&1
popd


#cmd="./CASCADE-MAKE.sh makedist"
#echo "Running $cmd to make tarballs of subversion $version's binary and of its source"
#$cmd 2>&1

if [ -f ../subversion-src.tar.gz ] && [ -f ../subversion-$GSDLOS${true_bitness}.tar.gz ]; then
    echo ""
    echo "----"
    echo "To commit the generated subversion $version binary ($true_bitness bit) and source tarballs into the SVN repo, next run"
    echo "  ./GS-SUBVERSION-DIST-SVN-COMMIT.sh"
    echo "----"
    echo ""
else
    echo "ERROR: subversion-src.tar.gz or subversion-linux.tar.gz does not exist"
fi
