#!/bin/bash
#
# forgive me for the state of this script

set -e

tmp=$(mktemp -d)
pwd=$(pwd)

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

chromium_svn_dir=$1
chromium_tgt_dir=${tmp}/${chromium_svn_dir}

[ ! -d $chromium_svn_dir ] && \
    echo "No such directories: $chromium_svn_dir" && \
    [ -f ${chromium_svn_dir}.tar.xz ] && \
    echo "Found $chromium_svn_dir.tar.xz" && \
    echo "Decompressing $chromium_svn_dir.tar.xz" && \
    tar -xf ${chromium_svn_dir}.tar.xz

mkdir -p ${chromium_tgt_dir}

name=chromium
#version="`cat $chromium_svn_dir/chrome/VERSION | cut -f2 -d= |while read i; do echo -n $i\. ; done`"
version="`cat $chromium_svn_dir/chrome/VERSION | cut -f2 -d= |while read i; do echo -n $i\. ; done`" && version=$(echo $version | sed s/\.$//)
#revision="svn`cat $chromium_svn_dir/src/.svn/entries | grep -m1 -A1 'dir' | tr '\n\r' '-' | cut -f2 -d-`"

echo "Version: $version"
#echo "Revision: $revision"

xz="`which xz 2>/dev/null`"
#xz="`which pxz 2>/dev/null`"
#lzma="`which lzma 2>/dev/null`"


if [ -z $chromium_svn_dir ]
then
	echo "Usage:  `basename $0` [SVN_SOURCE_DIR]"
	exit 1
fi

if [ -f $xz ]
then
	compress=$xz
	#compress_opts='-9 -F lzma' #xz compresses MUCH faster, so why not make it compress more?  We have the RAM...
	compress_opts='-9'
	echo "using xz"
else
    exit 1
	compress=$lzma
	compress_opts="-7"
	echo "using lzma"
fi

echo
echo "Moving source in staging area"
cp -ra $chromium_svn_dir ${chromium_tgt_dir}/src

# remove big bad ffmpeg binaries.
echo "Removing ffmpeg binaries/sources"
cd $chromium_tgt_dir/src/third_party/ffmpeg/
rm -rf binaries/ chromium/binaries/
rm -rf doc ffpresets mt-work tools tests
find -type f -name "*.c" | grep -v xcode_hack.c | xargs rm -f 
#echo "Removing courgette sources"
#cd $chromium_tgt_dir/src/
#rm -rf courgette/
echo "Removing o3d plugin sources (Not yet buildable)"
cd $chromium_tgt_dir/src/
rm -rf o3d/
echo "Removing unnecessary sources"
rm -rf third_party/WebKit/WebKitTools/Scripts/webkitpy/layout_tests/
rm -rf webkit/data/layout_tests/
#rm -rf third_party/hunspell/dictionaries/
rm -rf src/third_party/WebKit/LayoutTests/
rm -rf chrome/test/data/
rm -rf third_party/icu/icudt42.dll  
#rm -rf third_party/icu/linux/  
rm -rf third_party/icu/mac/  
#rm -rf third_party/icu/source/
rm -rf native_client/tests/
#rm -rf third_party/libvpx/ 
#rm -rf third_party/speex/
# Remove other directories (untested)
cd $chromium_tgt_dir/src/third_party
# First, just take out the sources for the items which have already been conditionalized so we're sure we're not using them.
# We need to leave the .gyp files since this is how it finds the system libs.
# bzip2
#rm -rf bzip2/*.c bzip2/*.h bzip2/LICENSE

# libjpeg
#rm -rf libjpeg/*.c libjpeg/README*

# libpng
#rm -rf libpng/*.c libpng/*.h libpng/README* libpng/LICENSE

# libevent 
#rm -rf libevent/*.c libevent/*.h libevent/*sh libevent/config* libevent/*.3 libevent/README libevent/compat libevent/linux libevent/mac libevent/sample libevent/test libevent/ChangeLog libevent/Makefile.* libevent/aclocal.m4 libevent/*.py libevent/mising libevent/mkinstalldirs

# libxml
#rm -rf libxml/*c libxml/*.h libxml/*.in libxml/*sh libxml/*.m4 libxml/*.py libxml/*.xml libxml/missing libxml/mkinstalldirs libxml/*.1 libxml/build libxml/include libxml/linux libxml/mac libxml/win32 libxml/AUTHORS libxml/COPYING libxml/ChangeLog libxml/Copyright libxml/INSTALL libxml/NEWS libxml/README libxml/README.tests libxml/TODO* libxml/config* libxml/*.pl

# libxslt
#rm -rf libxslt/build libxslt/libexslt libxslt/libxslt libxslt/linux libxslt/mac libxslt/win32 libxslt/AUTHORS libxslt/COPYING libxslt/ChangeLog libxslt/FEATURES libxslt/INSTALL libxslt/NEWS libxslt/README libxslt/TODO libxslt/*.h libxslt/*.m4 libxslt/compile libxslt/config* libxslt/depcomp libxslt/*sh libxslt/*.in libxslt/*.spec libxslt/missing

# Next, nuke the whole directories for things not yet conditionalized:
#rm -rf nss/ nspr/ 

# expat is only built on windows
#rm -rf expat/files

# Another copy of zlib? Unpossible!
#rm -rf src/third_party/WebKit/WebCore/platform/image-decoders/zlib/

# #Remove v8 as that we build against the system one
# rm -rf src/v8/include src/v8/src/


echo 
echo "Recompressing and excluding svn data"
echo "  this takes a while, sorry"
echo "  Compressing with $compress"

cd $chromium_tgt_dir/
cd ..

tar --exclude=\.svn -cf - ${name}-${version} | $compress ${compress_opts} > ${pwd}/${name}-${version}-vine.tar.xz

#rm -rf $chromium_tgt_dir

exit