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%';
        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(@{$package->{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;
