#!/bin/sh

# copyright, licensing and documentation at the end

set -e
set -u

die() {
	echo "$1" >&2
	exit 1
}

warn() {
	echo "$1" >&2
}

# sanity checks
dh_testdir  || die "This doesn't look like a source package directory."
[ -d .git ] || die "No .git directory found."

# defaults for configuration variable(s)

DPT_RM_TARGZ=${DPT_RM_TARGZ:-''}

# upstream-repo remote
dpt upstream-repo
if git remote show | grep -qx upstream-repo ; then
	git fetch upstream-repo
fi

gbp pull

# uscan and versions
PKG="$(dpkg-parsechangelog --show-field Source)"
USCAN="$(uscan --report-status --dehs|| true)"
UVERSION="$(echo $USCAN | perl -nE '/<upstream-version>([^<]+)<\/upstream-version>/ and say $1')"
UVERSIONZERO="$(echo "$UVERSION" | perl -ne 'print if s/0+$//')"
# in case of error, don't crash, just set to null string.
UTAG=''
UTAG="$(git tag | grep -x "v\?$UVERSION" || git tag | grep -- "-$UVERSION\$")" || /bin/true
if [ -z "$UTAG" ] && [ -n "$UVERSIONZERO" ]; then
	UTAG="$(git tag | grep -x "v\?$UVERSIONZERO" || git tag | grep -- "-$UVERSIONZERO\$")" || /bin/true
fi
UNAME=$(awk '/Upstream-Name:/ {print $2;}' debian/copyright)

# download and import
if  command -v gbp 1>/dev/null 2>&1; then
	IMPORT_CMD="gbp import-orig"
else
	IMPORT_CMD=git-import-orig
fi

# we're using --no-interactive here because
# the dpt() shell function and "gbp import-orig"
# don't play well together (waiting for input before showing the question)
${IMPORT_CMD} --no-interactive --uscan --pristine-tar ${UTAG:+--upstream-vcs-tag="$UTAG"}

# Create a changelog entry as a start, and to un-confuse git-dch later
DVERSION="$(dpkg-parsechangelog --show-field Version)"
# from uupdate
eval $(echo "$DVERSION" | perl -ne '/^(?:(\d+):)?(.*)/; print "EPOCH=$1\n";')
NEWVERSION="${UVERSION}-1"
if [ -n "$EPOCH" ] ; then
	NEWVERSION="${EPOCH}:${UVERSION}-1"
fi
dch -v${NEWVERSION} --no-auto-nmu --mainttrailer --release-heuristic=changelog "Import upstream version ${UVERSION}"
git add debian/changelog
git commit -m "Update debian/changelog" -m "Gbp-Dch: Ignore"

# cleanup on request
if [ -n "$DPT_RM_TARGZ" ] ; then
	rm -fv ../${PKG}*.tar.gz ../${PKG}*.tgz
	rm -fv ../${UNAME}*.tar.gz ../${UNAME}*.tgz
fi

POD=<<'EOF'
=head1 NAME

dpt-import-orig - "gbp import-orig" wrapper with upstream tracking support

=head1 SYNOPSIS

B<dpt import-orig>

=head1 DESCRIPTION

B<dpt import-orig> is a wrapper around B<gbp import-orig> using its
B<--uscan> and B<--upstream-vcs-tag> options to fetch and import tarballs
and link to upstream Git tags.

If necessary, the B<git remote> is added using L<dpt-upstream-repo(1)>.

=head1 CONFIGURATION

B<dpt import-orig> reads the C<DPT_RM_TARGZ> environment variable to
determine if the downloaded F<.orig.tar.gz> should be removed after the
import.

See L<dpt-config(5)> for details.

=head1 SEE ALSO

L<dpt-upstream-repo(1)>

=head1 COPYRIGHT & LICENSE

=over

=item Copyright 2013 Jonas Smedegaard L<dr@jones.dk>

=item Copyright 2013-2014 gregor herrmann L<gregoa@debian.org>

=item Copyright 2014 David Bremner L<bremner@debian.org>

=back

This program is free software, licensed under the same term as perl.

=cut
EOF
