#!/usr/bin/perl -w

# rulesout.pl: a short script to generate input for the techtree 
# program from Freeciv ruleset files.
#
# Author: Mike Jing <mikezjing AT hotmail DOT com>
#
# This software is released under the terms of the Artistic License
# <http://www.opensource.org/licenses/artistic-license.html>
# Although there's hardly anything artistic about it.


if (@ARGV) {
    $data_dir = $ARGV[0];
} 
else {
    $data_dir = 'data/default';
}

$tech_file = $data_dir.'/techs.ruleset';
$gov_file = $data_dir.'/governments.ruleset';
$unit_file = $data_dir.'/units.ruleset';
$build_file = $data_dir.'/buildings.ruleset';

open(TECH, $tech_file) or die "$tech_file not found.\nUsage: $0 [ datadir ]\n";
while (<TECH>) {
    if (/^name\s*=.*?"(\w.*?)"/) {
	$tech = $1;
	$reqs{$tech} = "";
	$effects{$tech} = "";
	$obs_effects{$tech} = "";
	while ($line = <TECH>) {
	    if ($line =~ /^req\d+\s*=.*?"(\w.*?)"/) {
		$req = $1;
		$reqs{$tech} .= "$req;";
	    }
	    elsif ($line =~ /^\[advance_.*\]/) {
		last;
	    }
	}
    }
}

if (open(GOV, $gov_file)) {
    while (<GOV>) {
	if (/^name\s*=.*?"(\w.*?)"/) {
	    $gov = $1;
	    while (<GOV>) {
		if (/^tech_req\s*=.*?"(\w.*?)"/) {
		    $tech = $1;
		    $effects{$tech} .= "gov: $gov;";
		    last;
		}
	    }
	}
    }
}
else {
    print STDERR "$gov_file not found. Skipping...\n";
}

if (open(UNIT, $unit_file)) {
    while (<UNIT>) {
	if (/^name\s*=.*?"(\w.*?)"/) {
	    $unit = $1;
	    while ($line = <UNIT>) {
		if ($line =~ /^tech_req\s*=.*?"(\w.*?)"/) {
		    $tech = $1;
		    $tech_req{$unit} = $tech;
		    $effects{$tech} .= "unit: $unit;";
		}
		elsif ($line =~ /^obsolete_by\s*=.*?"(\w.*?)"/) {
		    $successor = $1;
		    if ($successor ne "None") {
			$successor{$unit} = $successor;
		    }
		}
		elsif ($line =~ /^\[unit_.*\]/) {
		    last;
		}
	    }
	}
    }
    foreach $unit (sort keys %successor) {
	$tech = $tech_req{$successor{$unit}};
	if ($tech ne "None") {
	    $obs_effects{$tech} .= "obs-unit: $unit;";
	}
    }
}
else {
    print STDERR "$unit_file not found. Skipping...\n";
}

if (open(BUILD, $build_file)) {
    while (<BUILD>) {
	if (/^name\s*=.*?"(\w.*?)"/) {
	    $building = $1;
	    while ($line = <BUILD>) {
		if ($line =~ /^tech_req\s*=.*?"(\w.*?)"/) {
		    $tech_req = $1;
		}
		elsif ($line =~ /^obsolete_by\s*=.*?"(\w.*?)"/) {
		    $tech_obs = $1;
		}
		elsif ($line =~ /^is_wonder\s*=\s*(\d)/) {
		    $is_wonder = $1;
		}
		elsif ($line =~ /^\[building_.*\]/) {
		    if ($is_wonder eq 0) {
			$cat = "impr";
		    }
		    else {
			$cat = "wonder";
		    }
		    if ($tech_req ne "None") {
			$effects{$tech_req} .= "$cat: $building;";
		    }
		    if ($tech_obs ne "None") {
			$obs_effects{$tech_obs} .= "obs-$cat: $building;";
		    }
		    last;
		}
	    }
	}
    }
}
else {
    print STDERR "$build_file not found. Skipping...\n";
}

print "[tech_0]\n";
print "name=\"None\"\n";
print "\n";

$tech_no = 1;
foreach $tech (sort keys %reqs) {
    print "[tech_$tech_no]\n";
    print "name=\"$tech\"\n";
    $req_no = 0;
    @reqs = split(";", $reqs{$tech});
    foreach $req (@reqs) {
	if ($req eq "Never") {
	    print "exists=0\n";
	    last;
	}
	next if ($req eq "None");	    
	print "req${req_no}=\"$req\"\n";
	$req_no++;
    }
    $effects{$tech} .= $obs_effects{$tech};
    $effect_no = 0;
    @effects = split(";", $effects{$tech});
    foreach $effect (@effects) {
	print "effect${effect_no}=\"$effect\"\n";
	$effect_no++;
    }
    print "\n";
    $tech_no++;
}
