#!/usr/bin/perl
#  Bigwikifier.pl  Copyright 2005 by Jeremy Anderson
#  Licensed under the GPL, v2.  See http://www.gnu.org/
#
#  You must have a working install of mediawiki to use this tool.
#  Your filepro processing tables must be compiled to text (ABE=ASCII)
#  This works on filepro 4.02, not tested on anything else.
#  comments, questions, flames to jeremy a t angelar . com
#
#  If this tool helps you escape FilePro, I'd love to hear about it.
#
#  If this tool make FilePro supportable for you, I'm happy for you.  Keep using
#  the tool.
#
#  As the GPL specifies, this code may be sold, or bundled with code that is sold, but 
#  ANY MODIFICATIONS MUST BE MADE AVAILABLE.
#
#  Furthermore, yes, this is an ugly hack of code.  I will likely optimize this a bit as I add features.
#  The next step will be to add code to translate menus.
#  Then, selection sets.
#  I'll probably try to strip out the index header information and wikify that some how.
#  Actually doing file maps would be nice as well.
#  And, pulling out the sort/subtotal information from output formats, as long as somehow automatically mapping 
#    temporary variables to given screens.
#
#    As my good friend Mike always tells me, I'm gilding the turd here.


sub check_if_exists($) {
	my $totalcount = 0;
	$query = "select count(*) from cur where cur_title = \"$_[0]\"";
	#print "sending query |$query|\n";
	$result = $db->prepare($query);
	$result->execute();
	@row = $result->fetchrow_array();
	$totalcount = @row[0];
	$result->finish();
	#print "the query returned:  @row[0]\n";


	return $totalcount;
}

sub getPageTitle($$) {
	if ($_[1] eq "none") { 
		$current_dir = `pwd`;
		chomp $current_dir;
	} else {$current_dir = $_[1];}
	$basename = `basename $current_dir`;
	$basename = "\u$basename";
	chomp $basename;
	$filename = $_[0];
	return "$basename.$filename";
}

sub pushlink($) {
	#print "entering pushlink found is $found{\"$_[0]\"}\n";
	if (!defined $found{"$_[0]"}) {	
		push (@linklist, $_[0]);
		#	print "pushing a link to $_[0]\n";
		$len = @linklist;
		#print "list is now $len items long\n";
		$found{"$_[0]"} = "found";
	} #else { print "we already linked to $_[0]\n"; }
	return;
}

sub wikilink($) {
	#print "called with |$_[0]|\n";
	$argument = $_[0];
	if ($argument =~ '|' ) { 
		($reallink, $crud) = split(/\|/, $argument); 
		pushlink($reallink);
	} else { pushlink($argument); }
	$newlink = "\[\[$argument\]\]";

	return $newlink;

}

sub check_if_text($$$) {
	#print "entering check_is_text, with arguments |$_[0]|$_[1]|\n";
	my $iskosher = "0";
	$file = "$_[0]/$_[1]/$_[2]";
	#print "examining |$file|\n";
	#print "grep :\$ $file| wc -l";
	$count = `grep :\$ $file| wc -l`;
	if ($count != 0) { $iskosher = 1; }
	#print "back from the grep/linecount\n";
	return $iskosher;
}

sub get_link_id($) { 
	#given a text page title, return the id of the existing page, or -1 if it is a new page
	#remember, you have to capitalize the first letter of the name!
	$targetpage = 
	$pageid = "-1";
	$query = "select cur_id from cur where lower(cur_title) = lower(\"$_[0]\")";
	#print "sending query |$query|\n";
	$result = $db->prepare($query);
	$result->execute();
	if (@row = $result->fetchrow_array ) {
		$pageid = @row[0];
	}
	$result->finish();

	return $pageid;
}

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

	return $wiki_d_index;
}

