#!/usr/bin/perl -w

sub mydie {
   my $message = shift;
   print "ERROR: $message\n";
   exit 1;
}

print "reindent - reformat specified files to match Audacity guidelines.\n";

# check for indent and correct version

$indent = `which indent`;
chomp $indent;

-x $indent || mydie "GNU indent must be installed and in your path";

$version = `$indent --version`;
($major, $minor, $subminor) = ($version =~ /(\d+)\.(\d+)\.(\d+)/);

if($major < 2 ||
   ($major == 2 && $minor < 2) ||
   ($major == 2 && $minor == 2 && $subminor < 4)) {

   mydie "GNU indent must be at least version 2.2.6 (found: $major.$minor.$subminor)";
}

mydie "no files specified" unless @ARGV;

# everything checks out, proceed...

foreach my $file (@ARGV) {
   mydie "file \"$file\" does not exist" unless -e $file;
   print "indenting $file...\n";
   system("$indent -kr -nut -i3 -nsob $file");
   system("rm $file~");
}

# arch-tag: b30b2ac9-5f36-4226-8b79-cb8ebfb908e1