#!/bin/sh
set -e

#################################################################
# This script makes an install package of EDGE Excel Add-In.
#
# Run it on any operating system - will create an OS-neutral
# package.
#
# This script has been tested on Mac OS X Panther and Windows 
# XP Professional.
#
#################################################################

# Get the operating system name and set up some OS-dependent
# variables.
BASEDIR=../../
XARGS_REPLACE=-I
BRANCH_SUBDIR=branches/1.0
DELETEFILES="edge_bugs.txt scripts/ design/ src/scripts/make_install_package src/excel/ src/edge_start/"

OS=`uname -s`
case $OS in 
CYGWIN*)
	WINDOWS=1
	;;
MINGW*)
	WINDOWS=1
	XARGS_REPLACE=--replace=
	;;
Darwin*)
	MAC=1
	;;
*)
	echo "Unsupported OS: $OS"
	exit 1
	;;
esac

# Set up Subversion variables
if test "$SVN_PREFIX" = ""
then
    SVN_PREFIX=svn+ssh://sum.biostat.washington.edu
fi

SVN_REPO=$SVN_PREFIX/home/devel/storeysoft/svn/edge/$BRANCH_SUBDIR/src/excel

#
# Get EDGE repository from Subversion.
#

# First get everything to a temporary directory
EDGEDIRTMP=edge_excel.tmp

if test -d "$EDGEDIRTMP"
then
    echo "directory $EDGEDIRTMP exists -- please delete and try again"
    exit 2
fi

echo "Getting $SVN_REPO. Password may be required."
svn export $SVN_REPO $EDGEDIRTMP > svnout

# Get the revision number that we just exported
# and rename the temp directory accordingly
REVISION=`cat svnout | grep "^Exported" | perl -ne 'm/([0-9]+)/; print $1'`
if test "$REVISION" = ""
then
    echo "could not get SVN revision of $BASEDIR"
    exit 1
else
    rm -rf svnout
fi
VERSION=\\\"1.0.$REVISION\\\"
EDGEDIR=edge_excel.1.0.$REVISION

if test -d "$EDGEDIR"
then
    echo "directory $EDGEDIR exists -- please delete and try again"
    exit 2
fi

mv "$EDGEDIRTMP" "$EDGEDIR"

echo "Revision = $REVISION"
echo "Edge Dir = $EDGEDIR"


CURDIR=`pwd`
cd $EDGEDIR/

# Simply remove files we don't want to ship
rm -rf $DELETEFILES

cd $CURDIR

#
# Archive and compress the release.
#
echo "Creating archive"

FINALDIR=EDGE_Excel
rm -rf $FINALDIR/
mv $EDGEDIR/ $FINALDIR

case $OS in
    CYGWIN* | MINGW*)
	TARFILE=$EDGEDIR.zip
	zip -rq $TARFILE $FINALDIR/
	$WINZIPSE $EDGEDIR.zip -y -d "C:\Program Files\R\EDGE" -m edge.excel.message.txt -le -overwrite
	;;
    *)
	TARFILE=$EDGEDIR.tar.gz
	tar czf $TARFILE $FINALDIR/
	;;
esac
