#!/bin/sh

set -e
set -u

# documentation is at the end

DPT__SCRIPTS=scripts # REPLACED AT INSTALL TIME

help() {
    perldoc "$0"
}

usage() {
    perl -MPod::Usage=pod2usage -e"pod2usage(-input => '$0', -verbose=>99, -sections=>[qw(SYNOPSIS DESCRIPTION COMMANDS)])"
}

for f in ~/.dpt.conf ~/.config/dpt.conf; do
    if [ -r "$f" ]; then
        . "$f"
    fi
done

for v in `set | egrep ^DPT_ | sed 's/=.*//'`; do
    export $v
done

CMD="${1:-}"

if [ -z "$CMD" ]; then
    usage
    exit 1
fi

if [ "$CMD" = "--help" -o "$CMD" = "-h" ]; then
    help
    exit 0
fi

shift

if [ "$CMD" = "co" ]; then
    CMD=checkout
fi

if [ ! -x "$DPT__SCRIPTS/$CMD" ]; then
    echo "Subcommand $CMD not found. Try --help."
    exit 1
else
    exec "$DPT__SCRIPTS/$CMD" "$@"
fi

DOC=<<EOF
=encoding utf8

=head1 NAME

dpt - Debian Perl module packaging Tool

=head1 SYNOPSIS

 dpt [I<global option>...] I<command> [I<command option...>]
 dpt --help

=head1 DESCRIPTION

B<dpt> is a helper tool for maintaining Perl module packages in Debian. All of
the functions are implemented as sub-commands.

=head1 GLOBAL OPTIONS

=over

=item B<--help|-h>

Show this documentation.

=back

=head1 COMMANDS

=over

=item B<alioth-repo> - clone newly created repository to alioth.debian.org

Removed. Please use "dpt salsa pushrepo" as a successor.
See L<dpt-salsa(1)>.

=item B<cd> - change directory to a package working directory

See L<dpt-cd(1)>

=item B<ci-failures> - query ci.debian.net for autopkgtest failures

See L<dpt-ci-failures(1)>.

=item B<clean-mr-repos> - remove local repos which are gone from salsa.debian.org

See L<dpt-clean-mr-repos(1)>.

=item B<checkout> - work on pkg-perl package

=item (also available as B<co>)

See L<dpt-checkout(1)>.

=item B<dch-note> - add notes/TODO items to F<debian/changelog>

See L<dpt-dch-note(1)>.

=item B<debian-upstream> - create debian/upstream/metadata file from META.{json,yml}

See L<dpt-debian-upstream(1)>.

=item B<forward> - forward a bug or a patch upstream

See L<dpt-forward(1)>.

=item B<gc> - swipe pkg-perl working directories

See L<dpt-gc(1)>.

=item B<gen-itp> - aid for creating ITP bug reports

See L<dpt-gen-itp(1)>.

=item B<get-ubuntu-packages> - list Ubuntu packages maintained by the group

See L<dpt-get-ubuntu-packages(1)>.

=item B<import-orig> - "gbp import-orig" wrapper with upstream tracking support

See L<dpt-import-orig(1)>.

=item B<invite-github> - Invite someone to GitHub team

See L<dpt-invite-github(1)>.

=item B<lp-mass-subscribe> - subscribes the Ubuntu group to a list of packages

See L<dpt-lp-mass-subscribe(1)>.

=item B<missing-pristine-tar> - remedy missing pristine-tar information

See L<dpt-missing-pristine-tar(1)>.

=item B<missing-upload> - remedy upload done outside of Git

See L<dpt-missing-load(1)>.

=item B<missing-upstream> - remedy missing upstream branch/tags

See L<dpt-missing-upstream(1)>.

=item B<new-upstream> - list packages with newer upstream versions

See L<dpt-new-upstream(1)>.

=item B<packagecheck> - various package consistency checks

See L<dpt-packagecheck(1)>.

=item B<push> - push relevant refs to C<origin> remote

See L<dpt-push(1)>.

=item B<rename-uploader> - mass-change of uploaded name/email

See L<dpt-rename-uploader(1)>.

=item B<repack.sh> - repackaging helper

See L<dpt-repack.sh(1)>.

=item B<salsa> - manage repositories and members on salsa.debian.org

See L<dpt-salsa(1)>.

=item B<shell-lib> - shell integration

See L<dpt-shell-lib(1)>

=item B<takeover> - take over package maintenance

See L<dpt-takeover(1)>.

=item B<upstream-repo> - add upstream Git repository as git remote upstream-repo

See L<dpt-upstream-repo(1)>.

=back

For each command, see its manual page for more details.

=head1 FILES

F<.dpt.conf> and F<.config/dpt.conf> are sourced (in this order) and all
variables starting with C<DPT_> are exported to the environment before the
command script is run.

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2013 Damyan Ivanov <dmn@debian.org>

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut
EOF
