#!/bin/csh -f

# calculate the total resources of a country.  And make some faulty
# predictions about what is going to happen.

set census = newcen			# output of census
set commod = newcom			# output of commodity
set prod =   produce			# output of producuction

# -1,-3  r 100%  684 0.00 medical 0.38 $0     0d   0o   0l    0d   0o   0l 0.00
#  1     2   3    4   5     6      7    8      9   10   11    12   13   14  15
awk '\
$6=="lcm" { pelo = $7 }\
$6=="oil" { pelo = $7 }\
$6=="tech" { petr = $7 }\
$6=="medical" { petr = $7 }\
$6=="guns" { petd = $7 }\
$6=="shells" { petd = $7 }\
$6=="petrol" { petg = $7 }\
END	{\
	print 0+pelo\
	print 0+petr\
	print 0+petd\
	print 0+petg\
	}\
' $prod > tmp

set pe = (`cat tmp`)
\rm tmp

# pe[1] = oil/lcm prodcution ratio
# pe[2] = tech/res production ratio

#  sect      eff mob  uf  uf   civ mil  uw  food  min gold fert oil uran work
#-17,-9  #    3% 127  ..  ..     1   0   0     0    0    0   76   0    0 100%
#   1    2     3   4   5  6     7   8    9   10    11   12   13  14  15   16
awk '\
BEGIN{	ip=0;op=0;dp=0;lp=0;tp=0;rp=0\
	ep=0;hp=0;bp=0;Hp=0;Sp=0;Gp=0;Pp=0}\
NF==16{	civ+=$7\
    mil+=$8\
    uw+=$9\
    fud+=$10\
    if($2!="^"){\
	if($11>50)min+=$11\
	if($12>30)gold+=$12\
	if($14>30)oil+=$14\
	if($15>5)uran+=$15\
    }\
    eff=eff+substr($3,1,length($3)-1)\
}\
{	w = ($7+$9+2*$8/5) }\
$2=="m"	{ip+=$11*w/100*.08}\
$2=="o"	{op+=$14*w/100*.08}\
$2=="g"	{dp+=$12*w/100*.08}\
$2=="j"	{lp+=(w*.08)}\
$2=="t"	{tp+=(w*.08)}\
$2=="r"	{rp+=(w*.08)}\
$2=="l"	{ep+=(w*.08)}\
$2=="p"	{hp+=(w*.08)}\
$2=="b"	{bp+=(w*.08)}\
$2=="k"	{Hp+=(w*.08)}\
$2=="d"	{Gp+=(w*.08)}\
$2=="i"	{Sp+=(w*.08)}\
$2=="%"	{Pp+=(w*.08)}\
END	{\
	print "civ:	"civ\
	print "mil:	"mil\
	print "uw:	"uw\
	print "eff:	"eff\
	print "gold:	"gold\
	print "min:	"min\
	print "oil:	"oil\
	print "uran:	"uran\
	print "food:	"fud\
print ip " "op" "dp" "lp" "tp" "rp" "ep" "hp" "bp" "Hp" "Gp" "Sp" "Pp>"tmpp"\
}'	$census

set makes = `cat tmpp`
\rm tmpp
awk '\
NF==14{	shells+=$5\
	guns+=$6\
	petrol+=$7\
	iron+=$8\
	dust+=$9\
	bars+=$10\
	crude+=$11\
	lcm+=$12\
	hcm+=$13\
	rads+=$14\
	}\
END	{\
	print "shells:  "shells\
	print "guns:    "guns\
	print "petrol:  "petrol\
	print "iron:    "iron\
	print "dust:    "dust\
	print "bars:    "bars\
	print "crude:   "crude\
	print "lcm:     "lcm\
	print "hcm:     "hcm\
	print "rads:    "rads\
}'	$commod

#   #used what p.e 	wkfs   USED: iron lcm   oil   dust hcm   COST
#    1   2    3          4             5   6      7 	 8  9     10
#
echo 1  iron 1.0	$makes[1] 	1  0	  0	 0  0	  0	>tmp
echo 1  oil  $pe[1]	$makes[2] 	0  0	  $pe[1] 0  0	  0	>>tmp
echo 1  dust 1.0	$makes[3] 	0  0	  0	 1  0	  0	>>tmp
echo 1  lcm  $pe[1]	$makes[4] 	-1 $pe[1] 0	 0  0	  0	>>tmp
echo 16 tech $pe[2]	$makes[5] 	0  -10	  -5	 -1 0	  100	>>tmp
echo 16 med $pe[2]	$makes[6] 	0  -10	  -5	 -1 0	  100	>>tmp
echo 1  edu 1.0		$makes[7] 	0  -1	  0	 0  0	  10	>>tmp
echo 1  hap 1.0		$makes[8] 	0  -1	  0	 0  0     10	>>tmp
echo 5  bars 1.0	$makes[9] 	0  0	  0	 -5 0     30	>>tmp
echo 2  hcm  $pe[1]	$makes[10] 	-2 0	  0	 0 $pe[1] 0	>>tmp
echo 3  shells $pe[3]	$makes[11] 	0  -2	  0	 0 -1	  3	>>tmp
echo 16  guns $pe[3]	$makes[12] 	0  -5	  -1	 0 -10	  30	>>tmp
echo .1 petrol $pe[4]	$makes[13] 	0  0	  -.1	 0  0	  1	>>tmp

awk '{x = $3*$4/$1 ; print "makes "$2":	"x"	$"x*$10\
	iron= iron+$5*$4\
	lcm = lcm +$4*$6/$1\
	oil = oil +$4*$7/$1\
	dust = dust + $4*$8/$1\
	hcm = hcm + $4*$9/$1\
	money = money+x*$10\
	}\
END { print "delta iron: 	"iron\
      print "delta lcm:  	"lcm\
      print "delta dust: 	"dust\
      print "delta oil:  	"oil\
      print "delta hcm:  	"hcm\
      print "costing...:	$"money\
      }' < tmp

\rm tmp

