#!/usr/bin/perl
#
# Author: Peter Keel <killer@discordia.ch>
#
# Aimed at converting DOS Microsoft-C to Borland C, which in turn can be
# used on Unix using libgrx. 
# 
# Linking with -lgrx -lbcc -lvga -lconio
#

die "Usage: $0 filenames\n"       unless($ARGV[0]);

foreach $file_name (@ARGV) {
    $temp_file="/tmp/$file_name";
    open(IN_FILE,"<$file_name") || die "Cannot open $file_name for input\n";
    open(TEMP,">$temp_file");
    while(<IN_FILE>){
    	$_ =~ s/_setvideomode\(_VRES16COLOR/setgraphmode\(VGAHI/g;
	$_ =~ s/_setvideomode\(_DEFAULTMODE/setgraphmode\(GRX_DEFAULT_GRAPHICS/g;
	$_ =~ s/_rectangle\(_GBORDER,/rectangle\(/g;
	$_ =~ s/_rectangle\(_GFILLINTERIOR,/rectangle\(/g;
	$_ =~ s/_displaycursor\(_GCURSORON/_setcursortype\(_NOCURSOR/g;
	$_ =~ s/_displaycursor\(_GCURSOROFF/_setcursortype\(_NOCURSOR/g;
	$_ =~ s/_outgtext/outtext/g;
	$_ =~ s/_setcolor/setcolor/g;
	$_ =~ s/_moveto/moveto/g;
	$_ =~ s/_settextposition/settextjustify/g;
	$_ =~ s/_registerfonts/registerbgifont/g;
	$_ =~ s/_setfont/installuserfont/g;
	$_ =~ s/_setcliprgn/setviewport/g;
	$_ =~ s/_clearscreen/GrClearScreen/g;
	$_ =~ s/_lineto/lineto/g;
	$_ =~ s/_getimage/getimage/g;
	$_ =~ s/_putimage/putimage/g;
	$_ =~ s/_floodfill/floodfill/g;
	$_ =~ s/_hfree/free/g;
	$_ =~ s/_huge//g; # don't need memory models. 
	$_ =~ s/_stdprn/tst/g; ### EVIL!
	$_ =~ s/halloc/calloc/g;
	$_ =~ s/\#include \<graphics.h\>/\#include \<Grx\/grx.h\>\n\#include \<Grx\/bccgrx.h\>\n\#include \<Grx\/mousex.h\>\n\#include \<Grx\/lnxconio.h\>\n\#include \<Grx\/soundIt.h\>/g;
	$_ =~ s/_GPSET/1/g; #whats this param to putimage? 
        print TEMP ($_);
    }
    close IN;
    close TEMP;
    system ("mv $temp_file $file_name");
}
