#!/usr/bin/perl
#
# script to read a text filepro PRC file, and wiki-fy it.
# basically, hyperlink all file names, and hyperlink all known operators.

while (<>) {
	$myline = " $_";
	$myline =~ s/ eq / [[eq]] /ig;
	$myline =~ s/ co / [[co]] /ig;
	$myline =~ s/ ne / [[ne]] /ig;
	$myline =~ s/ le / [[le]] /ig;
	$myline =~ s/ ge / [[ge]] /ig;
	$myline =~ s/ gt / [[gt]] /ig;
#if (0 == 1) {
	$myline =~ s/ lt / [[lt]] /ig;
	$myline =~ s/lookup /[[lookup]] /ig;
	$myline =~ s/ r=free / [[r=free]] /ig;
	$myline =~ s/\{/[[concatenate|{]] /g;
	$myline =~ s/\</[[concatenate|<]] /g;
	$myline =~ s/\&/[[concatenate|&]] /g;
	$myline =~ s/mid\(/[[mid]]\(/ig;
	$myline =~ s/\@FN/[[\@FN]]/ig;
	$myline =~ s/\@ID/[[\@ID]]/ig;
	$myline =~ s/\@SN/[[\@SN]]/ig;
	$myline =~ s/\@RN/[[\@RN]]/ig;
	$myline =~ s/\@TD/[[\@TD]]/ig;
	$myline =~ s/\@TM/[[\@TM]]/ig;
	$myline =~ s/return/[[return]]/ig;
	$myline =~ s/input/[[input]]/ig;
	$myline =~ s/goto/[[goto]]/ig;
	$myline =~ s/gosub/[[gosub]]/ig;
	$myline =~ s/display/[[display]]/ig;
	$myline =~ s/end/[[end]]/ig;
	$myline =~ s/show/[[show]]/ig;
	$myline =~ s/form/[[form]]/ig;
	$myline =~ s/beep/[[beep]]/ig;
	$myline =~ s/delete/[[delete]]/ig;
	$myline =~ s/popup/[[popup]]/ig;
	$myline =~ s/lowup/[[lowup]]/ig;
	$myline =~ s/allup/[[allup]]/ig;
	$myline =~ s/getnext/[[getnext]]/ig;
	$myline =~ s/getprev/[[getprev]]/ig;

	if ($myline =~ m/lookup/i) {
		#print "in lookup loop\n";
		#sleep 3;
		#keyfield lookups
		@components = split /:/, $myline;
		@look_comps = split /\s+/, $components[2];

		
		if ($look_comps[2] =~ "[rk]=") { 
			$LFILE = $look_comps[1];
			$look_comps[1] = "\[\[$look_comps[1]\]\]";
			#print STDOUT "$look_comps[3] pos 3 has the FileName\n";
		} else {
			$LFILE = $look_comps[3];
			$look_comps[3] = "\[\[$look_comps[3]\]\]";	
			#print STDOUT "$look_comps[2] pos 2 has the FileName\n";
		}
		$components[2] = join(" ",@look_comps);
		$myline = join(":",@components);
		#index lookups ahead!
		if ($myline =~ "i=") { 
			@crap = split /i=/, $myline;
			@indexname = split /\s+/, $crap[1];
			$targetindex=$indexname[1];
			if ($indexname[0] =~ "[A-Za-z0-9]") { 
				$newindexname = &linkindex($indexname[0],$LFILE);
				$indexname[0] = $newindexname;
			} else {
				$newindexname = &linkindex($indexname[1],$LFILE);
				$indexname[1] = $newindexname;
			}

			$crap[1] = join(" ",@indexname);
			$myline = join("i=",@crap);
			$myline = "$myline\n";
		}
		$myline =~ s/k=/[[k=]]/i;
		$myline =~ s/r=/[[r=]]/i;

	}
	print $myline;
}	

sub linkindex($$) {
	$myindex=$_[0];
	$myfilename=$_[1];
	#print STDOUT "linkindex got |$_[0]| and |$_[1]|\n";
	$wiki_d_index = "[[$myfilename.index.$myindex|$myindex]]";

	return $wiki_d_index;
}
