#!/bin/sh
# This script packages the directory into a tar file.
# Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
# 
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  version 2 as published by the Free Software Foundation.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# make sure we're in a checked out svn copy
if [ ! -d .svn ]; then
   echo "ERROR: No SubVersion information found. This script requires an svn tree."
   exit 0
fi

# Check if SVN is available.  If not, then abort.
which svn >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "ERROR: svn command missing."
   exit 1
fi
which svnversion >/dev/null 2>&1
if [ $? != 0 ]; then
   echo "ERROR: svnversion command missing."
   exit 1
fi

######################################################################
# Package things up

if [ ! -f "Makefile.conf" ]; then
   echo "ERROR: This utility must be run from the top of the fossology source tree."
   exit 1
fi

eval `grep ^VERSION= Makefile.conf`

# SVN_REV is the last revision from svn info.  This is used for packaging.
SVN_REV="`svn info | grep '^Revision:' | awk '{print $2}'`"

TARBASE="fossology-$VERSION"
# Check for command-line for subversion
if [ "$1" == "-s" ]; then
   TARBASE="fossology-$VERSION-$SVN_REV"
fi

echo "*** Packaging $VERSION (svn $SVN_REV) into $TARBASE ***"

# Check for mixed revisions
# Warn if the current directory does not match SVN_REV, but allow it
SVN_CURR="`svnversion -n .`"
if [ "$SVN_CURR" != "$SVN_REV" ]; then
   echo "WARNING: Revision ($SVN_REV) does not match current directory"
   echo "  ($SVN_CURR). Using $SVN_REV, not $SVN_CURR. To use $SVN_CURR,"
   echo "  commit any changes and run 'svn up' before running this command."
   echo "  Proceeding anyway..."
fi

# Check for outstanding changes, warn if they exist, but allow it
STATUS=`svn status |wc -l`
if [ $STATUS != 0 ]; then
   echo "WARNING: there are outstanding changes in this svn tree, but the tar"
   echo "  file that will be created will be based on SVN revision $SVN_REV and"
   echo "  will not include these changes. Please run 'make clean' to clean up"
   echo "  built files and 'svn status' to see outstanding differences."
   echo "  Proceeding anyway..."
fi

if [ -d "../$TARBASE" ]; then
   echo "WARNING: ../$TARBASE exists, removing"
   rm -rf "../$TARBASE"
   if [ -d "../$TARBASE" ]; then
      echo "ERROR: Unable to delete ../$TARBASE, exiting."
      exit 2
   fi
fi

echo "*** Exporting svn version $SVN_REV to ../$TARBASE ***"
svn export -r "$SVN_REV" . "../$TARBASE" >/dev/null
if [ $? != 0 ]; then
   echo "ERROR: svn export failed."
   exit 3
fi

# Process the directory
(
cd "../$TARBASE"

echo "*** Storing svn version in Makefile.conf ***"
# Remove the dependency on svnversion; make the version static.
cp Makefile.conf Makefile.conf.svn
sed -e "s@^SVN_REV=.*@SVN_REV=${SVN_REV}@" Makefile.conf.svn > Makefile.conf
rm Makefile.conf.svn
)

# Create the tar
(
cd ..
if [ -f "$TARBASE.tar.gz" ]; then
   echo "WARNING: ../$TARBASE.tar.gz exists, removing"
   rm -f "$TARBASE.tar.gz"
   if [ -f "$TARBASE.tar.gz" ]; then
      echo "ERROR: unable to remove ../$TARBASE.tar.gz, exiting."
      exit 4
   fi
fi

echo "*** Creating tar ***"
tar --anchored --exclude=\*/debian -czf "$TARBASE.tar.gz" "$TARBASE"
if [ $? != 0 ]; then
   echo "ERROR: unable to create ../$TARBASE.tar.gz, exiting."
   exit 4
fi
)

# Clean up
echo "*** Cleaning up ***"
rm -rf "../$TARBASE"
if [ -d "../$TARBASE" ]; then
   echo "WARNING: Unable to clean up ../$TARBASE, exiting."
   exit 5
fi

echo "*** ../$TARBASE.tar.gz created successfully. ***"
