BEGIN{
	#print ARGC;
	num_files = ARGC-1;
	for (i = 1; i <= num_files; i++){
		ARGV[ARGC] = ARGV[i];
		ARGC++;
	}
	#print ARGC;
	cweapon = 600;
	carmor = 200;
	cmonster = 2000;
	csite = 750;
	cnation = 72;
	cspell = 829;
	cnametype = 146;
	fout = "headers.txt";
	print "-- Mod combined by The Notorious Doctor Praetorious, Prince of Men's Fates, God of the Pole Star, Lord of Courage." >> fout;
}

(FNR == 1){
	tcspell = 829;
}

## First pass
/^#newweapon/ && (ARGIND <= num_files){
	wmap[ARGIND+num_files,$2] = cweapon;
	cweapon++;
}

/^#newarmor/ && (ARGIND <= num_files){
	amap[ARGIND+num_files,$2] = carmor;
	carmor++;
}

/^#newmonster/ && (ARGIND <= num_files){
	mmap[ARGIND+num_files,$2] = cmonster;
	cmonster++;
}

/^#newsite/ && (ARGIND <= num_files){
	smap[ARGIND+num_files,$2] = csite;
	csite++;
}

/^#selectnation/ && (ARGIND <= num_files){
	if ($2 < 72){
		nmap[ARGIND+num_files,$2] = $2;
	} else {
		nmap[ARGIND+num_files,$2] = cnation;
		cnation++;
	}
}

/^#newspell/ && (ARGIND <= num_files){
	spmap[ARGIND+num_files,tcspell] = cspell;
	cspell++;
	tcspell++;
}

/^#selectnametype/ && (ARGIND <= num_files){
	namemap[ARGIND+num_files,$2] = cnametype;
	cnametype++;
}

### Second pass
## Choose output file.
(FNR == 1){fout = "headers.txt";}

/^#newweapon/ || /^#selectweapon/{
	fout = "weapons.txt";
}

/^#newarmor/ || /^#selectarmor/{
	fout = "armors.txt";
}

/^#newmonster/ || /^#selectmonster/{
	fout = "monsters.txt";
}

/^#newsite/ || /^#selectsite/{
	fout = "sites.txt";
}

/^#selectspell/ || /^#newspell/{
	fout = "spells.txt";
}

/^#selectnation/{
	fout = "nations.txt";
}

/^#selectnametype/{
	fout = "names.txt";
}

## Process individual lines.
# By default, we're going to print the line unmolested.
{pmode = 2;}
#	cweapon = 600;
#	carmor = 200;
#	cmonster = 2000;
#	csite = 750;
#	cnation = 68;

# weapons
(ARGIND > num_files && $2+0 >= 600) && (/#^weapon/ || /#^newweapon/){
	print $1 " " wmap[ARGIND,$2] >> fout;
	pmode = 0;
}

# armor
(ARGIND > num_files  && $2+0 >= 200) && /^#newarmor/{
	print $1 " " amap[ARGIND,$2] >> fout;
	pmode = 0;
}

# monsters
# NOTE - if a spell does >=2000 damage, I'm going to assume that it is referencing a monster ID. That's the only way I can tell.
(ARGIND > num_files  && $2+0 >= 2000) && (/^#newmonster/ || /^#firstshape/ || /^#secondshape/ || /^#secondtmpshape/ || /^#shapechange/ || /^#homemon/ || /^#homecom/ || /^#addrecunit/ || /^#addreccom/ || /^#defcom/ || /^#defunit/ || /^#hero/ || /^#multihero/ || /^#startcom/ || /^#startscout/ || /^#startunittype/ || /^#damage/){
	print $1 " " mmap[ARGIND,$2] >> fout;
	print "-- was " $2 >> fout;
	pmode = 0;
}

# nations
(ARGIND > num_files  && $2+0 >= 68) && (/^#selectnation/ || /^#restrictedgod/){
	print $1 " " nmap[ARGIND,$2] >> fout;
	pmode = 0;
}

# sites
(ARGIND > num_files  && $2+0 >= 750) && (/^#startsite/ || /^#newsite/){
	print $1 " " smap[ARGIND,$2] >> fout;
	pmode = 0;
}

# spells
(ARGIND > num_files && $2 + 0 >= 829) && (/^#onebattlespell/ || /^#nextspell/){
	print $1 " " spmap[ARGIND,$2] >> fout;
	pmode = 0;
}

#name
(ARGIND > num_files && namemap[ARGIND,$2] != "") && (/^#selectnametype/ || /^#nametype/){
	print $1 " " namemap[ARGIND,$2] >> fout;
	print "-- was " $2 >> fout;
	pmode = 0;
}

# sprites
(ARGIND > num_files) && (/^#spr1/ || /^#spr2/ || /^#indepflag/ || /^#flag/ || /^#icon/){
	j = split ($2,fname,"/");
	oldname = $2;
	newname = "./megamod/" tolower(fname[j]);
	seen[newname]++;
	if (seen[newname] > 1){newname = "./megamod/" seen[newname] "_" tolower(fname[j]);}
	command = "cp " oldname " \"" newname;
	system (command);
	print $1 " \"" newname >> fout;
	pmode = 0;
}

# If we didn't have to map anything on this line, we just spit it out.
(pmode == 2 && ARGIND > num_files){print >> fout;}


# End lines indicate that the *next* line may be filler, but we still need teh end line in the correct file.
/^#end/{print "" >> fout; fout = "footers.txt";}
