1b39c5158Smillertpackage MakeUtil ;
2b39c5158Smillertpackage main ;
3b39c5158Smillert
4b39c5158Smillertuse strict ;
5b39c5158Smillert
6b39c5158Smillertuse Config qw(%Config);
7b39c5158Smillertuse File::Copy;
8b39c5158Smillert
9b39c5158Smillertmy $VERSION = '1.0';
10b39c5158Smillert
11b39c5158Smillert
12b39c5158SmillertBEGIN
13b39c5158Smillert{
14b39c5158Smillert    eval { require File::Spec::Functions ; File::Spec::Functions->import() } ;
15b39c5158Smillert    if ($@)
16b39c5158Smillert    {
17b39c5158Smillert        *catfile = sub { return "$_[0]/$_[1]" }
18b39c5158Smillert    }
19b39c5158Smillert}
20b39c5158Smillert
21b39c5158Smillertrequire VMS::Filespec if $^O eq 'VMS';
22b39c5158Smillert
23b39c5158Smillert
24b39c5158Smillertunless($ENV{PERL_CORE}) {
25b39c5158Smillert    $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
26b39c5158Smillert}
27b39c5158Smillert
28b39c5158Smillert$ENV{SKIP_FOR_CORE} = 1 if $ENV{PERL_CORE} || $ENV{MY_PERL_CORE} ;
29b39c5158Smillert
30b39c5158Smillert
31b39c5158Smillert
32b39c5158Smillertsub MY::libscan
33b39c5158Smillert{
34b39c5158Smillert    my $self = shift;
35b39c5158Smillert    my $path = shift;
36b39c5158Smillert
37b39c5158Smillert    return undef
38*e0a54000Safresh1        if $path =~ /^(?:RCS|CVS|SCCS|\.svn|_darcs)$/ ||
39*e0a54000Safresh1           $path =~ /(~|\.bak|_bak)$/ ||
40b39c5158Smillert           $path =~ /\..*\.sw(o|p)$/  ||
41b39c5158Smillert           $path =~ /\B\.svn\b/;
42b39c5158Smillert
43b39c5158Smillert    return $path;
44b39c5158Smillert}
45b39c5158Smillert
46b39c5158Smillertsub MY::postamble
47b39c5158Smillert{
483d61058aSafresh1    my $self = shift ;
493d61058aSafresh1    my %params = @_ ;
503d61058aSafresh1
513d61058aSafresh1    if ($ENV{PERL_CORE} )
523d61058aSafresh1    {
533d61058aSafresh1        return <<EOM;
543d61058aSafresh1
553d61058aSafresh1READMEmd:
563d61058aSafresh1
573d61058aSafresh1EOM
583d61058aSafresh1
593d61058aSafresh1    }
60b39c5158Smillert
61b39c5158Smillert    my @files = getPerlFiles('MANIFEST');
62b39c5158Smillert
63b39c5158Smillert    # Note: Once you remove all the layers of shell/makefile escaping
64b39c5158Smillert    # the regular expression below reads
65b39c5158Smillert    #
66b39c5158Smillert    #    /^\s*local\s*\(\s*\$^W\s*\)/
67b39c5158Smillert    #
68b39c5158Smillert    my $postamble = '
69b39c5158Smillert
70b39c5158SmillertMyTrebleCheck:
71b39c5158Smillert	@echo Checking for $$^W in files: '. "@files" . '
72b39c5158Smillert	perl -ne \'						\
73b39c5158Smillert	    exit 1 if /^\s*local\s*\(\s*\$$\^W\s*\)/; \'		\
74b39c5158Smillert         ' . " @files || " . '				\
75b39c5158Smillert	(echo found unexpected $$^W ; exit 1)
76b39c5158Smillert	@echo All is ok.
77b39c5158Smillert
78b39c5158Smillert';
79b39c5158Smillert
803d61058aSafresh1    if (-e '.github' && exists $params{name})
813d61058aSafresh1    {
823d61058aSafresh1        my $name = $params{name};
833d61058aSafresh1        $postamble .= <<"EOM";
843d61058aSafresh1
853d61058aSafresh1READMEmd: .github/$name.pod
863d61058aSafresh1
873d61058aSafresh1.github/$name.pod: lib/Compress/Raw/$name.pm
883d61058aSafresh1	\@echo Creating .github/$name.pod from $name.pm
893d61058aSafresh1	\$(NOECHO) perl -ne 'print unless 1 .. /^__END__/' lib/Compress/Raw/$name.pm >.github/$name.pod
903d61058aSafresh1
913d61058aSafresh1EOM
923d61058aSafresh1    }
933d61058aSafresh1    else
943d61058aSafresh1    {
953d61058aSafresh1        $postamble .= <<"EOM";
963d61058aSafresh1
973d61058aSafresh1READMEmd:
983d61058aSafresh1
993d61058aSafresh1EOM
1003d61058aSafresh1    }
1013d61058aSafresh1
102b39c5158Smillert    return $postamble;
103b39c5158Smillert}
104b39c5158Smillert
105b39c5158Smillertsub getPerlFiles
106b39c5158Smillert{
107b39c5158Smillert    my @manifests = @_ ;
108b39c5158Smillert
109b39c5158Smillert    my @files = ();
110b39c5158Smillert
111b39c5158Smillert    for my $manifest (@manifests)
112b39c5158Smillert    {
113b39c5158Smillert        my $prefix = './';
114b39c5158Smillert
115b39c5158Smillert        $prefix = $1
116b39c5158Smillert            if $manifest =~ m#^(.*/)#;
117b39c5158Smillert
118b39c5158Smillert        open M, "<$manifest"
119b39c5158Smillert            or die "Cannot open '$manifest': $!\n";
120b39c5158Smillert        while (<M>)
121b39c5158Smillert        {
122b39c5158Smillert            chomp ;
123b39c5158Smillert            next if /^\s*#/ || /^\s*$/ ;
124b39c5158Smillert
125b39c5158Smillert            s/^\s+//;
126b39c5158Smillert            s/\s+$//;
127b39c5158Smillert
128b46d8ef2Safresh1            #next if m#t/Test/More\.pm$# or m#t/Test/Builder\.pm$#;
129b46d8ef2Safresh1
130b39c5158Smillert            /^(\S+)\s*(.*)$/;
131b39c5158Smillert
132b39c5158Smillert            my ($file, $rest) = ($1, $2);
133b39c5158Smillert
134b39c5158Smillert            if ($file =~ /\.(pm|pl|t)$/ and $file !~ /MakeUtil.pm/)
135b39c5158Smillert            {
136b39c5158Smillert                push @files, "$prefix$file";
137b39c5158Smillert            }
138b39c5158Smillert            elsif ($rest =~ /perl/i)
139b39c5158Smillert            {
140b39c5158Smillert                push @files, "$prefix$file";
141b39c5158Smillert            }
142b39c5158Smillert
143b39c5158Smillert        }
144b39c5158Smillert        close M;
145b39c5158Smillert    }
146b39c5158Smillert
147b39c5158Smillert    return @files;
148b39c5158Smillert}
149b39c5158Smillert
150b39c5158Smillertsub UpDowngrade
151b39c5158Smillert{
152b39c5158Smillert    return if defined $ENV{TipTop};
153b39c5158Smillert
154b39c5158Smillert    my @files = @_ ;
155b39c5158Smillert
156b39c5158Smillert    # our and use bytes/utf8 is stable from 5.6.0 onward
157b39c5158Smillert    # warnings is stable from 5.6.1 onward
158b39c5158Smillert
159b39c5158Smillert    # Note: this code assumes that each statement it modifies is not
160b39c5158Smillert    #       split across multiple lines.
161b39c5158Smillert
162b39c5158Smillert
163b39c5158Smillert    my $warn_sub = '';
164b39c5158Smillert    my $our_sub = '' ;
165b39c5158Smillert
166b39c5158Smillert    my $upgrade ;
167b39c5158Smillert    my $downgrade ;
168b39c5158Smillert    my $do_downgrade ;
169b39c5158Smillert
170b39c5158Smillert    my $caller = (caller(1))[3] || '';
171b39c5158Smillert
172b39c5158Smillert    if ($caller =~ /downgrade/)
173b39c5158Smillert    {
174b39c5158Smillert        $downgrade = 1;
175b39c5158Smillert    }
176b39c5158Smillert    elsif ($caller =~ /upgrade/)
177b39c5158Smillert    {
178b39c5158Smillert        $upgrade = 1;
179b39c5158Smillert    }
180b39c5158Smillert    else
181b39c5158Smillert    {
182b39c5158Smillert        $do_downgrade = 1
183b39c5158Smillert            if $] < 5.006001 ;
184b39c5158Smillert    }
185b39c5158Smillert
186b39c5158Smillert#    else
187b39c5158Smillert#    {
188b39c5158Smillert#        my $opt = shift @ARGV || '' ;
189b39c5158Smillert#        $upgrade = ($opt =~ /^-upgrade/i);
190b39c5158Smillert#        $downgrade = ($opt =~ /^-downgrade/i);
191b39c5158Smillert#        push @ARGV, $opt unless $downgrade || $upgrade;
192b39c5158Smillert#    }
193b39c5158Smillert
194b39c5158Smillert
195b39c5158Smillert    if ($downgrade || $do_downgrade) {
196b39c5158Smillert        # From: use|no warnings "blah"
197b39c5158Smillert        # To:   local ($^W) = 1; # use|no warnings "blah"
198b39c5158Smillert        $warn_sub = sub {
199b39c5158Smillert                            s/^(\s*)(no\s+warnings)/${1}local (\$^W) = 0; #$2/ ;
200b39c5158Smillert                            s/^(\s*)(use\s+warnings)/${1}local (\$^W) = 1; #$2/ ;
201b39c5158Smillert                        };
202b39c5158Smillert    }
203b39c5158Smillert    #elsif ($] >= 5.006001 || $upgrade) {
204b39c5158Smillert    elsif ($upgrade) {
205b39c5158Smillert        # From: local ($^W) = 1; # use|no warnings "blah"
206b39c5158Smillert        # To:   use|no warnings "blah"
207b39c5158Smillert        $warn_sub = sub {
208b39c5158Smillert            s/^(\s*)local\s*\(\$\^W\)\s*=\s*\d+\s*;\s*#\s*((no|use)\s+warnings.*)/$1$2/ ;
209b39c5158Smillert          };
210b39c5158Smillert    }
211b39c5158Smillert
212b39c5158Smillert    if ($downgrade || $do_downgrade) {
213b39c5158Smillert        $our_sub = sub {
214b39c5158Smillert	    if ( /^(\s*)our\s+\(\s*([^)]+\s*)\)/ ) {
215b39c5158Smillert                my $indent = $1;
216b39c5158Smillert                my $vars = join ' ', split /\s*,\s*/, $2;
217b39c5158Smillert                $_ = "${indent}use vars qw($vars);\n";
218b39c5158Smillert            }
219b39c5158Smillert	    elsif ( /^(\s*)((use|no)\s+(bytes|utf8)\s*;.*)$/)
220b39c5158Smillert            {
221b39c5158Smillert                $_ = "$1# $2\n";
222b39c5158Smillert            }
223b39c5158Smillert          };
224b39c5158Smillert    }
225b39c5158Smillert    #elsif ($] >= 5.006000 || $upgrade) {
226b39c5158Smillert    elsif ($upgrade) {
227b39c5158Smillert        $our_sub = sub {
228b39c5158Smillert	    if ( /^(\s*)use\s+vars\s+qw\((.*?)\)/ ) {
229b39c5158Smillert                my $indent = $1;
230b39c5158Smillert                my $vars = join ', ', split ' ', $2;
231b39c5158Smillert                $_ = "${indent}our ($vars);\n";
232b39c5158Smillert            }
233b39c5158Smillert	    elsif ( /^(\s*)#\s*((use|no)\s+(bytes|utf8)\s*;.*)$/)
234b39c5158Smillert            {
235b39c5158Smillert                $_ = "$1$2\n";
236b39c5158Smillert            }
237b39c5158Smillert          };
238b39c5158Smillert    }
239b39c5158Smillert
240b39c5158Smillert    if (! $our_sub && ! $warn_sub) {
241b39c5158Smillert        warn "Up/Downgrade not needed.\n";
242b39c5158Smillert	if ($upgrade || $downgrade)
243b39c5158Smillert          { exit 0 }
244b39c5158Smillert        else
245b39c5158Smillert          { return }
246b39c5158Smillert    }
247b39c5158Smillert
248b39c5158Smillert    foreach (@files) {
249b39c5158Smillert        #if (-l $_ )
250b39c5158Smillert          { doUpDown($our_sub, $warn_sub, $_) }
251b39c5158Smillert          #else
252b39c5158Smillert          #{ doUpDownViaCopy($our_sub, $warn_sub, $_) }
253b39c5158Smillert    }
254b39c5158Smillert
255b39c5158Smillert    warn "Up/Downgrade complete.\n" ;
256b39c5158Smillert    exit 0 if $upgrade || $downgrade;
257b39c5158Smillert
258b39c5158Smillert}
259b39c5158Smillert
260b39c5158Smillert
261b39c5158Smillertsub doUpDown
262b39c5158Smillert{
263b39c5158Smillert    my $our_sub = shift;
264b39c5158Smillert    my $warn_sub = shift;
265b39c5158Smillert
266b39c5158Smillert    return if -d $_[0];
267b39c5158Smillert
268b39c5158Smillert    local ($^I) = ($^O eq 'VMS') ? "_bak" : ".bak";
269b39c5158Smillert    local (@ARGV) = shift;
270b39c5158Smillert
271b39c5158Smillert    while (<>)
272b39c5158Smillert    {
273b39c5158Smillert        print, last if /^__(END|DATA)__/ ;
274b39c5158Smillert
275b39c5158Smillert        &{ $our_sub }() if $our_sub ;
276b39c5158Smillert        &{ $warn_sub }() if $warn_sub ;
277b39c5158Smillert        print ;
278b39c5158Smillert    }
279b39c5158Smillert
280b39c5158Smillert    return if eof ;
281b39c5158Smillert
282b39c5158Smillert    while (<>)
283b39c5158Smillert      { print }
284b39c5158Smillert}
285b39c5158Smillert
286b39c5158Smillertsub doUpDownViaCopy
287b39c5158Smillert{
288b39c5158Smillert    my $our_sub = shift;
289b39c5158Smillert    my $warn_sub = shift;
290b39c5158Smillert    my $file     = shift ;
291b39c5158Smillert
292b39c5158Smillert    use File::Copy ;
293b39c5158Smillert
294b39c5158Smillert    return if -d $file ;
295b39c5158Smillert
296b39c5158Smillert    my $backup = $file . ($^O eq 'VMS') ? "_bak" : ".bak";
297b39c5158Smillert
298b39c5158Smillert    copy($file, $backup)
299b39c5158Smillert        or die "Cannot copy $file to $backup: $!";
300b39c5158Smillert
301b39c5158Smillert    my @keep = ();
302b39c5158Smillert
303b39c5158Smillert    {
304b39c5158Smillert        open F, "<$file"
305b39c5158Smillert            or die "Cannot open $file: $!\n" ;
306b39c5158Smillert        while (<F>)
307b39c5158Smillert        {
308b39c5158Smillert            if (/^__(END|DATA)__/)
309b39c5158Smillert            {
310b39c5158Smillert                push @keep, $_;
311b39c5158Smillert                last ;
312b39c5158Smillert            }
313b39c5158Smillert
314b39c5158Smillert            &{ $our_sub }() if $our_sub ;
315b39c5158Smillert            &{ $warn_sub }() if $warn_sub ;
316b39c5158Smillert            push @keep, $_;
317b39c5158Smillert        }
318b39c5158Smillert
319b39c5158Smillert        if (! eof F)
320b39c5158Smillert        {
321b39c5158Smillert            while (<F>)
322b39c5158Smillert              { push @keep, $_ }
323b39c5158Smillert        }
324b39c5158Smillert        close F;
325b39c5158Smillert    }
326b39c5158Smillert
327b39c5158Smillert    {
328b39c5158Smillert        open F, ">$file"
329b39c5158Smillert            or die "Cannot open $file: $!\n";
330b39c5158Smillert        print F @keep ;
331b39c5158Smillert        close F;
332b39c5158Smillert    }
333b39c5158Smillert}
334b39c5158Smillert
335b39c5158Smillert
336b39c5158Smillertsub FindBrokenDependencies
337b39c5158Smillert{
338b39c5158Smillert    my $version = shift ;
339b39c5158Smillert    my %thisModule = map { $_ => 1} @_;
340b39c5158Smillert
341b39c5158Smillert    my @modules = qw(
342b39c5158Smillert                    IO::Compress::Base
343b39c5158Smillert                    IO::Compress::Base::Common
344b39c5158Smillert                    IO::Uncompress::Base
345b39c5158Smillert
346b39c5158Smillert                    Compress::Raw::Zlib
347b39c5158Smillert                    Compress::Raw::Bzip2
348b39c5158Smillert
349b39c5158Smillert                    IO::Compress::RawDeflate
350b39c5158Smillert                    IO::Uncompress::RawInflate
351b39c5158Smillert                    IO::Compress::Deflate
352b39c5158Smillert                    IO::Uncompress::Inflate
353b39c5158Smillert                    IO::Compress::Gzip
354b39c5158Smillert                    IO::Compress::Gzip::Constants
355b39c5158Smillert                    IO::Uncompress::Gunzip
356b39c5158Smillert                    IO::Compress::Zip
357b39c5158Smillert                    IO::Uncompress::Unzip
358b39c5158Smillert
359b39c5158Smillert                    IO::Compress::Bzip2
360b39c5158Smillert                    IO::Uncompress::Bunzip2
361b39c5158Smillert
362b39c5158Smillert                    IO::Compress::Lzf
363b39c5158Smillert                    IO::Uncompress::UnLzf
364b39c5158Smillert
365b39c5158Smillert                    IO::Compress::Lzop
366b39c5158Smillert                    IO::Uncompress::UnLzop
367b39c5158Smillert
368b39c5158Smillert                    Compress::Zlib
369b39c5158Smillert                    );
370b39c5158Smillert
371b39c5158Smillert    my @broken = ();
372b39c5158Smillert
373b39c5158Smillert    foreach my $module ( grep { ! $thisModule{$_} } @modules)
374b39c5158Smillert    {
375b39c5158Smillert        my $hasVersion = getInstalledVersion($module);
376b39c5158Smillert
377b39c5158Smillert        # No need to upgrade if the module isn't installed at all
378b39c5158Smillert        next
379b39c5158Smillert            if ! defined $hasVersion;
380b39c5158Smillert
381b39c5158Smillert        # If already have C::Z version 1, then an upgrade to any of the
382b39c5158Smillert        # IO::Compress modules will not break it.
383b39c5158Smillert        next
384b39c5158Smillert            if $module eq 'Compress::Zlib' && $hasVersion < 2;
385b39c5158Smillert
386b39c5158Smillert        if ($hasVersion < $version)
387b39c5158Smillert        {
388b39c5158Smillert            push @broken, $module
389b39c5158Smillert        }
390b39c5158Smillert    }
391b39c5158Smillert
392b39c5158Smillert    return @broken;
393b39c5158Smillert}
394b39c5158Smillert
395b39c5158Smillertsub getInstalledVersion
396b39c5158Smillert{
397b39c5158Smillert    my $module = shift;
398b39c5158Smillert    my $version;
399b39c5158Smillert
400b39c5158Smillert    eval " require $module; ";
401b39c5158Smillert
402b39c5158Smillert    if ($@ eq '')
403b39c5158Smillert    {
404b39c5158Smillert        no strict 'refs';
405b39c5158Smillert        $version = ${ $module . "::VERSION" };
406b39c5158Smillert        $version = 0
407b39c5158Smillert    }
408b39c5158Smillert
409b39c5158Smillert    return $version;
410b39c5158Smillert}
411b39c5158Smillert
412b39c5158Smillertpackage MakeUtil ;
413b39c5158Smillert
414b39c5158Smillert1;
415