#!/bin/awk -f
#	Filter a dump file to locate shortages of food.			#

BEGIN	{
	civ	= 0
	mil	= 0
	uw	= 0
	food	= 0
	first	= 1
	}

{
	if (first && (substr("dump", 1, length($1)) != $1))
	{	print	"shortage: input is not a dump file"
		exit	1
	}
	first	= 0
	#if ((NF > 26) && ($1 != "x") && (match($4, "[A-Z]") == 0))
	if ((NF > 26) && ($1 != "x"))
	{
		# Check for food shortages. According to the "version"	#
		# command, 1000 people eat 1 unit of food each time	#
		# unit, that is, 8 units per update.			#
		civ	= $16
		mil	= $17
		uw	= $18
		food	= $19

		if (civ + mil + uw <= 2)
			# Almost nobody there. This never seems to	#
			# give rise to any problems.			#
			continue

		if (((civ + mil + uw) * 8) > (food * 1000))
		{	# Not enough to last even the next update. Oh	#
			# dear, starvation iminent			#
			printf	("%d,%d\t: pop=%5d food=%5d (!!not enough!!)\n", $1, $2, civ + mil + uw, food)
			printf	("%4d,%d %d\n", $1, $2, 2) >> "special"
		}
		else if (((civ + mil + uw) * 8) > (food * 1000 / 10))
		{	# Insufficient to last 5 days at 2 updates per	#
			# day. Issue a warning about this.		#
			printf	("%d,%d\t: pop=%5d food=%5d (%d days)\n", $1, $2, civ + mil + uw, food, (food * 1000) / ((civ + mil + uw) * 16))
			printf	("%4d,%d %d\n", $1, $2, 1) >> "special"
		}

	}
}
