#!/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 upgrade/ ;
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", "upgrade=s")
    or die("Error in command line arguments\n");

my $config = dh_make_raku_config;

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

if ($arg->{upgrade}) {
    upgrade($config, $arg->{upgrade});
}

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

if ($init) {
    commit();
}

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

set_remote( $config->{debian_remote} => $git_url);

__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

  # get new release from upstream
  dh-make-raku --upgrade <new_upstream_version>

=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 installing
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.

=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

=back

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

=head1 Create package files

=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

=head1 Update a package

Run

  dh-make-raku --upgrade <new_upstream_version>

This command update the C<upstream> branch and merge it in
C<debian/sid> branch and then update debian files.

=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 *

I could not find a way to extract a description from markdown
documentation. So you'll have to manually edit this field in
C<debian/control> or with C<cme edit dpkg-control>.

=item *

The content of the generated C<debian/gbp.conf> may have the wrong
version tag. Edit this file manually if C<gbp build-package> complains
about missing tarball.

=item *

The generated C<debian/copyright> file is somewhat too verbose. This
file is generated by
L<cme|https://manpages.debian.org/unstable/cme/cme.1p.en.html>. I'm
working on improving this.
See L<cme wiki on dpkg-copyright|https://github.com/dod38fr/config-model/wiki/Updating-debian-copyright-file-with-cme>
if you want to work around generation issues.

=back

=head1 AUTHOR

=over

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

=back


=cut

