#!/bin/bash

set -e

tmp=$(mktemp -d)

trap cleanup EXIT
cleanup() {
    set +e
    [ -z "$tmp" -o ! -d "$tmp" ] || rm -rf "$tmp"
}

unset CDPATH
pwd=$(pwd)

dirname=v8

cd "$tmp"
#svn checkout http://v8.googlecode.com/svn/trunk $dirname
svn checkout -r 6522 http://v8.googlecode.com/svn/trunk $dirname
cd $dirname
MAJOR_VERSION=$(grep "#define MAJOR_VERSION" src/version.cc | sed -e "s/#define MAJOR_VERSION[[:blank:]]*//" -e "s/[[:blank:]]*$//")
MINOR_VERSION=$(grep "#define MINOR_VERSION" src/version.cc | sed -e "s/#define MINOR_VERSION[[:blank:]]*//" -e "s/[[:blank:]]*$//")
BUILD_NUMBER=$(grep "#define BUILD_NUMBER" src/version.cc | sed -e "s/#define BUILD_NUMBER[[:blank:]]*//" -e "s/[[:blank:]]*$//")
VERSION=${MAJOR_VERSION}.${MINOR_VERSION}.${BUILD_NUMBER}
REVISION=`LC_ALL=C svn info 2> /dev/null | grep Revision | cut -d' ' -f2`
find . -type d -name .svn -print0 | xargs -0r rm -rf
cd ..
mv $dirname ${dirname}-${VERSION}
tar jcf "$pwd"/${dirname}-${VERSION}.svn${REVISION}.tar.bz2 ${dirname}-${VERSION}
cd ${pwd} >/dev/null

exit
