#!/usr/bin/perl

use strict;
use warnings;
use autodie qw(:all);

use v5.32;

use Getopt::Long;
use DhMakeRaku qw/setup_debian_files setup_git commit set_remote/ ;
use DhMakeRaku::GitLab qw/setup_project/;
use DhMakeRaku::Config qw/dh_make_raku_config/;
use DhMakeRaku::RakudoStar qw/is_star_module star_module_version add_dep_in_raku_pkg/;

use feature qw/signatures/;
no warnings qw/experimental::signatures/;

my $arg = {};
GetOptions($arg, "pkg=s", "git=s", "tag=s")
    or die("Error in command line arguments\n");

my ($pkg_dir, $git, $package, $tag, $init) = setup_git ($arg);

setup_debian_files ($pkg_dir, $git, $package, $tag, $init);

if ($init) {
    commit();
}

my $config = dh_make_raku_config;

# setup_project is idempotent
my $git_url = setup_project($config, $package);

set_remote( origin => $git_url);

if (is_star_module($package)) {
    my $version = star_module_version($package);
    add_dep_in_raku_pkg($package, $version);
}

__END__


=encoding utf8

=head1 NAME

dh-make-raku - Create debian source package from Raku module

=head1 SYNOPSIS

  # Create a new package
  dh-make-raku --git=<git_url> --pkg=<pkg-name> --tag=<upstream_release_tag>

  # refresh a package (to be run in package dir)
  dh-make-raku

=head1 REQUIREMENTS

This script requires:

=over

=item *

An account on Salsa and be part of Raku package team.
See L<How to get involved|https://wiki.debian.org/Teams/DebianRakudoGroup#How_to_get_involved>
wiki page.

=item *

Git credentials for Salsa. See L</"Git credentials"> section.

=item *

L<Quilt|https://tracker.debian.org/pkg/quilt> installed and
configured. The installation should be done when install
C<dh-make-raku>, but the configuration must be done in your home
directory. Please see
L<Using Quilt|https://wiki.debian.org/UsingQuilt> wiki page.

=item *

A specific directory layout. See L</"Directory layout"> section.

=back

=head1 DESCRIPTION

When fed a C<git> upstream URL, B<dh-make-raku> does the following tasks:

=over

=item *

Clone upstream repository in C<upstream> branch

=item *

Create the files required to build a debian source package on
C<debian/sid> branch.

=item *

Commit these files

=item *

Create a Raku module project on Debian's salsa

=item *

Setup C<origin> remote on Salsa

=item *

If the package is part of L<Rakudo Star|https://github.com/rakudo/star>,
check or update the dependency list of L<raku|https://tracker.debian.org/pkg/raku> package.
(<raku> package is expected to be in C<../../raku>).

=back

This works for most simple packages and is also useful for getting
started with packaging Raku modules.

=head1 Create package files

=head2 Directory layout

Your Raku work zone is expected to have this layout:

    .
    |-- modules
    |   |-- raku-module-1
    |   |-- raku-module-2
    |   |-- etc...
    `-- raku
        `-- debian
            `-- control

Updates of raku package dependencies are skipped if C<dh-make-raku>
cannot find C<raku> package in C<../../raku>.

=head2 With upstream release tag

To create a new package, you must have:

=over

=item *

The URL of the git repository of the new module. Only C<https> style
URL are supported.

=item *

Debian package name. Usually C<raku-something>

=item *

The upstream tag of the release to be packaged. See below if upstream
does not use tags.

=back

Then run a command like:

  dh-make-raku --git=<git_url> --pkg=<pkg-name> --tag=<upstream_release_tag>

For instance:

  dh-make-raku --git=https://git.tyil.nl/raku/Log --pkg=raku-log --tag=v0.3.1

=head2 Without upstream release tag

In this case, you must use a git reference instead of a tag. Usually
C<HEAD> is good enough. C<dh-make-raku> will get upstream version from
C<META6.json>

For instance:

  dh-make-raku --tag HEAD --git https://github.com/tadzik/File-Find.git --pkg raku-file-find

=head1 Build the package

Run:

  gbp buildpackage --git-pristine-tar --git-pristine-tar-commit --git-debian-branch=debian/sid

=head1 Options

=over

=item --git

URL of the package repository.

=item --pkg

Debian package name.

=item --tag

Upstream tag or a git reference.

=back

=head1 Git credentials

Be default, C<dh-make-raku> gets git credentials from
C<~/.git-credentials> file.

If this file is not available, Salsa credentials must be provided with
C<DRT_SALSA_USER> and C<DRT_SALSA_PRIVATE_TOKEN> environment variables.

For what it's worth, C<DRT> means "Debian Raku Team".

=head1 BUGS

=over

=item *

Dependencies are not yet managed.

=back

=head1 AUTHOR

=over

=item Dominique Dumont E<lt>dod@debian.orgE<gt>

=back


=cut

