#!/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."

[ -z "${1:-}" ] || die "No command line arguments supported"

PKG="$(dpkg-parsechangelog --show-field Source)"
USCAN="$(uscan --dehs|| true)"
if echo "$USCAN" | grep -q "<status>newer package available</status>"; then
    ORIG=$(echo "$USCAN" | grep "<target-path>"|sed -e's,</\?target-path>,,g')
    echo "Importing $ORIG"
    gbp import-orig "$ORIG"
fi

POD=<<'EOF'
=head1 NAME

dpt-uscan - a tiny wrapper around C<uscan> and C<gbp import-orig>

=head1 SYNOPSIS

B<dpt uscan>

=head1 DESCRIPTION

B<dpt uscan> calls L<uscan(1)> and then B<gbp import-orig>
if a newer upstream version was downloaded.

It is similar to L<dpt-import-orig(1)>, but without the upstream repository
integration.

Running B<dpt uscan> is equivalent to

 uscan
 gbp import-orig I<upstream-tarball>

with the location of the newly downloaded tarball detected from uscan's
B<--dehs> output, and therefore is not hardcoded to F<../>, but depending on
uscan's configuration.

=head1 SEE ALSO

L<dpt-import-orig(1)>, L<uscan(1)>, L<devscripts.conf(5)>,
L<gbp-import-orig(1)>

=head1 COPYRIGHT & LICENSE

=over

=item Copyright 2017 Damyan Ivanov L<dam@debian.org>

=back

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

=cut
EOF
