#!/bin/sh

# copyright, licensing and documentation at the end

set -e
set -u

# functions

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

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

repofromcopyright() {
	# XXX Eliminate call to grep?
	# XXX this does not work for https://foo.git URLs.
	REPOURL=$(perl -MDebian::Copyright -E '$c = Debian::Copyright->new(); $c->read("debian/copyright"); say $c->header()->Source' | grep -oE "git(?:+ssh)?:.+$" || true)
}

repofrommetadata() {
	# XXX use something better than awk to parse this YAML file?
	REPOURL=$(awk '/^Repository:\s*.+/ {print $2;}' debian/upstream/metadata || true)
}

checkrepourl() {
	if ! GIT_ASKPASS=/bin/true git ls-remote "$REPOURL" >/dev/null 2>&1; then
		REPOURL=
	fi
}

# default values

CREATE=1
UPDATE=0
REPOURL=

# command line arguments
# -g don't create debian/upstream/metadata
# -u update url of remote "upstream-repo"
#    to be used from .mrconfig
while getopts gu OPTS; do
	case "$OPTS" in
		g)
			CREATE=0
			;;
		u)
			UPDATE=1
			CREATE=0
			;;
		*)
			;;
	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."

# "main"
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
	repofromcopyright

	# case 2: Repository in d/upstream/metadata
	if [ -z "$REPOURL" -a ! -e debian/upstream/metadata -a "$CREATE" -eq "1" ] ; then
		warn "No 'debian/upstream/metadata' file found, trying to create it ..."
		dpt debian-upstream || true
		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
	if [ -z "$REPOURL" -a -e debian/upstream/metadata ] ; then
		repofrommetadata
	fi

	if [ -n "$REPOURL" ] ; then
		checkrepourl
		if [ -n "$REPOURL" ] ; then
			warn "Adding Git remote 'upstream-repo' with URL '$REPOURL' ..."
			git remote add upstream-repo "$REPOURL"
			git fetch upstream-repo
		fi
	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 || true
	if [ "$UPDATE" -eq "1" ] ; then
		OLDREPOURL=$(git remote get-url upstream-repo)
		repofromcopyright
		[ -n "$REPOURL" ] || repofrommetadata
		if [ -n "$REPOURL" -a "$OLDREPOURL" != "$REPOURL" ] ; then
			checkrepourl
			if [ -n "$REPOURL" ] ; then
				git remote set-url upstream-repo "$REPOURL"
				warn "Updating Git remote 'upstream-repo' to use URL '$REPOURL' ..."
				git remote --verbose show upstream-repo
			fi
		fi
	fi
	git fetch --prune upstream-repo || true
fi

POD=<<'EOF'
=head1 NAME

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

=head1 SYNOPSIS

B<dpt upstream-repo> [-g|-u]

=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 create F<debian/upstream/metadata>.

Used for initial cloning, e.g. with B<mr up>: We want the upstream
repo being added if debian/upstream/metadata exists.

The letter B<g> is used for historical reasons because initially this
option only suppressed the automatic committing to the B<g>it
repository.

=item B<-u>

Update the URL of the I<upstream-repo> B<git remote>.
The value is read from the same places as for the initial creation.

B<-u> implies B<-g>, i.e. we also don't create F<debian/upstream/metadata>
on updates.

=back

=head1 SEE ALSO

L<dpt-debian-upstream(1)>

=head1 COPYRIGHT & LICENSE

=over

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

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

=item Copyright 2015 Axel Beckert L<abe@debian.org>

=item Copyright 2016 Alex Muntada L<alexm@alexm.org>

=back

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

=cut
EOF
