#!/bin/sh

# documentation, copyright and license at the end

set -e -u

# fall back if Module::Inspector fails
detect_dist() {
	DIR=.
	local PERLNAME
	PERLNAME=''
	if [ -s $DIR/Build.PL ]; then
		PERLNAME=$(perl -n -e "print if s;^.*module_name.*=>.*['\"[]([a-zA-Z0-9:_-]+)[]'\"].*\$;\$1;" $DIR/Build.PL | sed -e 's/::/-/g' | head -n 1)
	fi
	if [ -s $DIR/Makefile.PL ]; then
		PERLNAME=$(perl -n -e "print if s;^.*(?:DIST)?NAME.*=>.*['\"[]([a-zA-Z0-9:_-]+)[]'\"].*\$;\$1;" $DIR/Makefile.PL | sed -e 's/::/-/g' | head -n 1)
	fi
	if [ -s $DIR/META.yml ]; then
		PERLNAME=$(perl -n -e "print if s;^name:.* ([a-zA-Z0-9:_-]+).*\$;\$1;" $DIR/META.yml | head -n 1)
	fi
	if [ -n "$PERLNAME" ]; then
		if curl --location --silent https://metacpan.org/release/$PERLNAME | grep '<title>.*</title>' | grep --silent $PERLNAME; then
			echo $PERLNAME
		fi
	fi
}

PKG=`dpkg-parsechangelog --show-field Source`

[ -n "$PKG" ]

# needs META.yml
set +e
DIST=`perl -MModule::Inspector -we'my $d=Module::Inspector->new(dist_dir => "."); print $d->dist_name;'`
set -e

[ -n "$DIST" ] || DIST=$(detect_dist)
set +u
if [ -z "$DIST" ] && [ -n "$1" ]; then
    set -u
    DIST="$1"
fi
set -u

if [ -z "$DIST" ]; then
   echo "E: Couldn't determine distribution name. You can pass it manually as parameter." 1>&2
   exit 1
fi

# Setup the repository on alioth if it doesn't exist yet.
if ssh git.debian.org test -e "/git/pkg-perl/packages/$PKG.git"; then
    echo "I: /git/pkg-perl/packages/$PKG.git already exists on git.debian.org, skipping setup-repository" 1>&2
else
    ssh git.debian.org /git/pkg-perl/setup-repository $PKG \"Debian packaging of $DIST CPAN distribution\"
fi

# Check if there is already a remote called "origin" before proceeding
if git remote | egrep -q '^origin$'; then
    if git remote -v | egrep "^origin[[:space:]].*git\.debian.org.*/git/pkg-perl/packages/$PKG\.git\b.*\(push\)$"; then
        echo 'I: A remote called "origin" already exists and looks like the right one.' 1>&2
        echo 'I: Skipping the addition of a remote repository named "origin".' 1>&2
    else
        echo "E: A remote called \"origin\" already exists and doesn't look right. Aborting." 1>&2
        echo 'E: You may want to rename it with "git remote rename origin origin-old".' 1>&2
        exit 1;
    fi
else
    git remote add origin ssh://git.debian.org/git/pkg-perl/packages/$PKG.git
fi

git push --all origin --set-upstream
git push --tags origin

exit 0

doc=<<'EOF'
=head1 NAME

dpt-alioth-repo -- clone newly created repository to alioth.debian.org

=head1 SYNOPSIS

B<dpt alioth-repo>

=head1 DESCRIPTION

B<dpt alioth-repo> is to be run in a newly created Git repository containing
unpacked Debian source package. It creates and configures a Git repository at
the right location on L<alioth.debian.org> and then pushes all branches and
tags.

=head1 COPYRIGHT & LICENSE

=over

=item Copyright (c) 2011 Damyan Ivanov <dmn@debian.org>

=item Copyright (c) 2012 gregor herrmann <gregoa@debian.org>

=back

This program is free software; you can redistribute it and/or modify
it under the terms of either:

=over

=item (a)

the GNU General Public License as published by the Free Software Foundation;
either version 1, or (at your option) any later version, or

=item (b)

the "Artistic License" which comes with Perl.

=back

=cut
EOF
