#!/bin/bash
# Repack tarball because the IETF RFC license has non-free restrictions.

REMOTE=upstream
BRANCH=${REMOTE}/master

if [ $(git remote show ${REMOTE} | wc -l) -eq 0 ]; then
	git remote add ${REMOTE} https://github.com/google/libkml.git
fi

git fetch ${REMOTE} --no-tags

PACKAGE=$(dpkg-parsechangelog | grep ^Source: | awk '{print $2}')

COMMIT=$(git log -n1 --format=format:%h ${BRANCH})
DATE=$(date +%Y%m%d --date="@$(git log -n1 --format=format:%ct ${BRANCH})")

VERSION="1.3.0~r864+git${DATE}-${COMMIT}"

git archive --format=tar.gz --prefix=${PACKAGE}-${VERSION}/ -o ../${PACKAGE}_${VERSION}.orig.tar.gz ${BRANCH}

gunzip -c ../${PACKAGE}_${VERSION}.orig.tar.gz | tar --delete --wildcards -vf - ${PACKAGE}-${VERSION}/third_party/uriparser-\*/doc/rfc\* ${PACKAGE}-${VERSION}/third_party/\*.win32/\* | GZIP="--best --no-name" gzip -c > ../${PACKAGE}_${VERSION}+dfsg.orig.tar.gz
