#!/bin/sh
# if you need to repack for whatever reason you can
# use this script via uscan or directly
#
# FIXME: currently the code is not conform to Debian Policy
#        http://www.debian.org/doc/debian-policy/ch-source.html
#        "get-orig-source (optional)"
#        This target may be invoked in any directory, ...
# --> currently it is assumed the script is called in the
#     source directory featuring the debian/ dir

COMPRESS=xz

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

# RDNAPTRANS2008
# https://www.kadaster.nl/web/file?uuid=f7ea740f-0f3c-402a-bf34-896dac7d557f&owner=23cbe925-35ce-4a72-ac8c-a33a0c19ae1e&contentid=9675
#
# Download page:
# https://www.kadaster.nl/web/show?mode=preview&contentid=9674&id=107118&l_id=17022&webid=26098

if ! echo $@ | grep -q upstream-version ; then
    VERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/\([0-9\.]\+\)-[0-9]\+$/\1/'`
    wget "https://www.kadaster.nl/web/file?uuid=f7ea740f-0f3c-402a-bf34-896dac7d557f&owner=23cbe925-35ce-4a72-ac8c-a33a0c19ae1e&contentid=9675" -O ../RDNAPTRANS2008.zip
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

mkdir -p ../tarballs
cd ../tarballs
# need to clean up the tarballs dir first because upstream tarball might
# contain a directory with unpredictable name
rm -rf *

unzip ../RDNAPTRANS2008.zip -d RDNAPTRANS2008

# RDNAPTRANS2008.zip is not redistributable! 
# The grid correction files for PROJ.4 are shipped in the file NTv2_2008.zip,
# which is more premissivly licensed.

unzip RDNAPTRANS2008/NTv2_2008.zip

UPSTREAMTARDIR="NTv2_2008/"
TARDIR=${NAME}-${VERSION}

if [ "${UPSTREAMTARDIR}" != "${TARDIR}" ] ; then
    mv "${UPSTREAMTARDIR}" "${TARDIR}"
fi

GZIP="--best --no-name" tar --owner=root --group=root --mode=a+rX -caf ../"$NAME"_"$VERSION".orig.tar.${COMPRESS} "${TARDIR}"
cd ../
rm -rf tarballs
