#!/bin/sh

validate '
BEGIN {
	henear = "henear.db"
	henear_init = 0

	fixedpi = "FixedPI.db"
	fixedpi_init = 0

	fixedpr = "FixedPR.db"
	fixedpr_init = 0

	Standard = "(SAO.*)|(AGK.*)|(M31)|(M32)|(HD.*)|(FEIGE.*)|(HZ..)"
}

{ FixedPI = 0
  FixedPR = 0 }

TITLE ~ "henear" { 
	if ( henear_init == 0 ) {
		print Header > henear
		print Dashes > henear
	}
	print > "henear.db" 

	henear_init = 1
	next
}

# Fix up the PI
#
toupper(PI) ~ /^ALL([ \t]|$)/	{ PI="Calib";		FixedPI = 1; PROGRAM = 0 }
toupper(PI) ~ "CALIB"		{ PI="Calib";		FixedPI = 1; PROGRAM = 0 }

toupper(PI) ~ "CFA RED"		{ PI="JHuchra";		FixedPI = 1; PROGRAM = 1 }


toupper(PI) ~ "ASTRO"      { PI = "ASTRO"; 		FixedPI = 1 }
toupper(PI) ~ "BARMBY"     { PI = "PBarmby";            FixedPI = 1 }
toupper(PI) ~ "BARTON"     { PI	= "EBarton"; 		FixedPI = 1 }
toupper(PI) ~ "CAO?LDWE"   { PI	= "NCaldwell"; 		FixedPI = 1 }
toupper(PI) ~ "CALLAN"	   { PI	= "PCallanan"; 		FixedPI = 1 }
toupper(PI) ~ "CHALLIS"    { PI = "RKirshner"; 		FixedPI = 1 }
toupper(PI) ~ "DACOSTA"    { PI = "LDaCosta"; 		FixedPI = 1 }
toupper(PI) ~ "ELVIS"      { PI = "MElvis"; 		FixedPI = 1 }
toupper(PI) ~ "FANG"       { PI = "JHuchra"; 		FixedPI = 1 }
toupper(PI) ~ "FABRI"      { PI = "DFabricant"; 	FixedPI = 1 }
toupper(PI) ~ "GELL"       { PI = "MGeller";		FixedPI = 1 }
toupper(PI) ~ "GROGIN"     { PI = "NGrogin";		FixedPI = 1 }
toupper(PI) ~ "HAZ"        { PI = "GHazenberg";		FixedPI = 1 }
toupper(PI) ~ "HU?CH"      { PI = "JHuchra";		FixedPI = 1 }
toupper(PI) ~ "KIRSC?H"	   { PI = "RKirshner";		FixedPI = 1 }
toupper(PI) ~ "KORANY"	   { PI	= "DKoranyi"; 		FixedPI = 1 }
toupper(PI) ~ "KUHN"	   { PI	= "OKuhn"; 		FixedPI = 1 }
toupper(PI) ~ "DOBRZ"	   { PI	= "ADobrzycki";		FixedPI = 1 }
toupper(PI) ~ "KENYO"	   { PI	= "SKenyon"; 		FixedPI = 1 }
toupper(PI) ~ "LATH"	   { PI	= "DLatham"; 		FixedPI = 1 }
toupper(PI) ~ "LUU"	   { PI	= "JLuu"; 		FixedPI = 1 }
toupper(PI) ~ "STAUFF"	   { PI	= "JStauffer"; 		FixedPI = 1 }
toupper(PI) ~ "DELL"       { PI	= "IDellAntonio"; 	FixedPI = 1 }
toupper(PI) ~ "PROSSER"    { PI = "CProsser"; 		FixedPI = 1 }
toupper(PI) ~ "RAMELL"     { PI = "MRamella"; 		FixedPI = 1 }
toupper(PI) ~ "SCHACHTER"  { PI	= "JSchachter";		FixedPI = 1 }
toupper(PI) ~ "SCHLEGEL"   { PI	= "ESchlegel";		FixedPI = 1 }
toupper(PI) ~ "SEWARD"     { PI	= "FSeward"; 		FixedPI = 1 }
toupper(PI) ~ "MCDOWE"     { PI	= "JMcDowell"; 		FixedPI = 1 }
toupper(PI) ~ "MACKIE"     { PI	= "GMackie"; 		FixedPI = 1 }
toupper(PI) ~ "MAHDAV"     { PI	= "AMahdavi"; 		FixedPI = 1 }
toupper(PI) ~ "MOHR"       { PI	= "JMohr"; 		FixedPI = 1 }
toupper(PI) ~ "FALCO"      { PI	= "EFalco"; 		FixedPI = 1 }
toupper(PI) ~ "GREEN"      { PI	= "PGreen"; 		FixedPI = 1 }
toupper(PI) ~ "VESTERG"	   { PI	= "MVesterg"; 		FixedPI = 1 }
toupper(PI) ~ "VANCU"	   { PI	= "OVancura"; 		FixedPI = 1 }
toupper(PI) ~ "GOODMAN"	   { PI	= "AGoodman";		FixedPI = 1 }
toupper(PI) ~ "BRICEN"	   { PI	= "CBriceno"; 		FixedPI = 1 }
toupper(PI) ~ "BERLIND"	   { PI	= "PBerlind"; 		FixedPI = 1 }
toupper(PI) ~ "PLUCINSKY"  { PI	= "PPlucinsky";		FixedPI = 1 }
toupper(PI) ~ "GROGIN"	   { PI	= "NGrogin"; 		FixedPI = 1 }
toupper(PI) ~ "PANN?TOJA"  { PI	= "CPantoja";		FixedPI = 1 }
toupper(PI) ~ "MCCLIN"     { PI = "JMcClintock";	FixedPI = 1 }
toupper(PI) ~ "CURIEL"     { PI = "SCuriel"; 		FIxedPI = 1 }
toupper(PI) ~ "WILKE"      { PI	= "BWilkes"; 		FixedPI = 1 }
toupper(PI) ~ "WLIKE"      { PI	= "BWilkes"; 		FixedPI = 1 }
toupper(PI) ~ "WEEK"       { PI	= "TWeekes"; 		FixedPI = 1 }

