1#!/usr/local/bin/perl
2
3use strict;
4use warnings;
5use lib qw(/usr/mports/Tools/lib);
6
7use Mport::Utils qw(recurse_ports make_var);
8
9use File::Temp qw(tempfile);
10use File::Copy qw(move copy);
11
12sub fgrep (&@);
13
14my ($wanted) = @ARGV;
15
16recurse_ports {
17  if (make_var($wanted)) {
18    print "Bumping $_[0]... ";
19    bump_portrevision(shift);
20    print "done.\n";
21  }
22};
23
24
25sub bump_portrevision {
26  my ($port) = @_;
27
28  my $makefile = "$port/Makefile";
29  my ($tmp, $tmpname) = tempfile();
30
31  if (fgrep { m/PORTREVISION/ } $makefile) {
32    open(my $in, '<', $makefile) || die "Couldn't open $makefile: $!\n";
33
34    local $_;
35    while (<$in>) {
36      if (m/^(PORTREVISION\s*\??=\s*)(\d+)/) {
37        $_ = $1 . ($2 + 1) . "\n";
38      }
39      print $tmp $_;
40    }
41    close($in) || die "Couldn't close $makefile: $!\n";
42  } else {
43    open(my $in, '<', $makefile) || die "Couldn't open $makefile: $!\n";
44
45    local $_;
46    while (<$in>) {
47      if (m/PORTVERSION=(\s*)/) {
48        $_ .= "PORTREVISION=${1}1\n";
49      }
50      print $tmp $_;
51    }
52
53    close($in) || die "Couldn't close $makefile: $!\n";
54  }
55  close($tmp) || die "Couldn't close $tmpname: $!\n";
56
57  move($tmpname, $makefile) || die "Couldn't move $tmpname to $makefile: $!\n";
58}
59
60
61sub fgrep (&@) {
62  my ($code, @files) = @_;
63  my ($fh, $ret);
64
65  foreach my $file (@files) {
66    open($fh, '<', $file) || die "Couldn't open $file: $!\n";
67    while (local $_ = <$fh>) {
68      if ($code->()) {
69        close($fh) || die "Couldn't close $file: $!\n";
70        return 1;
71      }
72    }
73    close($fh) || die "Couldn't close $file: $!\n";
74  }
75
76  return;
77}
78
79