#!/bin/awk -f

#	Scan a produce file to locate commodity shortages and to	#
#	generate a production summary.					#


BEGIN	{
	header	= 0
	first	= 1

	# For each commodity, total up the actual production, and the	#
	# maximum (ie., that assuming the same populations but enough	#
	# input materials), and similarly the actual and maximum	#
	# consumption.							#
	prod_H = maxp_H = 0
	prod_f = maxp_f = 0
	prod_u = maxp_u = 0
	prod_p = maxp_p = 0
	prod_g = maxp_g = 0
	prod_s = maxp_s = 0
	prod_t = maxp_t = 0
	prod_e = maxp_e = 0
	prod_a = maxp_a = 0
	prod_i = maxp_i = used_i = maxu_i = 0
	prod_o = maxp_o = used_o = maxu_o = 0
	prod_d = maxp_d = used_d = maxu_d = 0
	prod_l = maxp_l = used_l = maxu_l = 0
	prod_h = maxp_h = used_h = maxu_h = 0
	prod_perc = 0
	cost   = 0
	}
{
	if (first && (substr("production", 1, length($1)) != $1))
	{	print	"produce: input is not a production file"
		exit	1
	}

	first	= 0

	if ((length($2) == 1) && (substr($3, length($3), 1) == "%"))
	{
		# Interesting lines have a sector designation in the	#
		# second field, and the third field ends with a percent	#
		# character						#

		use1 = use2 = use3 = 0
		max1 = max2 = max3 = 0


		if	($2 == "m")
		{	# Iron mines use nothing at all and is free.	#
			prod_i	= prod_i + $5
			maxp_i	= maxp_i + $9
		}
		else if	($2 == "u")
		{	# uraniums mines cost money, but we are not     #
			# gonna worry.                                  #
			prod_u	= prod_u + $5
			maxp_u	= maxp_u + $9
		}
		else if	($2 == "o")
		{	# Oil well use nothing at all and is free.	#
			prod_o	= prod_o + $5
			maxp_o	= maxp_o + $9
		}
		else if ($2 == "=")
		{	# Ditto bridges  				#
			prod_f	= prod_f + $5
			maxp_f	= maxp_f + $9
		}
		else if ($2 == "a")
		{	# Ditto agribusinesses				#
			prod_f	= prod_f + $5
			maxp_f	= maxp_f + $9
		}
		else if ($2 == "g")
		{	# Gold mines use gold ore but this doesn't	#
			# show up on the production report		#
			prod_d	= prod_d + $5
			maxp_d	= maxp_d + $9
		}
		else if ($2 == "i")
		{	# gunrunin' people...                          #
			use1	= substr ($9,  1, length($9 )-1)
			use2	= substr ($10, 1, length($10)-1)
			max1	= substr ($12, 1, length($11)-1)
			max2	= substr ($13, 1, length($12)-1)
			typ1	= "lcm"
			typ2	= "hcm"
			prod_s	= prod_s + $5
			maxp_s	= maxp_s + $13
			used_l  = used_l + use1
			maxu_l	= maxu_l + max1
			used_h  = used_h + use2
			maxu_h	= maxu_h + max2
		}
		else if ($2 == "t")
		{	# techie people...                           #
			use1	= substr ($9,  1, length($9 )-1)
			use2	= substr ($10, 1, length($10)-1)
			use3	= substr ($11, 1, length($11)-1)
			max1	= substr ($12, 1, length($12)-1)
			max2	= substr ($13, 1, length($13)-1)
			max3	= substr ($14, 1, length($14)-1)
			typ1	= "dust"
			typ2	= "oil"
			typ3	= "lcm"
			prod_t	= prod_t + $5
			maxp_t	= maxp_t + $15
			used_d  = used_d + use1
			maxu_d	= maxu_d + max1
			used_o  = used_o + use2
			maxu_o	= maxu_o + max2
			used_l  = used_l + use3
			maxu_l	= maxu_l + max3
		}
		else if ($2 == "d")
		{	# defense people...                           #
			use1	= substr ($9,  1, length($9 )-1)
			use2	= substr ($10, 1, length($10)-1)
			use3	= substr ($11, 1, length($11)-1)
			max1	= substr ($12, 1, length($12)-1)
			max2	= substr ($13, 1, length($13)-1)
			max3	= substr ($14, 1, length($14)-1)
			typ1	= "oil"
			typ2	= "lcm"
			typ3	= "hcm"
			prod_g	= prod_g + $5
			maxp_g	= maxp_g + $15
			used_o  = used_o + use1
			maxu_o	= maxu_o + max1
			used_l  = used_l + use2
			maxu_l	= maxu_l + max2
			used_h  = used_h + use3
			maxu_h	= maxu_h + max3
		}
		else if ($2 == "b")
		{
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "dust"
			prod_b	= prod_b + $5
			maxp_b	= maxp_b + $11
			used_d  = used_d + use1
			maxu_d	= maxu_d + max1
		}
		else if ($2 == "%")
		{
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "oil"
			prod_p	= prod_p + $5
			maxp_p	= maxp_p + $11
			used_o  = used_o + use1
			maxu_o	= maxu_o + max1
		}
		else if ($2 == "j")
		{	# Light industry uses iron and nothing but iron	#
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "iron"
			prod_l	= prod_l + $5
			maxp_l	= maxp_l + $11
			used_i  = used_i + use1
			maxu_i	= maxu_i + max1
		}
		else if ($2 == "k")
		{	# Light industry uses iron and nothing but iron	#
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "iron"
			prod_h	= prod_h + $5
			maxp_h	= maxp_h + $11
			used_i  = used_i + use1
			maxu_i	= maxu_i + max1
		}
		else if ($2 == "p")
		{	# Consume lcm's and money to produce happy's	#
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "lcm"
			prod_H	= prod_H + $5
			maxp_H	= maxp_H + $11
			used_l  = used_l + use1
			maxu_l	= maxu_l + max1
		}
		else if ($2 == "l")
		{	# Consume lcm's and money to produce edu's	#
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "lcm"
			prod_e	= prod_e + $5
			maxp_e	= maxp_e + $11
			used_l  = used_l + use1
			maxu_l	= maxu_l + max1
		}
		else if ($2 == "e")
		{	# Enlistment centres consume civilians		#
			use1	= substr ($9,  1, length($9 )-1)
			max1	= substr ($10, 1, length($10)-1)
			typ1	= "civ"
			prod_a	= prod_a + $5
			maxp_a	= maxp_a + $11
		}

		if ((header == 0) && (use1 < max1 || use2 < max2 || use3 < max3))
		{
			print	"sect      des : product :  use  max"
			header	= 1
		}

		# Check each of the three possible input commodities	#
		# and warn about any possible shortages.		#
		if (use1 < max1)
			printf	("%-9s  %1s  : %-7s : %4d %4d\n", $1, $2, typ1, use1, max1) ;
		if (use2 < max2)
			printf	("%-9s  %1s  : %-7s : %4d %4d\n", $1, $2, typ2, use2, max2) ;
		if (use3 < max3)
			printf	("%-9s  %1s  : %-7s : %4d %4d\n", $1, $2, typ3, use3, max3) ;
	}
}

