#!/bin/sh
VERSION=`grep SoftwareVersion installers/autopackage/default.x86.apspec|cut -d ' ' -f 2`
DESTINATION=`pwd`/TarballPackages
TREEROOT=`pwd`

if [ "$VERSION" == "" ]
then
   echo "I can't find the apspec file. This script needs to be run from"
   echo "the repository top level directory to find all it needs."
   exit 255
fi

if [ -e $DESTINATION/oolite-$VERSION-src.tar.gz ]; then
  echo "Source Tarball for Version $VERSION already exists"
  # TODO - query / param to continue anyway
  exit 0
fi

# clear out the cruft
rm -rf $DESTINATION/oolite-$VERSION-src
rm -f $DESTINATION/oolite-$VERSION-src.tar.gz
rm -rf $DESTINATION/oolite-$VERSION-data
rm -f $DESTINATION/oolite-$VERSION-data.tar.gz

if ! mkdir -p $DESTINATION/oolite-$VERSION-src;
then
   echo "Couldn't create directory for the source files"
   exit 255
fi

if ! mkdir -p $DESTINATION/oolite-$VERSION-data;
then
   echo "Couldn't create directory for the data files"
   exit 255
fi

for i in autopackage debian deps Doc FreeDesktop installers Schemata Oolite-importer Oolite.xcodeproj src tools GNUmakefile GNUmakefile.postamble Makefile README.txt contributors.txt
do
   if ! cp -r $i $DESTINATION/oolite-$VERSION-src;
   then
      echo "Failed to copy $i to $DESTINATION/oolite-$VERSION-src"
      exit 255
   fi
done

if ! cp -r Resources $DESTINATION/oolite-$VERSION-data;
then
   echo "Failed to copy Resources to $DESTINATION/oolite-$VERSION-data"
   exit 255
fi

# Save the SVN Revision number in the source tarball for the tools/mkversion script
echo `tools/mkversion --subversion` > $DESTINATION/oolite-$VERSION-src/svnrevision

#echo "Press a key to continue"
#read -s -n 1

cd $DESTINATION
mv oolite-$VERSION-src oolite-$VERSION
if ! tar zcf oolite-$VERSION-src.tar.gz oolite-$VERSION --exclude .svn
then
   echo "Failed to create src tarball"
   exit 255
fi

# so the data tarball has the same path as src
rm -rf oolite-$VERSION
mv oolite-$VERSION-data oolite-$VERSION
if ! tar zcf oolite-$VERSION-data.tar.gz oolite-$VERSION --exclude .svn
then
   echo "Failed to create data tarball"
   exit 255
fi

rm -rf oolite-$VERSION
echo "Tarballs placed in $DESTINATION."

