#!/bin/sh -e
# This script automatically downloads the latest snapshot and imports it
# in the upstream git branch.

TIN_VERSION='2.6.2'

[ -d debian/ ] || exit 1

wget -P .. ftp://ftp.tin.org/pub/news/clients/tin/v${TIN_VERSION%.*}/snapshots/tin-$TIN_VERSION.tar.xz

SNAPSHOT_DATE="$(tar xf ../tin-$TIN_VERSION.tar.xz -O | sed -nre '/^-- 2\.[6-9]+\.[0-9]+ release [0-9]{8} .+/ { s/.+ release ([^ ]+) .+/\1/p;q }')"

if [ -z "$SNAPSHOT_DATE" ]; then
  echo "Could not determine the snapshot date!" >&2
  exit 1
fi

mv ../tin-${TIN_VERSION}.tar.xz ../tin_$TIN_VERSION~$SNAPSHOT_DATE.orig.tar.xz

gbp import-orig --no-symlink-orig ../tin_$TIN_VERSION~$SNAPSHOT_DATE.orig.tar.xz

dch -v 1:$TIN_VERSION~$SNAPSHOT_DATE-1 'New upstream snapshot.'

