#!/usr/bin/perl

# PODNAME: ooi
# ABSTRACT: Commandline tool to install OTRS addons

use v5.10;

use strict;
use warnings;

use OTRS::OPM::Installer;
use Getopt::Long;

GetOptions(
    'p=s'     => \my $package,
    'h'       => \my $help,
    'r=s'     => \my @repositories,
    'v=s'     => \my $version,
    'vv'      => \my $verbose,
    'version' => \my $print_version,
    'list'    => \my $list_available,
    'sudo'    => \my $sudo,
    'force'   => \my $force,
);

unshift @ARGV, $package if $package;

if ( $print_version ) {
    print OTRS::OPM::Installer->VERSION();
    exit;
}

if ( $list_available ) {
    my $installer = OTRS::OPM::Installer->new;
    for my $addon ( $installer->list_available ) {
        say sprintf "%s (%s) from\n    %s\n",
            $addon->{name}, $addon->{version}, $addon->{url};
    }
    exit;
}

if( $help || !@ARGV ) {
    print_usage();
    exit;
}

my %opts;
$opts{repositories} = \@repositories if @repositories;
$opts{version}      = $version       if $version;

my %class_opts;
$class_opts{verbose} = 1             if $verbose;
$class_opts{sudo}    = 1             if $sudo;
$class_opts{force}   = 1             if $force;

my $installer = OTRS::OPM::Installer->new( %class_opts );

for my $package_info ( @ARGV ) {
    my ($package, $version) = split /=/, $package_info;
    $opts{version}          = $version if $version;

    $installer->install(
        package => $package,
        %opts,
    );
}

sub print_usage {
    print qq~$0 [-h] [-r <repository>] [-v <version>] [vv] <package> [<package>...]
    
    version:    exact version of otrs package to be installed
    repository: repository for OTRSM packages
    help:       show this screen
    vv:         be verbose
~;
}

__END__

=pod

=encoding UTF-8

=head1 NAME

ooi - Commandline tool to install OTRS addons

=head1 VERSION

version 0.04

=head1 AUTHOR

Renee Baecker <reneeb@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2018 by Renee Baecker.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut
