Commit | Line | Data |
---|---|---|
4e86fc4b JH |
1 | #!/usr/bin/perl |
2 | ||
3 | # Usage: manisort [-q] [-o outfile] [filename] | |
4 | # | |
5 | # Without 'filename', looks for MANIFEST in the current dir. | |
6 | # With '-o outfile', writes the sorted MANIFEST to the specified file. | |
7 | # Prints the result of the sort to stderr. '-q' silences this. | |
8 | # The exit code for the script is the sort result status | |
9 | # (i.e., 0 means already sorted properly, 1 means not properly sorted) | |
10 | ||
11 | use strict; | |
12 | use warnings; | |
13 | $| = 1; | |
14 | ||
15 | # Get command line options | |
16 | use Getopt::Long; | |
5051ccfe | 17 | require "./Porting/manifest_lib.pl"; |
4e86fc4b JH |
18 | my $outfile; |
19 | my $check_only = 0; | |
20 | my $quiet = 0; | |
21 | GetOptions ('output=s' => \$outfile, | |
22 | 'check' => \$check_only, | |
23 | 'quiet' => \$quiet); | |
24 | ||
25 | my $file = (@ARGV) ? shift : 'MANIFEST'; | |
26 | ||
27 | # Read in the MANIFEST file | |
28 | open(my $IN, '<', $file) | |
29 | or die("Can't read '$file': $!"); | |
30 | my @manifest = <$IN>; | |
31 | close($IN) or die($!); | |
32 | chomp(@manifest); | |
33 | ||
3dfcef7e YO |
34 | my %seen= ( '' => 1 ); # filter out blank lines |
35 | my @sorted = grep { !$seen{$_}++ } | |
36 | sort_manifest(@manifest) | |
37 | ; | |
4e86fc4b JH |
38 | |
39 | # Check if the file is sorted or not | |
40 | my $exit_code = 0; | |
41 | for (my $ii = 0; $ii < $#manifest; $ii++) { | |
42 | next if ($manifest[$ii] eq $sorted[$ii]); | |
43 | $exit_code = 1; # Not sorted | |
44 | last; | |
45 | } | |
46 | ||
47 | # Output sorted file | |
48 | if (defined($outfile)) { | |
49 | open(my $OUT, '>', $outfile) | |
50 | or die("Can't open output file '$outfile': $!"); | |
b14c5451 | 51 | binmode($OUT); |
4e86fc4b JH |
52 | print($OUT join("\n", @sorted), "\n"); |
53 | close($OUT) or die($!); | |
54 | } | |
55 | ||
56 | # Report on sort results | |
57 | printf(STDERR "'$file' is%s sorted properly\n", | |
58 | (($exit_code) ? ' NOT' : '')) if (! $quiet); | |
59 | ||
60 | # Exit with the sort results status | |
61 | exit($exit_code); | |
62 | ||
63 | # EOF |