#!/usr/bin/perl -w

use strict;
use warnings;
use utf8;

=head1 NAME

extrepo - manage external Debian repositories

=head1 SYNOPSIS

extrepo search I<search_key>

extrepo enable I<repository_name>

extrepo disable I<repository_name>

extrepo update I<repository_name>

=head1 DESCRIPTION

The extrepo tool is used to manage external repositories in Debian.
Before extrepo, users who wished to use software not packaged for Debian
had to manually write the apt configuration files, run an unsigned
script as root, or install an unsigned .deb package that contained all
the configuration on their system. None of these methods were very
secure.

Extrepo remedies this by way of a metadata repository for external
package repositories. The user can search the list of metadata
repositories by way of C<extrepo search>, and manage them through
C<extrepo enable>, C<extrepo disable>, or C<extrepo update>.

=head1 SUBCOMMANDS

=head2 search I<key>

Searches for a repository where the given argument I<key> (a regular
expression) matches either the name of the repository, its description,
or the URL of the repository in question. The full YAML configuration of
all repositories that have a match are printed to standard output.

To search for all repositories, don't provide any search key.

=head2 enable I<repository_name>

Enable the repository named I<repository_name>. 

There are two cases for this command:

=over

=item *

If the repository had not yet been created before, this creates its
configuration from the current metadata.

=item *

If the repository had been created before but disabled by way of the
C<extrepo disable> command, re-enables it I<without> updating the
metadata. If you want to update it, see the C<extrepo update> command.

=back

=head2 disable I<repository_name>

This simply adds a line "Enabled: no" to the apt configuration file, so
that the repository is not enabled. It can be re-enabled by changing the
line to "Enabled: yes" (or removing it entirely), or by way of the
C<extrepo enable> command (second case).

=head2 update I<repository_name>

Re-writes the apt configuration file for this repository, as well as the
GPG keyring for it, from the current metadata.

=head1 SEE ALSO

L<https://salsa.debian.org/extrepo-team/extrepo-data> for instructions
on adding your own repository

=head1 AUTHOR

Wouter Verhelst

=cut

use Debian::ExtRepo::Commands::Search;
use Debian::ExtRepo::Commands::Update;
use Debian::ExtRepo::Commands::Disable;
use Debian::ExtRepo::Commands::Enable;

my $appname = $0;

sub usage {
	print "Usage:\n";
	print "$appname search\tsearch for repositories\n";
	print "$appname update\tupdate a repository to the latest metadata\n";
	print "$appname disable\tdisable an extrepo-configured repository\n";
	print "$appname enable\t(re-)enable an extrepo repository\n";
	print "\n";
	print "For more info, please read extrepo(1)\n";
}

my $cmdlower = shift;
my $arg = shift;

if(!defined $cmdlower) {
	usage();
	die "Need command, can't continue\n";
}

usage() if($cmdlower) eq "--help";

$arg = "" unless defined($arg);

my $command = ucfirst($cmdlower);

eval "Debian::ExtRepo::Commands::${command}::run(\$arg);";
if($@) {
	die $!;
}
