package Greenstone::Package::_apt;

use strict;
use warnings;
use utf8;
use parent 'Greenstone::Package';

# Deb packages expect lists to be comma separated
sub replacement_array {
    my $self = shift;
    my $array = shift;
    return join ", ", @{$array};
}

sub write_function {
    my ($self, $name, @lines) = @_;
    my $file = "$self->{output}/debian/$name";
    print "    - $file\n";
    open my $OUT, '>', $file
        or die "Failed to open $file: $!";
    for my $line (@lines) {
        print $OUT $line, "\n";
    }
    close $OUT;
    chmod 0755, $file;
}

# Add pre/post install/remove hooks
sub add_install {
    my ($self, $name) = @_;
    my $prefix = (defined $name ? "$name." : "");
    exists $self->{config}->{PRE_INSTALL} and
        $self->write_function ("${prefix}preinst", @{$self->{config}->{PRE_INSTALL}});
    exists $self->{config}->{POST_INSTALL} and
        $self->write_function ("${prefix}postinst", @{$self->{config}->{POST_INSTALL}});
    exists $self->{config}->{PRE_REMOVE} and
        $self->write_function ("${prefix}prerm", @{$self->{config}->{PRE_REMOVE}});
    exists $self->{config}->{POST_REMOVE} and
        $self->write_function ("${prefix}postrm", @{$self->{config}->{POST_REMOVE}});
}

# Adds a definition for a package or subpackage
sub add_package_section {
    my ($self, $out) = @_;
    for my $line (
        'Package: %NAME%',
        'Architecture: %ARCHITECTURE%',
        'Homepage: %HOMEPAGE%',
        'Depends: %DEPENDS%',
        'Description: %DESCRIPTION_SHORT%',
        '  %DESCRIPTION%',
        ''
    ) {
        my $copy = $line;
        $self->subst ($copy);
        print $out $copy, "\n";
    }
}

# Adds a definition for files included in a subpackage
sub add_package_files {
    my $self = shift;
    my $file = "$self->{output}/debian/$self->{config}->{NAME}.install";
    print "    - $file\n";
    open my $INSTALL, '>', $file;
    for my $line (@{$self->{config}->{FILES}}) {
        print $INSTALL $line, "\n";
    }
    close $INSTALL;
}

# Handles the apt-specific package generation
sub add_package_impl {
    my ($self, $packages) = @_;
    my $file = "$self->{output}/debian/control";
    print "    - $file\n";
    open my $CONTROL, '>', $file
        or die "Failed to open $file: $!";
    for my $line (
        'Source: %NAME%',
        'Section: java',
        'Priority: optional',
        'Maintainer: %MAINTAINER_NAME% <%MAINTAINER_EMAIL%>',
        'Build-Depends: %MAKEDEPENDS%',
        ''
    ) {
        my $copy = $line;
        $self->subst ($copy);
        print $CONTROL $copy, "\n";
    }
    if (defined $packages) {
        my @names;
        # add main package
        push @names, $self->{config}->{NAME};
        $self->add_install ($self->{config}->{NAME});
        $self->add_package_section ($CONTROL);
        $self->add_package_files;

        my $config = $self->{config};
        # add subpackages
        for my $package (@{$packages}) {
            push @names, $package->{NAME};
            $package->{HOMEPAGE} = $config->{HOMEPAGE};
            $self->{config} = $package;
            $self->add_install ($package->{NAME});
            $self->add_package_section ($CONTROL);
            $self->add_package_files;
        }
        $self->{config} = $config;
        my $rules = "$self->{output}/debian/rules";
        open my $RULES, '>>', $rules
            or die "Failed to open $rules: $!";
        print $RULES "\noverride_dh_gencontrol:\n";
        for my $name (@names) {
            print $RULES "\tdh_gencontrol -p$name\n";
        }
        close $RULES;
    } else {
        $self->add_install;
        $self->add_package_section ($CONTROL);
    }
    close $CONTROL;
}

1;