toupper(PI) ~ "3C273"		{ PI = "MGeller";	FixedPI = 1; PROGRAM = 5 }

toupper(PROGRAM) ~ "TAURUS LOW MASS"	{ PI = "CBriceno"; 	FixedPI = 1; PROGRAM = 38 }

toupper(PI) ~ "KIR" && PROGRAM ~ "2"    { PI = "RKirshner";		FixedPI = 1 }

toupper(TITLE) ~ "^SKY"  		{ PI      = "Calib"
							FixedPI = 1
							PROGRAM = 0
							FixedPR = 1 }

toupper(TITLE) ~ "EVESKY"		{ PI = "Calib"
                                          FixedPI = 1
                                          PROGRAM = 0
                                          FixedPR = 1 }

FixedPI == 0 && PI ~ " *" && toupper(TITLE) ~ Standard {PI = "Calib"
							FixedPI = 1
							PROGRAM = 0
							FixedPR = 1
							}

toupper(PROGRAM) ~ "99" { PI = "JHuchra";		FixedPI = 1; PROGRAM = 99 }

toupper(PROGRAM) ~ "(KIRSH)|(CHALL)" {
			     PI = "RKirshner";		FixedPI = 1; PROGRAM = 2 }

  FixedPI != 1 {	print "Cant match PI for line " NR " : " PI " : "> "/dev/stderr" 
	if ( fixedpi_init == 0 ) {
  		print Header					> fixedpi
		print Dashes					> fixedpi
	}
	print $0						> fixedpi
	fixedpi_init = 1
  }


# Fix up the PROGRAM
#
toupper(PROGRAM) ~ "ASTRO" 			{ PROGRAM = 99; 	FixedPR = 1 }
toupper(PROGRAM) ~ "CALIB" 			{ PROGRAM =  0; 	FixedPR = 1 }
toupper(PROGRAM) ~ "REDSHIFT"	 		{ PROGRAM =  1; 	FixedPR = 1 }
toupper(PROGRAM) ~ "SYMBIO"	 		{ PROGRAM = 12; 	FixedPR = 1 }
toupper(PROGRAM) ~ "WARPFIRE"	 		{ PROGRAM = 17; 	FixedPR = 1 }
toupper(PROGRAM) ~ "^SN"	 		{ PROGRAM =  2; 	FixedPR = 1 }
toupper(PROGRAM) ~ "AGN[ _]MON"	 		{ PROGRAM =  6; 	FixedPR = 1 }
toupper(PROGRAM) ~ "CFA NEP SUR"	 	{ PROGRAM = 99; 	FixedPR = 1 }
toupper(PROGRAM) ~ "MISC"  && PI == "JHuchra" 	{ PROGRAM = 99; 	FixedPR = 1 }
toupper(PROGRAM) ~ "ROSAT" && PI == "JHuchra" 	{ PROGRAM = 99; 	FixedPR = 1 }
toupper(PROGRAM) ~ "ROSAT" && PI == "BWilkes" 	{ PROGRAM = 16; 	FixedPR = 1 }
toupper(PROGRAM) ~ "QSO"   && PI == "BWilkes" 	{ PROGRAM =  7; 	FixedPR = 1 }
toupper(PROGRAM) ~ "QSO"   && PI == "OKuhn" 	{ PROGRAM =  7; 	FixedPR = 1 }
toupper(PROGRAM) ~ "M67"   && PI == "NCaldwell"	{ PROGRAM = 10; 	FixedPR = 1 }
toupper(PROGRAM) ~ "CALDWELL" && PI == "NCaldwell"	{ PROGRAM = 10; 	FixedPR = 1 }

PROGRAM ~ /^[#- ]*[0-9]+/ {
	match(PROGRAM, /[0-9]+/)
	PROGRAM = substr(PROGRAM, RSTART, RLENGTH);	FixedPR = 1 
}

  FixedPR != 1 {	print "Cant match PROGRAM for line " NR " : " PROGRAM " : "> "/dev/stderr" 
	if ( fixedpr_init == 0 ) {
  		print Header					> fixedpr
		print Dashes					> fixedpr
	}
	print $0						> fixedpr
	fixedpr_init = 1
  }

FixedPI == 0 || FixedPR == 0 { next }
'