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 11use strict; 12use warnings; 13$| = 1; 14 15# Get command line options 16use Getopt::Long; 17require "./Porting/manifest_lib.pl"; 18my $outfile; 19my $check_only = 0; 20my $quiet = 0; 21GetOptions ('output=s' => \$outfile, 22 'check' => \$check_only, 23 'quiet' => \$quiet); 24 25my $file = (@ARGV) ? shift : 'MANIFEST'; 26 27# Read in the MANIFEST file 28open(my $IN, '<', $file) 29 or die("Can't read '$file': $!"); 30my @manifest = <$IN>; 31close($IN) or die($!); 32chomp(@manifest); 33 34my %seen= ( '' => 1 ); # filter out blank lines 35my @sorted = grep { !$seen{$_}++ } 36 sort_manifest(@manifest) 37; 38 39# Check if the file is sorted or not 40my $exit_code = 0; 41for (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 48if (defined($outfile)) { 49 open(my $OUT, '>', $outfile) 50 or die("Can't open output file '$outfile': $!"); 51 binmode($OUT); 52 print($OUT join("\n", @sorted), "\n"); 53 close($OUT) or die($!); 54} 55 56# Report on sort results 57printf(STDERR "'$file' is%s sorted properly\n", 58 (($exit_code) ? ' NOT' : '')) if (! $quiet); 59 60# Exit with the sort results status 61exit($exit_code); 62 63# EOF 64