#!/bin/sh
# get source for mauve from SVN because there is no source tarball distribution

# To rebuild the new upstream tarball you need these packages installed.  
# The reason is that the original source contains some icons copyrighted
# by Sun Microsystems Inc. and these will be replaced by free icons
# shipped with Gnome
BUILDTARBALLDEPENDS="imagemagick gnome-icon-theme"
missingdepends=`dpkg -l ${BUILDTARBALLDEPENDS} | \
    grep -v -e "^ii" -e "^|" -e "^++" -e "^ " -e "^..[^[:space:]]" | \
    sed 's/^[a-z][a-z][[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+.*/\1/'`

if [ "$missingdepends" != "" ] ; then
    echo "Please install the following packages to rebuild the upstream source tarball:"
    echo $missingdepends
    exit -1
fi

set -e
NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`

if ! echo $@ | grep -q upstream-version ; then
    VERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/\([0-9\.]\+\)-[0-9]\+$/\1/'`
else
    VERSION=`echo $@ | sed "s?^.*--upstream-version \([0-9.]\+\) .*${NAME}.*?\1?"`
    if echo "$VERSION" | grep -q "upstream-version" ; then
        echo "Unable to parse version number"
        exit
    fi
fi

SVNURI="svn://svn.code.sf.net/p/mauve/code/mauve/trunk"
revision=`LANG=C svn info ${SVNURI} | grep "^Last Changed Rev:" | sed 's/Last Changed Rev: *//'`
VERSION=`echo ${VERSION}| sed "s/+[0-9]\+$//"`+${revision}

TARDIR=${NAME}-${VERSION}

mkdir -p ../tarballs
cd ../tarballs
# svn export conserves time stamps of the files, checkout does not
LC_ALL=C svn export ${SVNURI} ${TARDIR} >/dev/null 2>/dev/null

cd ${TARDIR}
rm -rf osx win32 win64
rm -rf linux-x64/mauveAligner linux-x86/mauveAligner linux-x64/progressiveMauve linux-x86/progressiveMauve
find . -name "*.jar" -delete
rm -rf build_support

# Replace icons
#   Copyright 2000 by Sun Microsystems, Inc. All Rights Reserved.
# by pendants from gnome-icon-theme
cd src/images
GICONDIR16=/usr/share/icons/gnome/16x16/actions
GICONDIR24=/usr/share/icons/gnome/24x24/actions

convert $GICONDIR16/go-previous.png     Back16.gif
convert $GICONDIR16/go-down.png         Down16.gif
convert $GICONDIR16/go-next.png         Forward16.gif
convert $GICONDIR16/go-home.png         Home16.gif
convert $GICONDIR16/go-up.png           Up16.gif
convert $GICONDIR16/edit-find.png       Zoom16.gif
convert $GICONDIR16/zoom-in.png         ZoomIn16.gif
convert $GICONDIR16/zoom-out.png        ZoomOut16.gif

convert $GICONDIR24/zoom-in.png         ZoomIn24.gif
convert $GICONDIR24/zoom-out.png        ZoomOut24.gif

cd ../../..

tar --owner=root --group=root --mode=a+rX -caf "$NAME"_"$VERSION".orig.tar.xz "${TARDIR}"
rm -rf "${NAME}"-"$VERSION"
