#!/usr/bin/perl5
#
# Copyright 1999, Vincent Cate   vince@offshore.ai

$rfname = shift || die "Must specify a file to open.\n";    
open (IN,"<$rfname") || die "Can't open $rfname for reading.\n"; 

doall();

close(IN);

exit(0);



sub doall {
    $solvancydefault = 75;            
    while(<IN>) {          
        chop;
        (
            $country, $fname,  $language,
            $govfat, $expenditures, $gdptotal,
            $migration,  $unemploymentperc,
            $lifeexpectancy, $infantmortality,
            $gdpcap, $literacy, $inflation, $growthrate, $debt,
            $taxhaven,$land,$population)
             = split(':'); 

            $econfree  = maxof100 (100.0 - (200 * $govfat));
            $fvotes    = maxof100 (4 * $migration);
            $jobs      = maxof100 (100.0 - (4 * $unemploymentperc));
            $health    = maxof100 (1.2 * ($lifeexpectancy - $infantmortality));
            $wealth    = maxof100 ($gdpcap/300.0);
            $edu       = maxof100 (2 * ($literacy - 50));
            $curstb    = maxof100 (100 - (5 * $inflation));
            $growth    = maxof100 ($growthrate * 10);
            if ($population > 0) {
               $space     = maxof100 (10000 * $land / $population);
            } else {
               $space = 50;
            }
            if ($gdptotal > 0 && $debt != 123456 ) {
                $solvancy  = maxof100 (100 - (200 * $debt/$gdptotal));
            } else {
                $solvancy = $solvancydefault;
            }

            $rating = (0.5*$econfree  + 0.3*$fvotes + 0.1*$jobs   + 0.1*$health 
                 +0.1*$wealth    + 0.1*$edu    + 0.1*$curstb + 0.1*$growth
                 +0.1*$solvancy + 0.05*$space)/1.55;

           printf("%0.1f:%s:%s:%s:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f:%0.0f\n", 
            $rating,   $country, $fname,  $language, 
            $econfree, $fvotes,  $jobs,   $health, 
            $wealth,   $edu,     $curstb, $growth,  $solvancy, $taxhaven, $space);

   }
}


sub maxof100{
    ($factor) = @_;
    if ($factor > 100) {
       $result=100;
    } else {
       $result=$factor;
    }
    if ($result < -100) {
       $result=-100;
    }

    return($result);
}

                                                
