#!/usr/bin/perl
# Like tex-check but for org

#Initializations
$, = ' ';		# set output field separator
$\ = "\n";		# set output record separator
#local function definitions

sub org_check {
my ($file,$handle) = @_;
        $handle++;  #generate unique handle 
#print "Org-checking $file";

        open($handle, "$file") || return 0; 

while (<$handle>) {
    chomp;	# strip record separator
    #ignore comments
    next  if (/^\s*\#/);
    #Missing space after ,
    if(/,[a-zA-Z]/) {
      &print_message("Possible missing space after comma ".$_);
}
    #warn if space precedes a hyphen
    if (/\s-[a-zA-Z]/) {
      &print_message ("Possible unwanted space in hyphenated word ". $_);
    }
    #warn if space follows a hyphen
    if (/[a-zA-Z]-\s+/) {
      &print_message ("Possible unwanted space in hyphenated word ". $_);
    }
    #warn if hyphenated word in mixed case
    if (/[a-z]-[A-Z]/) {
      &print_message ("Mixed case in hyphenated word ". $_);
    }
    #Unwanted blank lines
#remember that blank line seen:
    if (/^[ ]*$/) {
	$blank = 1;
    }
#and if line after blank line starts in lower case: ...
    if (/^[ ]*[a-z]/) {
	if ($blank == 1) {
	    &print_message("Check capitalization".$_);
	    $blank = 0;
	}
    }
    if (!/^[ ]*$/) {
	$blank = 0;
    }
    # Space before Punctuations: 
    if (/\ +\./) {
	&print_message('Space before period. ');
	print $_;
    }
if (/\ +;/) {
	&print_message('Space before semi colon. ');
	print $_;
    }
    if (/\ +,/) {
	&print_message('Space before comma. ');
	print $_;
    }
    if (/\ +\?/) {
	&print_message('Space before question-mark. ');
	print $_;
    }
    if (/\ +:/) {print_message("Space before colon. ")}
    if (/\ +;/) {print_message("Space before  semi-colon ")}
    if (/\ +\!/) {print_message("Space before  exclamation-mark ")}
    if (/[ a-z]\.[A-Z]/) {
	&print_message('Space missing between sentences.');
	print $_;
    }
    if (/[.!?]\ +[a-z]/) {
	&print_message('Sentence does not start in upper case');
	print $_;
    }
}
close $handle; 
}


sub print_message {
    my ($message_string) = @_;

    #Print a message like grep -n
    print $ARGV . ':' . $. . ':' . $message_string;
}

#top-level:
$file = shift || print "Specify a filename to check. "; 
&org_check($file, 'fh00'); 
