#!/usr/bin/perl

opendir(DIR, "longs") || die;
foreach $name (readdir(DIR)) {
    if ($name =~ /Long.*/) {
        push(@names, $name);
    }
}

foreach $file (@names) {
    $target = $file;
    $target =~ s/Long/Int/;
    
    open INPUT, "longs/$file" or die;
    open OUTPUT, ">ints/$target" or die;
    while (<INPUT>) {
        if ($_ =~ /.*\/\/ PREPROC: .* only/) {
            if ($_ =~ /Int/) { 
                $_ =~ s/^\/\///;
                $_ =~ s/\/\/ PREPROC: .*//;
            }
	    else {
	        $_ = "";
	    }
	}
        elsif ($_ =~ /.*\/\/ PREPROC: except .*/) {
	    if ($_ =~ /Int/) { 
                $_ =~ "";
	    }
	    else {
                $_ =~ s/^\/\///;
                $_ =~ s/\/\/ PREPROC: .*//;
	    }
        }
        
        $_ =~ s/long/int/g;
        $_ =~ s/writeLong/writeInt/g;
        $_ =~ s/readLong/readInt/g;
        $_ =~ s/Long\)/Integer\)/g;
        $_ =~ s/Long\(/Integer\(/g;
        $_ =~ s/Long\./Integer\./g;
        $_ =~ s/Long /Integer /g;
        $_ =~ s/Long/Int/g;
        print OUTPUT $_;
    }
    
    close INPUT;   
    close OUTPUT;


    $target = $file;
    $target =~ s/Long/Short/;
        
    open INPUT, "longs/$file" or die;
    open OUTPUT, ">shorts/$target" or die;
    while (<INPUT>) {
        if ($_ =~ /.*\/\/ PREPROC: .* only/) {
            if ($_ =~ /Short/) { 
                $_ =~ s/^\/\///;
                $_ =~ s/\/\/ PREPROC: .*//;
            }
	    else {
	        $_ = "";
	    }
	}
        elsif ($_ =~ /.*\/\/ PREPROC: except .*/) {
	    if ($_ =~ /Short/) { 
                $_ =~ "";
	    }
	    else {
                $_ =~ s/^\/\///;
                $_ =~ s/\/\/ PREPROC: .*//;
	    }
        }
        
        $_ =~ s/long/short/g;
        $_ =~ s/Long/Short/g;
        print OUTPUT $_;
    }
    
    close INPUT;   
    close OUTPUT;
    
        
    #printf("Int%s\n", $target);
}
