package Greenstone::Package::_rpm;

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

sub replacement_array {
    my $self = shift;
    my $array = shift;
    return join ", ", @{$array};
}

sub replacement {
    my ($self, $var) = @_;
    if ($var eq 'FILES') {
        return join "\n", @{$self->{config}->{$var}};
    }
    return $self->SUPER::replacement ($var);
}

sub write_function {
    my ($name, @lines) = @_;
    my $ret = "\n";
    $ret .= "%" . $name . "\n";
    for my $line (@lines) {
        $ret .= $line . "\n";
    }
    return $ret;
}

sub add_install {
    my ($self, $package) = shift;
    my $suffix = (defined $package ? " $package" : "");
    my $ret = '';
    exists $self->{config}->{PRE_INSTALL} and
        $ret .= write_function "pre$suffix", @{$self->{config}->{PRE_INSTALL}};
    exists $self->{config}->{POST_INSTALL} and
        $ret .= write_function "post$suffix", @{$self->{config}->{POST_INSTALL}};
    exists $self->{config}->{PRE_REMOVE} and
        $ret .= write_function "preun$suffix", @{$self->{config}->{PRE_INSTALL}};
    exists $self->{config}->{POST_REMOVE} and
        $ret .= write_function "postun$suffix", @{$self->{config}->{POST_INSTALL}};
    return $ret;
}

sub add_package_impl {
    my ($self, $packages) = @_;
    my $file = "$self->{output}/$self->{config}->{NAME}.spec";
    print "    - $file\n";
    open my $SPEC, '>', $file;
    {
        my @optional;
        scalar(@{$self->{config}->{DEPENDS}}) gt 0 and
            push @optional, 'Requires:      %DEPENDS%';
        # Cannot set architecture for subpackages because RPM's subpackages
        #  implementation is broken.
        # Greenstone core gets split into three packages:
        #  - The main web servlets (java classes)       - noarch
        #  - The native libraries used by the servlets  - native
        #  - The tools for building collections         - native
        # As RPM forces subpackages to inherit their name from a root package,
        #  the main web servlets must be set as the root package
        #  (in order for everything to get the right name).
        # This means that the root package should be noarch, and the subpackages
        #  must be native.
        # But this is impossible with RPM (subpackages must be either the same
        #  architecture as the root package, or noarch)
        # To avoid this until someone fixes RPM into an actual working package
        #  manager, we will just make the core greenstone package pretend to
        #  be native.
        unless (defined $packages) {
            $self->{config}->{ARCHITECTURE} eq $self->{config}->{ARCH_ANY} and
                push @optional, 'BuildArch:     %ARCHITECTURE%';
        }
        for my $line (
            'Name:          %NAME%',
            'Version:       %VERSION%',
            'Release:       %RELEASE%',
            'License:       %LICENSE_SHORT%',
            'URL:           %HOMEPAGE%',
            'Source0:       %NAME%',
            'AutoReqProv:   no',
            'BuildRequires: %MAKEDEPENDS%',
            @optional,
            'Summary:       %DESCRIPTION_SHORT%',
            '',
            '%description',
            '%DESCRIPTION%',
            '',
            '%prep',
            'cp -pr %SOURCE0 .',
            '',
            '%build',
            'cd %NAME%',
            'make %{?_smp_mflags}',
            '',
            '%install',
            'cd %NAME%',
            '%make_install',
            $self->add_install,
            '%files',
            '%FILES%',
            '',
            ''
        ) {
            my $copy = $line;
            $self->subst ($copy);
            print $SPEC $copy, "\n";
        }
    }
    if (defined $packages) {
        my $config = $self->{config};
        for my $package (@{$packages}) {
            $self->{config} = $package;
            my @optional;
            scalar(@{$self->{config}->{DEPENDS}}) gt 0 and
                push @optional, 'Requires:      %DEPENDS%';
            for my $line (
                '%package %NAME%',
                'Summary:       %DESCRIPTION_SHORT%',
                'AutoReqProv:   no',
                @optional,
                '',
                '%description %NAME%',
                '%DESCRIPTION%',
                $self->add_install ($package->{NAME}),
                '%files %NAME%',
                '%FILES%',
                '',
                ''
            ) {
                my $copy = $line;
                $self->subst ($copy);
                print $SPEC $copy, "\n";
            }
        }
        $self->{config} = $config;
    }
    print $SPEC '%changelog', "\n";
    close $SPEC;
}

1;
