#!/bin/sh

# copyright, licensing and documentation at the end

set -e
set -u

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

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

NOGIT=0

# command line arguments
# -g don't call git add/commit for debian/upstream/metadata
#    to be used from .mrconfig
while getopts g OPTS; do
	case "$OPTS" in
		g)
			NOGIT=1
			;;
		*)
			;;
	esac
done
shift $((OPTIND -1))

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

if ! git remote show | grep -qx upstream-repo ; then
	warn "Git remote 'upstream-repo' not found, trying to add it ..."

	# case 1: Git URL in d/copyright's Source paragraph
	# XXX Eliminate call to grep?
	# XXX this does not work for https://foo.git URLs.
	REPO=$(perl -MDebian::Copyright -E '$c = Debian::Copyright->new(); $c->read("debian/copyright"); say $c->header()->Source' | grep -oE "git(?:+ssh)?:.+$" || true)

	# case 2: Repository in d/upstream
	if [ -z "$REPO" -a ! -e debian/upstream/metadata ] ; then
		warn "No 'debian/upstream/metadata' file found, trying to create it ..."
		dpt debian-upstream || true
		if [ -e debian/upstream/metadata -a "$NOGIT" -eq "0" ] ; then
			git add debian/upstream/metadata
			git commit -m "Add debian/upstream/metadata"
			dch --no-auto-nmu --mainttrailer --release-heuristic=changelog "Add debian/upstream/metadata"
			git add debian/changelog
			git commit -m "Update debian/changelog" -m "Gbp-Dch: Ignore"
		fi
	fi
	if [ -z "$REPO" -a -e debian/upstream/metadata ] ; then
		# XXX use something better than awk to parse this YAML file?
		URL=$(awk '/^Repository:\s*.+/ {print $2;}' debian/upstream/metadata || true)
		if GIT_ASKPASS=/bin/true git ls-remote ${URL} >/dev/null 2>&1; then
			REPO=${URL}
		fi
	fi

	if [ -n "$REPO" ] ; then
		warn "Adding Git remote 'upstream-repo' with URL '$REPO' ..."
		git remote add upstream-repo "$REPO"
		git fetch upstream-repo
	else
		warn "No upstream Git URL found in 'debian/copyright' or 'debian/upstream/metadata'. Run 'git remote add upstream-repo <URL>' manually to track upstream."
	fi
else
	warn "Found Git remote 'upstream-repo':"
	git remote --verbose show upstream-repo
	git fetch upstream-repo
fi

POD=<<'EOF'
=head1 NAME

dpt-upstream-repo - add upstream Git repository as git remote upstream-repo

=head1 SYNOPSIS

B<dpt upstream-repo> [-g]

=head1 DESCRIPTION

B<dpt upstream-repo> adds the upstream Git repository as a B<git remote>
named I<upstream-repo>. The URL for the repository is read from either
F<debian/copyright>'s I<Source> field or from F<debian/upstream/metadata>'s
I<Repository> field.

In case there's no Git URL in F<debian/copyright> and F<debian/upstream/metadata>
doesn't exist, the latter is created with L<dpt-debian-upstream(1)>.

=head1 OPTIONS

=over

=item B<-g>

Don't run C<git add debian/upstream/metadata; git commit> after creating
F<debian/upstream/metadata> via B<dpt debian-upstream>.

=back

=head1 SEE ALSO

L<dpt-debian-upstream(1)>

=head1 COPYRIGHT & LICENSE

=over

=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
