#!/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 | awk '/^Source: / { print $2 }'`

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

[ -n "$DIST" ]

ssh git.debian.org /git/pkg-perl/setup-repository $PKG \"Debian packaging of $DIST CPAN distribution\"

git remote add origin ssh://git.debian.org/git/pkg-perl/packages/$PKG.git || [ $? = 128 ]
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
