# guess operating system

$os = &check_os;

if ($os =~ /Windows/) {
  $binpath = "..\\..\\..\\src\\tools\\";
  $compare = "fc /n";
  $remove = "del";
}
elsif ($os =~ /Unix/) {
  $binpath = "../../../src/tools/";
  $compare = "diff";
  $remove = "rm";
}
else {
  die "Unsupported operating system: $os";
};

# real work begin here

opendir(DIR, ".") || die "Can't open current dir";

@entries = readdir(DIR);

closedir(DIR);

@sorted = sort(@entries);

foreach $entry (@sorted) {
  if (($entry =~ /^c/) and ($entry =~ /java$/)) {

    print "... Unparse $entry ... \n";
    $cmd=$binpath."unparse";
    $out="unparsed_".$entry; 
    system("$cmd $entry > $out");

    print "... Compare source with unparsed $entry ...\n";
    system("$compare $entry $out");

    print "... Compare unparsed $entry with output of another unparse phase ...\n";
    system("$cmd $out > temp ");
    system("$compare $out temp");
    
    print "... Prettify $entry ...\n";
    $cmd=$binpath."prettify";
    $out="prettified_".$entry; 
    system("$cmd $entry > $out");

    print "... Compare prettified $entry with output of another prettify phase ...\n";
    system("$cmd $out > temp ");
    system("$compare $out temp");

    print "\n";
  }
};
  
system ("$remove temp");

###
#
# Uses Config module to
# detect O.S.
#
use Config;

#
# Uses Config module to guess O.S.
# The Config module does have the O.S.
# name, but no direct reference to Unix,
# so you have to guess.
#
sub check_os {

    # Get O.S. name first.
    $osname = $Config{'osname'};
    $os     = "Unknown";

    # If its Windows, then it's not Unix.
    if ( $osname =~ /^Windows/ ) {
        $os = "Windows";
    } elsif ( $osname =~ /^MSWin/ ) {
        $os = "Windows";
    #
    # Anything ending in "ux"
    # "ix", "UX" or "IX", is Unix.
    #
    } elsif ( $osname =~ /ux$/ ) {
        $os = "Unix";
    } elsif ( $osname =~ /ix$/ ) {
        $os = "Unix";
    } elsif ( $osname =~ /UX$/ ) {
        $os = "Unix";
    } elsif ( $osname =~ /IX$/ ) {
        $os = "Unix";
    } else {
        # If not Windows, guess Unix.
        # This ignores the Mac and OS/2,
        # though.
        $os = "Unix";
    }

    return $os;
}

# config.pl

