summaryrefslogtreecommitdiff
path: root/Porting/manisort
blob: 167aada7345dcb946710187688b47ca17ec0ffa4 (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
#!/usr/bin/perl

# Usage:  manisort [-q] [-o outfile] [filename]
#
# Without 'filename', looks for MANIFEST in the current dir.
# With '-o outfile', writes the sorted MANIFEST to the specified file.
# Prints the result of the sort to stderr.  '-q' silences this.
# The exit code for the script is the sort result status
# (i.e., 0 means already sorted properly, 1 means not properly sorted)

use strict;
use warnings;
$| = 1;

# Get command line options
use Getopt::Long;
require "./Porting/manifest_lib.pl";
my $outfile;
my $check_only = 0;
my $quiet = 0;
GetOptions ('output=s' => \$outfile,
            'check'    => \$check_only,
            'quiet'    => \$quiet);

my $file = (@ARGV) ? shift : 'MANIFEST';

# Read in the MANIFEST file
open(my $IN, '<', $file)
    or die("Can't read '$file': $!");
my @manifest = <$IN>;
close($IN) or die($!);
chomp(@manifest);

my %seen= ( '' => 1 ); # filter out blank lines
my @sorted = grep { !$seen{$_}++ }
             sort_manifest(@manifest)
;

# Check if the file is sorted or not
my $exit_code = 0;
for (my $ii = 0; $ii < $#manifest; $ii++) {
    next if ($manifest[$ii] eq $sorted[$ii]);
    $exit_code = 1;   # Not sorted
    last;
}

# Output sorted file
if (defined($outfile)) {
    open(my $OUT, '>', $outfile)
        or die("Can't open output file '$outfile': $!");
    binmode($OUT);
    print($OUT join("\n", @sorted), "\n");
    close($OUT) or die($!);
}

# Report on sort results
printf(STDERR "'$file' is%s sorted properly\n",
            (($exit_code) ? ' NOT' : '')) if (! $quiet);

# Exit with the sort results status
exit($exit_code);

# EOF