#!/bin/sh

# copyright, licensing and documentation at the end

set -e
set -u

. "${DPT__SCRIPTS:-/usr/share/pkg-perl-tools}/lib/dpt-lib.sh"

# functions

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))

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

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

	if [ -n "$REPOURL" ] ; then
		checkrepourl
		if [ -n "$REPOURL" ] ; then
			info "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/upstream/metadata'. Run 'git remote add upstream-repo <URL>' manually to track upstream."
	fi
else
	info "Found Git remote 'upstream-repo':"
	git remote --verbose show upstream-repo || true
	if [ "$UPDATE" -eq "1" ] ; then
		OLDREPOURL=$(git remote get-url upstream-repo)
		repofrommetadata
		if [ -n "$REPOURL" -a "$OLDREPOURL" != "$REPOURL" ] ; then
			checkrepourl
			if [ -n "$REPOURL" ] ; then
				git remote set-url upstream-repo "$REPOURL"
				info "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
F<debian/upstream/metadata>'s I<Repository> field.

In case F<debian/upstream/metadata>
doesn't exist, it 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-2022 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
