#!/bin/bash

REMOTE=upstream
BRANCH=${REMOTE}/master

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

git fetch ${REMOTE} --no-tags

PACKAGE=$(dpkg-parsechangelog -S Source)
VERSION=$(dpkg-parsechangelog -S Version | sed -e 's/-[^-]*$//' | sed -e 's/^[0-9]*://' | sed -e 's/^[0-9]*://' | sed -e 's/\+.*//')

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

VERSION="${VERSION}+git${DATE}-${COMMIT}"

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