END	{
	print	""
	print	""
	print	"commodity   production       consumption"
	print	"            actual    max    actual    max"

	if ( maxp_f != 0 ) {
	printf	("%-9s   %6d %6d           \n", "food", prod_f, maxp_f)
	}
	if ( maxp_i != 0 ) {
	printf	("%-9s   %6d %6d    %6d %6d\n", "iron", prod_i, maxp_i, used_i, maxu_i)
	}
	if ( maxp_d != 0 ) {
	printf	("%-9s   %6d %6d    %6d %6d\n", "dust", prod_d, maxp_d, used_d, maxu_d)
	}
	if ( maxp_o != 0 ) {
	printf	("%-9s   %6d %6d    %6d %6d\n", "oil", prod_o, maxp_o, used_o, maxu_o)
	}
	if ( maxp_l != 0 ) {
	printf	("%-9s   %6d %6d    %6d %6d\n", "lcm",  prod_l, maxp_l, used_l, maxu_l)
	}
	if ( maxp_h != 0 ) {
	printf	("%-9s   %6d %6d    %6d %6d\n", "hcm",  prod_h, maxp_h, used_h, maxu_h)
	}
	if ( maxp_u != 0 ) {
	printf	("%-9s   %6d %6d           \n", "ura",  prod_u, maxp_u)
	}
	if ( maxp_g != 0 ) {
	printf	("%-9s   %6d %6d           \n", "gun",  prod_g, maxp_g)
	}
	if ( maxp_s != 0 ) {
	printf	("%-9s   %6d %6d           \n", "shell",  prod_s, maxp_s)
	}
	if ( maxp_p != 0 ) {
	printf	("%-9s   %6d %6d           \n", "petro",  prod_p, maxp_p)
	}
	if ( maxp_b != 0 ) {
	printf	("%-9s   %6d %6d           \n", "bars",  prod_b, maxp_b)
	}
	if ( maxp_t != 0 ) {
	printf	("%-9s   %6d %6d           \n", "tech",  prod_t, maxp_t)
	}
	if ( maxp_H != 0 ) {
	printf	("%-9s   %6d %6d           \n", "happy",  prod_H, maxp_H)
	}
	if ( maxp_e != 0 ) {
	printf	("%-9s   %6d %6d           \n", "edu",  prod_e, maxp_e)
	}
	if ( maxp_a != 0 ) {
	printf	("%-9s   %6d %6d           \n", "mil",  prod_a, maxp_a)
	}
	maxp_total =maxp_H + maxp_u + maxp_g + maxp_s + maxp_t + maxp_a + maxp_i + maxp_o + maxp_d + maxp_l + maxp_h
	prod_total =prod_H + prod_u + prod_g + prod_s + prod_t + prod_a + prod_i + prod_o + prod_d + prod_l + prod_h
	if ( prod_total != 0 ) {
	prod_perc = prod_total * 100 / maxp_total 
	}
	printf ("\nProduction percentage, minus aggie, mil, & petro - %.2f%%\n",prod_perc)
	}
