summaryrefslogtreecommitdiffstats
path: root/bin/templ/perl-pkg
blob: 3886a7135f427861e35ee79633471bbc82c859f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env perl

use warnings 'FATAL' => 'all';
use strict;

use Text::Wrap qw(wrap);

my %ACTIONS_OF = ('MM',
                  { 'build'   => [ q{/usr/bin/perl Makefile.PL}, q{make} ],
                    'check'   => [ q{make test} ],
                    'package' => [ q{make DESTDIR="$pkgdir" install} ]
                   },
                  'MB',
                  { 'build'   => [ q{/usr/bin/perl Build.PL}, q{./Build} ],
                    'check'   => [ q{./Build test} ],
                    'package' => [ q{./Build install} ]
                   } );

my $PBBEG = <<'END_BEG';
# Maintainer : %s
# Generator  : pbjam %s
END_BEG

my %FUNCFMTS;
$FUNCFMTS{'build'} = <<'END_FMT';
build() {
  ( export PERL_MM_USE_DEFAULT=1 PERL5LIB=""                 \
      PERL_AUTOINSTALL=--skipdeps                            \
      PERL_MM_OPT="INSTALLDIRS=vendor DESTDIR='$pkgdir'"     \
      PERL_MB_OPT="--installdirs vendor --destdir '$pkgdir'" \
      MODULEBUILDRC=/dev/null

    cd "$_distdir"
%s
  )
}
END_FMT

$FUNCFMTS{'check'} = <<'END_FMT';
check() {
  ( export PERL_MM_USE_DEFAULT=1 PERL5LIB=""
    cd "$_distdir"
%s
  )
}
END_FMT

$FUNCFMTS{'package'} = <<'END_FMT';
package() {
    cd "$_distdir"
%s
    find "$pkgdir" -name .packlist -o -name perllocal.pod -delete
}
END_FMT

my $PBEND = <<'END_END';
# Local Variables:
# mode: shell-script
# sh-basic-offset: 2
# End:
# vim:set ts=2 sw=2 et:
END_END

# Convert actions array into lines of bash to insert into template.
sub bashify
{
    my (@lines) = @_;
    my $txt = join qq{\n}, map { s/^/    /gm; $_ } @lines;
    return $txt
}

sub printpb
{
    my ($btype, $pbvars) = @_;
    my $acts = $ACTIONS_OF{$btype}
        or die "$0: unknown build type ($btype)\n";

    my $pkger = $pbvars->{'packager'}[0] || 'Anonymous';
    printf $PBBEG, $pkger, $pbvars->{'pbjver'}[0];
    print "\n";

    # Replace version string in first 'source' entry with $pkgver parameter.
    my $ver = $pbvars->{'pkgver'}[0];
    s/\Q$ver\E/\${pkgver}/g for $pbvars->{'source'}[0];

    # 'pbfields' will recognize and PKGBUILD fields in the data
    # and print them out in order, wordwrapping arrays.
    open my $pbfields, '|pbfields' or die "failed to pipe to pbfields: $!";
    while (my ($name, $vals) = each %$pbvars) {
        print $pbfields $name, "\n";
        print $pbfields $_, "\n" for @$vals;
        print $pbfields "\n";
    }
    close $pbfields;

    # Replace version string in _distdir with $pkgver parameter.
    my $distdir = $pbvars->{'distdir'}[0];
    $distdir =~ s/\Q$ver\E/\${pkgver}/;
    print qq{_distdir="\${srcdir}/$distdir"\n};
    print "\n";

    for my $func (qw/build check package/) {
        my $funclines = $acts->{$func};
        printf $FUNCFMTS{$func}, bashify(@$funclines);
        print "\n";
    }
    print $PBEND;
}

sub readvars
{
    local $/ = ""; # split records on empty lines
    my (%pbvars);
    while (<STDIN>) {
        my ($name, @vals) = split /\n/;
        $pbvars{$name} = [ @vals ];
    }
    return \%pbvars;
}

sub main
{
    my ($type) = @_;
    my $vars = readvars();
    printpb($type, $vars);
}

my $type = shift or die qq{$0: please provide "MM" or "MB" as argument\n};
main($type);