3 # Usage: manisort [-q] [-o outfile] [filename]
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)
15 # Get command line options
20 GetOptions ('output=s' => \$outfile,
21 'check' => \$check_only,
24 my $file = (@ARGV) ? shift : 'MANIFEST';
26 # Read in the MANIFEST file
27 open(my $IN, '<', $file)
28 or die("Can't read '$file': $!");
30 close($IN) or die($!);
33 # Sort by dictionary order (ignore-case and
34 # consider whitespace and alphanumeric only)
36 (my $aa = $a) =~ s/[^\s\da-zA-Z]//g;
37 (my $bb = $b) =~ s/[^\s\da-zA-Z]//g;
41 # Check if the file is sorted or not
43 for (my $ii = 0; $ii < $#manifest; $ii++) {
44 next if ($manifest[$ii] eq $sorted[$ii]);
45 $exit_code = 1; # Not sorted
50 if (defined($outfile)) {
51 open(my $OUT, '>', $outfile)
52 or die("Can't open output file '$outfile': $!");
53 print($OUT join("\n", @sorted), "\n");
54 close($OUT) or die($!);
57 # Report on sort results
58 printf(STDERR "'$file' is%s sorted properly\n",
59 (($exit_code) ? ' NOT' : '')) if (! $quiet);
61 # Exit with the sort results status