#!/bin/sh
set -ex

PACKAGE=slicer

VERSION=${VERSION:-$2}
TARFILE=${TARFILE:-$3}

if [ -z ${VERSION}]; then
VERSION=`dpkg-parsechangelog | sed -n -e 's/^Version: \(.*\)-[^-]*$/\1/p'`
fi

REVISION=`echo $VERSION | sed -e 's/^.*svn//g'`


if [ -z ${TARFILE}]; then
TARFILE=${PACKAGE}_${VERSION}.orig.tar.gz
fi

FOLDER=${PACKAGE}-${VERSION}

#svn checkout --quiet --revision ${REVISION} http://svn.slicer.org/Slicer3/trunk ${FOLDER}
#svn export --quiet --revision ${REVISION} http://svn.slicer.org/Slicer3/trunk ${FOLDER}
svn export --quiet --revision ${REVISION} http://svn.slicer.org/Slicer3/branches/Slicer-3-6 ${FOLDER}

cd ${FOLDER}
#rm ./Utilities/Launcher/*.tcl
#rm ./Utilities/Launcher/CMakeLists.txt
rm -rf ./QTModules/
rm -rf ./Utilities/Launcher/Slicer3Launchers/
rm -rf ./Utilities/Launcher/tclkits/
rm -rf ./Utilities/Launcher/Slicer3.vfs/
rm -f ./Utilities/Launcher/3DSlicerLogoICOimages/Thumbs.db
rm -rf ./Libs/SlicerExecutionModel/tclap/
#- find . -type f -name *.vcproj | xargs rm
rm -f ./Modules/QueryAtlas/Resources/controlledVocabulary.xls
#rm -f ./Modules/ChangeTracker/Testing/scan2.raw.gz
#rm -f ./Modules/ChangeTracker/Simulation/original.raw.gz
#rm -f ./Modules/ChangeTracker/Testing/scan1.raw.gz
# remove jar files
rm -rf ./Modules/QueryAtlas/OntologyViz/
#rm -rf ./Testing/
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/StandardData/StandardSegmentationResult_small_normalizationOn.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/StandardData/StandardSegmentationResult_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/atlasBackgroundReg_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/atlasCSFReg_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/atlasGreymatterReg_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/atlasWhitematterReg_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/segmentationResult_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/targetT1Normed_small.raw
#rm ./Modules/EMSegment/Testing/TestData/TutorialTest/VolumeData/targetT2Normed_small.raw
rm ./Modules/Editor/ImageData/Thumbs.db
rm ./Modules/MRAblation/.vtkMRAblationLogic.cxx.swo
rm -rf ./Modules/ProstateNav/newmat
#grep -rnl Davies ./Modules/ProstateNav/newmat/ | xargs rm
cd ..

GZIP=-9 tar czf ${TARFILE} ${FOLDER}
rm -rf ${FOLDER}

