
# parse the crossfire -m8 output

BEGIN {
	crosscmd = crosscmd " 2>&1";
	god = 0;
	while (crosscmd | getline buff == 1) {
	   if(buff ~ /GOD/) { god++; gift[god] =0; name[god] = get_entry(buff,2," "); }
	   if(!god) continue;
	   if(buff ~ /enemy:/) enemy[god] = get_entry(buff,2," ");
	   if(buff ~ /aligned/) aligned[god] = get_entry(buff,2," ");
	   if(buff ~ /enemy_race/) erace[god] = get_entry(buff,2," ");
	   if(buff ~ /servant/) servant[god] = get_entry(get_entry(get_entry(buff,2,": "),1,")"),2"(");
	   if(buff ~ /Attacks:/) atype[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Immune:/) immune[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Prot:/) prot[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Vuln:/) vuln[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Attuned:/) attuned[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Repelled:/) repelled[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Denied:/) denied[god] = get_entry(get_entry(buff,2,": "),1,")");
	   if(buff ~ /Desc:/) desc[god] = get_entry(get_entry(buff,2,": "),1,",");
	   if(buff ~ /Priest/ || gift[god]) {
	        if(!buff) continue;
		special[god,gift[god]++] = buff;
  	   }
	   # printf(" %s \n", buff);
	}
	close(crosscmd);
	for(i=1;i<=god;i++) {
	  printf("\{\\bf %s \} & %s \\\\\\hline", name[i], capitalize(desc[i]));
	  if(enemy[i]) 		printf("\nEnemy cult: & %s \\\\ ", enemy[i]);
	  # if(servant[i])	printf("Servant: & %s \\\\ \n ", servant[i]);
	  if(aligned[i]) 	prt_var("Aligned race(s): ",aligned[i],",",3);
	  if(erace[i]) 		prt_var("Enemy race(s): ",erace[i],",",3);
	  if(atype[i]) 		prt_var("Attacktype(s): ",atype[i],",",3);
	  if(immune[i]) 	prt_var("Immunity: ",immune[i],",",3);
	  if(prot[i]) 	 	prt_var("Protected: ",prot[i],",",3);
	  if(vuln[i]) 		prt_var("Vulnerable: ",vuln[i],",",3);
	  if(attuned[i])  	prt_var("Attuned: ",attuned[i],",",3);
	  if(repelled[i])  	prt_var("Repelled: ",repelled[i],",",3);
	  if(denied[i]) 	prt_var("Denied: ",denied[i],",",3);
	  for(j=1;j<gift[i];j++) {
	    if(j==1) printf("Added gifts/limits: & \\\\\n");
	    printf("& %s \\\\\n",special[i,j]);
	  }
	  if(i!=god) printf(" \\hline \n \\sngc\{ \} \\\\ \n \\sngc\{ \} \\\\ \\hline \n");
	  else printf(" \\hline \n");
	}
}

function prt_var (hstr,vstr,sep,len) {
  printf("%s & ",hstr); prtlong(vstr,sep,len);
}


function get_entry (str,start,sep) {
	nr = split(str, var, sep);
        for(k=start;k<=nr;k++) {
            if(var[k]==" ") continue;
	    break;
	}
	while(var[k] ~ /_/ ) { sub("_", " ", var[k]); }
  	return var[k];
}

function prtlong (str,sep,max) {
	nr = split(str, var, sep);
	printf("\n");
        for(l=1;l<=nr;l++) {
	    if(var[l] == "(null)") var[l] = "None";
	    if(l==nr) printf("%s \\\\",capitalize(var[l]));
	    else printf("%s,",capitalize(var[l]));
	    if(l!=1 && l!=nr && (l % max)==0) printf("\\\\ \n & ");
	}
}

function capitalize(str) {
        return toupper(substr(str, 1, 1)) substr(str, 2);
}
