#!/usr/bin/env perl -w

use warnings FATAL => 'all';
use strict;

foreach my $file (@ARGV) {
    $. = 0;
    my $set_e_count= 0;
    my $last_nonblank_line = undef;
    open (my $fh, '<', $file) or die "cannot open $file: $!";
    while (<$fh>) {
        chomp;
        if (/^\s*set -e\s*$/) {
            $set_e_count++;
            /^\s|\s$/ and warn "$file:$.: 'set -e' with extra whitespaces\n";
            if ($last_nonblank_line ne '. ./defs || Exit 1') {
               warn "$file:$.: 'set -e' used, but previous line was not" .
                    " the inclusion of './defs'\n";
               warn "$file:$.: last line was: ^$last_nonblank_line\$\n";
            }
        }
        /\S/ and $last_nonblank_line = $_;
    }
    if ($set_e_count > 1) {
        warn "$file: more than one call 'set -e' ($set_e_count calls)\n";
    } elsif (! $set_e_count) {
        warn "$file: no proper 'set -e' call\n";
    }
    close ($fh) or die "cannot close $file: $!";
}
