#!/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"
    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/\+.*//'`

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

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

tar -xaf ../${PACKAGE}-${VERSION}.tar.*

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

# Remove files with unclear rights
#
#  gdal/data/cubewerx_extra.wkt
#   Derived from definitions distributed by Cubewerx.
#   See http://trac.osgeo.org/gdal/ticket/2165
# 
#  gdal/data/ecw_cs.dat
#   Derived via much processing from ERMapper GDT definitions.
#   See http://trac.osgeo.org/gdal/ticket/2162
#
#  gdal/data/esri_extra.wkt
#   Derived with some processing from projections definitions in ArcGIS.
#   See http://trac.osgeo.org/gdal/ticket/2163

rm ${PACKAGE}-${VERSION}/data/cubewerx_extra.wkt
rm ${PACKAGE}-${VERSION}/data/ecw_cs.wkt
rm ${PACKAGE}-${VERSION}/data/esri_extra.wkt

mv ${PACKAGE}-${VERSION} ${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

