#!/usr/bin/env perl

use 5.036;
use strict;
use warnings;
use utf8;

use lib '/usr/local/lib/perl5';  # To be used together with the cpanm command given in the README.md file.

use Encode;
use English;
use Getopt::Long qw(VersionMessage :config no_ignore_case bundling);
use Music::Harmonica::TabsCreator qw(tune_to_tab_rendered transpose_tab_rendered list_tunings);
use Pod::Usage;

our $VERSION = $Music::Harmonica::TabsCreator::VERSION; ## no critic (ProhibitComplexVersion,RequireConstantVersion)

BEGIN {
  my $has_locale = eval { require Encode::Locale; Encode::Locale->import(); 1 };
  if ($has_locale) {
    Encode::Locale::decode_argv(Encode::FB_WARN);
  }
  if (-t STDIN && $has_locale) {
    binmode STDIN, ':encoding(console_in)';  # console_in is set by the Encode::Locale module.
  } else {
    binmode STDIN, ':encoding(UTF-8)';
  }
  if (-t STDOUT && $has_locale) {
    binmode STDOUT, ':encoding(console_out)';  # console_out is set by the Encode::Locale module.
  } else {
    binmode STDOUT, ':encoding(UTF-8)';
  }
  if (-t STDERR && $has_locale) {
    binmode STDERR, ':encoding(console_out)';  # console_out is set by the Encode::Locale module.
  } else {
    binmode STDOUT, ':encoding(UTF-8)';
  }
}

my $transpose;
my $max_bends = 0;
my $list_tunings = 0;
my $preferred_key = 'C';

# TODO: add a command line option to specify the tuning filtering.

GetOptions(
  'help|h' => sub { pod2usage(-verbose => 1, -exitval => 0) },
  'helpfull|help-full|man' => sub { pod2usage(-verbose => 2, -exitval => 0) },
  'version|v' => sub { VersionMessage(-exitval => 0) },
  'transpose|transpose-from|t=s' => \$transpose,
  'max-bends|b=i' => \$max_bends,
  'list-tunings|l' => sub { $list_tunings = 1 },
  'preferred-key|k=s' => \$preferred_key,
) or pod2usage(-verbose => 0, -exitval => 2);

if ($list_tunings) {
  print "Supported tunings (id: description (tags)):\n";
  for my $t (list_tunings()) {
    printf "- %s: %s tuning (%s)\n", $t->{id}, $t->{name}, join(', ', @{$t->{tags}});
  }
  exit 0;
}

my $input;
{
  local $INPUT_RECORD_SEPARATOR = undef;
  $input = <>;
}

my %options = (
  max_bends => $max_bends,
  preferred_key => $preferred_key,
);

if (defined $transpose) {
  $transpose =~ m/^(.*?)(:?:.*)?$/
      or die "Invalid argument for the --transpose option: ${transpose}\n";
  my ($tuning, $key) = ($1, $2 // 'C');
  say transpose_tab_rendered($input, $tuning, %options);
} else {
  say tune_to_tab_rendered($input, %options);
}

exit 0;

__END__

=pod

=encoding utf8

=head1 NAME

harmonica-tabs-creator – Convert tunes into harmonica tabs.

=head1 SYNOPSIS

  harmonica-tabs-creator < input-tune.txt

  harmonica-tabs-creator --transpose-from=richter:C < input-tab.txt

=head1 DESCRIPTION

Simple program to generate harmonica tabs from a textual representation of a
tune, using the L<Music::Harmonica::TabsCreator> library.

Currently C<harmonica-tabs-creator> can only read a single input from its
standard input and will write its output on the standard output.

See the
L<GitHub page|https://github.com/mkende/harmonica_tabs_creator> for details on
the input format.

=head1 OPTIONS

=over

=item --transpose-from=RICHTER:KEY, -t

Transpose from an input tab, rather than from sheet music. The tab syntax must
match the syntax used in the output of this program. The arguments to this
option are the ID of an harmonica tuning and the key of the input tab. The list
of supported IDs can be found with the C<--list-tunings> option. If the key is
omitted, the key of C is assumed for the input.

=item --max-bends=NUMBER, -b

Maximum number of half-step of bending allowed to use in the generated tabs.
Default to 0.

=item --preferred-key=KEY, -k

Specify what is the preferred key to use in the output. This option is only used
for Chromatic harmonicas, to avoid too many duplicated tabs. If no tabs can be
generated with the preferred key, then all 12 keys will be tried. Default to C.

=item --list-tunings, -l

List the supported harmonica tunings.

=back

=head1 AUTHOR

This program has been written by L<Mathias Kende|mailto:mathias@cpan.org>.

=head1 LICENCE

Copyright 2025 Mathias Kende

This program is distributed under the MIT (X11) License:
L<http://www.opensource.org/licenses/mit-license.php>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

=head1 SEE ALSO

=over

=item L<Music::Harmonica::TabsCreator>

=back

=cut
