package Greenstone::Package::_pacman;

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

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

sub add_md5 {
    my $self = shift;
    my $md5 = Digest::MD5->new;
    for my $source (@_) {
        open my $MD5FH, '<', "$self->{output}/$source";
        binmode $MD5FH;
        $md5->addfile ($MD5FH);
        push @{$self->{config}->{SOURCE_SUMS}}, $md5->hexdigest;
        close $MD5FH
    }
}

sub add_sources {
    my $self = shift;
    $self->SUPER::add_sources;
    $self->{config}->{SOURCE_SUMS} = [];
    $self->add_md5 (@{$self->{config}->{SOURCES}});
}

sub add_makefile {
    my $self = shift;
    $self->SUPER::add_makefile;
    $self->add_md5 ('Makefile');
}

sub write_function {
    my ($self, $name, $out, @lines) = @_;
    print $out $name, "() {\n";
    for my $line (@lines) {
        $self->subst ($line);
        print $out "\t", $line, "\n";
    }
    print $out "}\n\n";
}

sub add_install {
    my $self = shift;
    if (exists $self->{config}->{PRE_INSTALL} or
        exists $self->{config}->{POST_INSTALL} or
        exists $self->{config}->{PRE_REMOVE} or
        exists $self->{config}->{POST_REMOVE}) {
        my $name = "$self->{config}->{NAME}.install";
        open my $INSTALL, '>', "/tmp/gspkg.$name";
        exists $self->{config}->{PRE_INSTALL} and do {
            $self->write_function ("pre_install", $INSTALL, @{$self->{config}->{PRE_INSTALL}});
            $self->write_function ("pre_upgrade", $INSTALL, @{$self->{config}->{PRE_INSTALL}});
        };
        exists $self->{config}->{POST_INSTALL} and do {
            $self->write_function ("post_install", $INSTALL, @{$self->{config}->{POST_INSTALL}});
            $self->write_function ("post_upgrade", $INSTALL, @{$self->{config}->{POST_INSTALL}});
        };
        exists $self->{config}->{PRE_REMOVE} and
            $self->write_function ("pre_remove", $INSTALL, @{$self->{config}->{PRE_REMOVE}});
        exists $self->{config}->{POST_REMOVE} and
            $self->write_function ("post_remove", $INSTALL, @{$self->{config}->{POST_REMOVE}});
        close $INSTALL;
        $self->add ("/tmp/gspkg.$name", "$self->{output}/$name");
        return "install=$name";
    } else {
        return "";
    }
}

sub add_package_impl {
    my ($self, $packages) = @_;
    if (defined $packages) {
        $self->{config}->{NAME} = '';
        for my $package (@{$packages}) {
            $self->{config}->{NAME} .= " $package->{NAME}";
        }
    }
    open my $PKGBUILD, '>', "/tmp/gspkg.PKGBUILD";
    for my $line (
        '# Maintainer: %MAINTAINER_NAME% <%MAINTAINER_EMAIL%>',
        'pkgname=(%NAME%)',
        'pkgver=%VERSION%',
        'pkgrel=%RELEASE%',
        'url=%HOMEPAGE%',
        'license=%LICENSE_SHORT%',
        'arch=(any)',
        'makedepends=(',
        '%MAKEDEPENDS%',
        ')',
        'source=(',
        '%SOURCES%',
        ')',
        'md5sums=(',
        '%SOURCE_SUMS%',
        ')',
        'build() {',
        '  cd ${srcdir}',
        '  make build',
        '}'
    ) {
        my $copy = $line;
        $self->subst ($copy);
        print $PKGBUILD $copy, "\n";
    }
    if (defined $packages) {
        my $config = $self->{config};
        for my $package (@{$packages}) {
            $self->{config} = $package;
            $self->write_function ("package_$package->{NAME}", $PKGBUILD,
                'pkgdesc=%DESCRIPTION%',
                'arch=(\'%ARCHITECTURE%\')',
                'depends=(',
                '%DEPENDS%',
                ')',
                'optdepends=(',
                '%OPTDEPENDS%',
                ')',
                'provides=(',
                '%PROVIDES%',
                ')',
                $self->add_install,
                'DESTDIR=${pkgdir} make install_%NAME%',
            );
        }
        $self->{config} = $config;
    } else {
        print $PKGBUILD $self->add_install, "\n";
        $self->write_function ('package', $PKGBUILD, 'DESTDIR=${pkgdir} make install');
    }
    close $PKGBUILD;
    $self->add ("/tmp/gspkg.PKGBUILD", "$self->{output}/PKGBUILD");
}

1;