sub wikifile($) {
	$fullpath = $_[0];
	open(MYFILE,"$fullpath") or die "cannot open file!  aborting!\n";
	while ($myline = <MYFILE>) {
		foreach $commandword ('lookup','r=free','@FN','@ID','@SN','@RN','@TD','@TM','return','select','copy','input','goto','gosub','display','end','show','form','beep','delete','popup','lowup','allup','mdy\/','escape','getnext','getprev','@wlf','@wef','YYMD','screen','uplow','lowup','date','system','restart','mdyy\/','-nx','-ne', '-n', 'pushkey','skip') {
			if ($myline =~ /$commandword/i) {
				#print "changing $commandword!\n";
				$wikid_command = wikilink("$commandword");
				$myline =~ s/$commandword/$wikid_command/ig;
			}
		}
		foreach $comparison ('eq','co','le','ge','gt','lt','ne') {
			if ($myline =~ / $comparison /i) {
				$wikid_command = wikilink("$comparison");
				$myline =~ s/ $comparison / $wikid_command /ig;
			}
		}

		foreach $concatenator ('\{','\<','\&') {
			if ($myline =~ "$concatenator") {
				$myline =~ s/$concatenator/[[concatenate|$concatenator]] /g; 
				pushlink("concatenate"); 
			}
		}

		if ($myline =~ /system/i) {
			#break this bugger on double-quotes, because the command will lie inside them.
			($comp1, $comp2, $comp3) = split(/:/,$myline);
			($junk, $command, $junk) = split(/\"/,$comp3);
			if ($command =~ /\/appl\/fp\/menus\//i) {
				#peel off just the actual menu name
				($one, $two,$three, $four, $five) = split(/\//,$command);	
				$command = $five;
			}
			$wikid_command = wikilink("$command");
			$myline =~ s/$command/$wikid_command/ig;
		}

		if ($myline =~ /mid\(/i) {
			pushlink("mid");
			$myline = s/mid\(/[[mid]]\(/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] = wikilink($look_comps[1]);
				#print STDOUT "$look_comps[3] pos 3 has the FileName\n";
			} else {
				$LFILE = $look_comps[3];
				$look_comps[3] = wikilink($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;
	
		}
		$fulltext = "$fulltext $myline";
	}
	close MYFILE;
	return $fulltext;
}

use DBI;

my $PFDIR="/home/jeremy/filepro";
my $SQLServer = "earwig.plunketts.net";
my $filename = "none";
my $dirname = "none";
my $wikiuser = "wikiuser";
my $wikipass = "plunk_db";
#my %found = ("","");

if (@ARGV == 2) {
	$filename = $ARGV[0];
	$dirname = $ARGV[1];
	##print "I found @ARGV acceptable.  1st argument is $ARGV[0]\n";
	#print "filename is now $filename\n";
}
if ($filename eq "none") { die "must specify a processing table to wikify.  Cannot process $filename!\n"; }

$pagetitle = getPageTitle($filename, $dirname);
#print "Pagetitle is $pagetitle\n";

$db = DBI->connect("DBI:mysql:wikidb:$SQLServer","$wikiuser","$wikipass") or die "could not connect\n";
$isthere = check_if_exists($pagetitle);
if ($isthere > 0) { die "the page: $pagetitle already exists\n"; }

$iskosher = check_if_text($PFDIR, $ARGV[1], $ARGV[0]);
if ($iskosher == 0) { die "the page ~/$ARGV[1]/$ARGV[0] is NOT in text format.  It must be recompiled\n";} 
#if ($iskosher == 1) { print "this file is indeed text.  proceeding.\n"; }

#okay, if we get this far, it is an entirely new page!
#
#Now we get to feed this sucker into the database!
#

#steps:
#   1) build page title  (done)
#   2) build page body   (next)
#   3) build INSERT statement (later)

#$body = `~/filepro/util/wikifier.pl ~/filepro/$ARGV[1]/$ARGV[0]`;
$body = wikifile("$PFDIR/$dirname/$filename");
$body =~ s/'/\\'/g;
#print "BODY IS $body\n";
($crud, $shortfilename) = split /\./, $filename;

$bodyheader = "see also [[$dirname]], and [[$dirname.out.$shortfilename|out.$shortfilename]]\n";
pushlink($dirname);
pushlink("$dirname.out.$shortfilename");
$newbody = $bodyheader . $body;
$query = "INSERT into cur(cur_title,cur_text,cur_user_text) VALUES (\'$pagetitle\',\'$newbody\',\'WikifierBot\')";
$result = $db->prepare($query);
$result->execute();
$result->finish();
#now, get our own page id, and create links properly.
$myPageID=get_link_id($pagetitle);
# to create links, we'll need to look up all existing links, and insert them properly into either brokenlinks table, or links table.
#
foreach $link (@linklist) {
	#lookup each page, see if it exists.  If it does, stick it in 'links', else, in 'brokenlinks' table
	$newlinkID=get_link_id($link);
	if ($newlinkID == "-1") {
		#because mediawiki capitalizes the first letter of each work, we'll have to, as well.
		$link =~ s/([\w'].+)/\u$1/g;
		$query = "INSERT into brokenlinks(bl_from, bl_to) VALUES ($myPageID,\"$link\")";
		#print "inserting brokenlink to $link\n";
		#print "$query\n";
	} else {
		$query = "INSERT into links(l_from, l_to) VALUES ($myPageID,$newlinkID)";
		#print "inserting link from $myPageID to $newlinkID\n";
	}
	$result = $db->prepare($query);
	$result->execute();
	$result->finish();
}

print "inserted $pagetitle into wikidb\n";


