#!/bin/awk -f
#
#	Reads multiple nation and repo * input files, and calculates the
#	ranges of possible tech/research/edu levels possible based on these
#	reports.  Extracts names of countries from a relation and report
#	outputs.
#
#		nation
#		report
#		relations


function max(x,y) {
  if (x>y) return x;
  return y;
}
function min (x,y) {
  if (x<y) return x;
  return y;
}

function num(str) {
  return substr(str,1,length(str)-1);
}

#
#	Truncate starting and ending whitespace from a string
#
function truncwhite(str,  i,j) {
  for (i=1;i<=length(str) && substr(str,i,1) ~ "[ \t\r\n]";i++) ;
  for (j=length(str);j>i && substr(str,j,1) ~ "[ \t\r\n]";j--) ;
  return (substr(str,i,j-i+1));
}


BEGIN {
  SUBSEP=",";
  mode="";
  stat=0;


# some defaults so I don't have to parse *everything*
  country[82]="harmless";
  number["harmless"]=82;
  coun=82;
}


/.* Nation Report/ {
  if ($1 in number) coun=number[$1];
  country[coun]=$1;
  number[$1]=coun;
}
/Education\.* * .+ * Happiness\.* .+/ {
  edu[coun] = substr($0,20,6);
  hap[coun] = substr($0,50,6);
  next;
}
/Technology\.* *.+ * Research\.*.+/ {
  tech[coun] = substr($0,20,6);
  res[coun] = substr($0,50,6);
  next;
}


/^ *# *name *tech *research *education *status/ { mode="report"; stat=1; next; }
/^.* Diplomatic Relations Report/ { mode="relations"; stat=1; next; }

{
  if (mode=="report") {
    if (NF>5) {
      name=truncwhite(substr($0,8,14));
      if (!($1 in country)) {
	country[$1]=name;
	number[name]=$1;
      }
      if (coun in tech) {
	if (!($1 in maxtech)) maxtech[$1]=9999;
	rel=truncwhite(substr($0,23,10));
	if (rel=="similar") {
	  maxtech[$1]=min(maxtech[$1],min(tech[coun]*2+1,tech[coun]+50));
	  mintech[$1]=max(mintech[$1],max((tech[coun]-1)/2,tech[coun]-50));
	} else if (rel=="backward") {
	  maxtech[$1]=min(maxtech[$1],max((tech[coun]-1)/2,tech[coun]-50));
	  mintech[$1]=max(mintech[$1],min((tech[coun]-9)/10,tech[coun]-300));
	} else if (rel=="advanced") {
	  maxtech[$1]=min(maxtech[$1],min(tech[coun]*10+9,tech[coun]+300));
	  mintech[$1]=max(mintech[$1],min(tech[coun]*2+1,tech[coun]+50));
	} else if (rel=="primitive") {
	  maxtech[$1]=min(maxtech[$1],max((tech[coun]-9)/10,tech[coun]-300));
	} else if (rel=="superior") {
	  mintech[$1]=max(mintech[$1],min(tech[coun]*10+9,tech[coun]+300));
	}
      }
      if (coun in res) {
	if (!($1 in maxres)) maxres[$1]=9999;
	rel=truncwhite(substr($0,35,10));
	if (rel=="similar") {
	  maxres[$1]=min(maxres[$1],min(res[coun]*2+1,res[coun]+50));
	  minres[$1]=max(minres[$1],max((res[coun]-1)/2,res[coun]-50));
	} else if (rel=="backward") {
	  maxres[$1]=min(maxres[$1],max((res[coun]-1)/2,res[coun]-50));
	  minres[$1]=max(minres[$1],min((res[coun]-9)/10,res[coun]-300));
	} else if (rel=="advanced") {
	  maxres[$1]=min(maxres[$1],min(res[coun]*10+9,res[coun]+300));
	  minres[$1]=max(minres[$1],min(res[coun]*2+1,res[coun]+50));
	} else if (rel=="primitive") {
	  maxres[$1]=min(maxres[$1],max((res[coun]-9)/10,res[coun]-300));
	} else if (rel=="superior") {
	  minres[$1]=max(minres[$1],min(res[coun]*10+9,res[coun]+300));
	}
      }
      if (coun in edu) {
	if (!($1 in maxedu)) maxedu[$1]=9999;
	rel=truncwhite(substr($0,47,10));
	if (rel=="similar") {
	  maxedu[$1]=min(maxedu[$1],min(edu[coun]*2+1,edu[coun]+50));
	  minedu[$1]=max(minedu[$1],max((edu[coun]-1)/2,edu[coun]-50));
	} else if (rel=="backward") {
	  maxedu[$1]=min(maxedu[$1],max((edu[coun]-1)/2,edu[coun]-50));
	  minedu[$1]=max(minedu[$1],min((edu[coun]-9)/10,edu[coun]-300));
	} else if (rel=="advanced") {
	  maxedu[$1]=min(maxedu[$1],min(edu[coun]*10+9,edu[coun]+300));
	  minedu[$1]=max(minedu[$1],min(edu[coun]*2+1,edu[coun]+50));
	} else if (rel=="primitive") {
	  maxedu[$1]=min(maxedu[$1],max((edu[coun]-9)/10,edu[coun]-300));
	} else if (rel=="superior") {
	  minedu[$1]=max(minedu[$1],min(edu[coun]*10+9,edu[coun]+300));
	}
      }
      next;
    } else mode="";
  } else if (mode=="relations") {
    if (stat<3) {
      stat++;
      next;
    }
    if (stat==3) {
      if (NF==4) {
	if (!($2 in number)) {
	  number[$2]=substr($1,1,length($1)-1);
	  country[number[$2]]=$2;
	  next;
	}
      } else mode="";
    }
  } else {
    mode="";
    stat=0;
    next;
  }

}




END {
  
  printf("country             tech       research       education\n");
  for (coun in maxtech) {
    printf("%-14s %6.2f-%6.2f  %6.2f-%6.2f  %6.2f-%6.2f\n",country[coun],
	   mintech[coun],maxtech[coun],
	   minres[coun],maxres[coun],
	   minedu[coun],maxedu[coun]);
  }

}
