#!/bin/sh -x
# Repack tarball because of data files with unknown license

set -e

# Remark: A new uscan that enables easier handling of removing files can be
#         obtained via
#   git clone git://tille@git.debian.org/git/users/tille/devscripts.git
#         and then copy scripts/uscan.pl as uscan at the beginning of your PATH
if uscan --help | grep -q -- --repack-compression ; then
    echo "Use new enhanced uscan"
    # Don't repack with xz compression until the next upstream release.
    #uscan --verbose --force-download --repack-compression xz
    uscan --verbose --force-download
    exit
fi

# Falling back to manually removing files

PACKAGE=`dpkg-parsechangelog | sed -n 's/^Source: //p'`
VERSION=`dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p' | sed -e 's/\+.*//'`
UVERSION=`echo $VERSION | sed 's/\.0\$//'`

uscan --verbose --force-download --no-symlink

mkdir -p ../tarballs
cd ../tarballs

tar -xaf ../${PACKAGE}-${UVERSION}.tar.gz

# Directory in upstream tarball can contain the project name twice
if [ -d "${PACKAGE}-${PACKAGE}-${UVERSION}" ]; then
	mv ${PACKAGE}-${PACKAGE}-${UVERSION} ${PACKAGE}-${UVERSION}
fi

# Remove data files with unknown license
find ${PACKAGE}-${UVERSION}/data/ -not -name world.tif -not -path "*/resources/*" -type f -delete

mv ${PACKAGE}-${UVERSION} ${PACKAGE}-${VERSION}+dfsg.orig

# Don't repack with xz compression until the next upstream release.
#XZ_OPT="-6v" tar --owner=root --group=root --mode=a+rX -caf ../"${PACKAGE}"_"${VERSION}"+dfsg.orig.tar.xz "${PACKAGE}-${VERSION}"+dfsg.orig
GZIP="--best --no-name" tar --owner=root --group=root --mode=a+rX -caf ../"${PACKAGE}"_"${VERSION}"+dfsg.orig.tar.gz "${PACKAGE}-${VERSION}"+dfsg.orig

rm -rf "${PACKAGE}-${VERSION}"+dfsg.orig

