#!d:\perl\bin\perl.exe

use DBI;
use DBD::mysql;

$db=DBI->connect('DBI:mysql:plum_data');

$runidx=$ARGV[0];
($run)=fetchtable('select * from runs where idx='.$runidx);
$tblname=$run->{tblname};
$min=$run->{min};
$max=$run->{max};
$db->disconnect();
$order=$run->{numcombos};

$db=DBI->connect('DBI:mysql:plum_output');

#####################################################################################################
 
@MTL=findxcds('MTL_simul_xcd');
 $xcds{MTL}=\@MTL;

@LTL=findxcds('LTL_simul_xcd');
 $xcds{LTL}=\@LTL;

@keepalive=findxcds('keepalive_xcd');
 $xcds{keepalive}=\@keepalive;

@acoustics=findxcds('acoustics_xcd_simul');
 $xcds{acoustics}=\@acoustics;
 
@any_exceeded=findxcds('any_exceeded_simul');
 $xcds{any_exceeded}=\@any_exceeded;

@downlink=findxcds('downlink_avg_xcd');
 $xcds{downlink}=\@downlink;

@uplink=findxcds('uplink_avg_xcd');
 $xcds{uplink}=\@uplink;

@downlink_peak=findxcds('downlink_peak_xcd');
 $xcds{downlink_peak}=\@downlink_peak;

@uplink_peak=findxcds('uplink_peak_xcd');
 $xcds{uplink_peak}=\@uplink_peak;

@power=findxcds('power_xcd');
 $xcds{power}=\@power;

#####################################################################################################
##open FOO,">log.txt";
##print FOO 'nums';
##for($k=0;$k!=$max;$k++){print FOO "\t$k"}
##print FOO "\n";
##
##print FOO 'trng';
##foreach(@crewtrng){print FOO "\t$_";}
##print FOO "\n";
##
##print FOO 'stow';
##foreach(@stowage){print FOO "\t$_";}
##print FOO "\n";
##
##print FOO 'mass';
##foreach(@upmass){print FOO "\t$_";}
##print FOO "\n";
##
##print FOO 'mids';
##foreach(@middecks){print FOO "\t$_";}
##print FOO "\n";
##close FOO;
#####################################################################################################

for($count=$min;$count!=$max+1;$count++)
 {
 open FILE,">d:\\plum\\docs\\output\\simul_".$tblname.'_count_'.$count.'.txt';

# print FILE "CrewMTLTot\tCrewLTLTot\tIncAcousticsTot\tCrewAcousticsTot\tCrewEnergyTot\n";
 print FILE "Series_Series";
 print FILE "\tdownlink";
 print FILE "\tpower";
 print FILE "\tkeepalive";
 print FILE "\tMTL";
 print FILE "\tLTL";
 print FILE "\tuplink";
 print FILE "\tuplink peak";
 print FILE "\tdownlink peak";
 print FILE "\tacoustics";
 print FILE "\tany";
 print FILE "\n";

 print FILE "ceeds Avail. Resources";
 print FILE "\t".($xcds{downlink}->[$count]);
 print FILE "\t".($xcds{power}->[$count]);
 print FILE "\t".($xcds{keepalive}->[$count]);
 print FILE "\t".($xcds{MTL}->[$count]);
 print FILE "\t".($xcds{LTL}->[$count]);
 print FILE "\t".($xcds{uplink}->[$count]);
 print FILE "\t".($xcds{uplink_peak}->[$count]);
 print FILE "\t".($xcds{downlink_peak}->[$count]);
 print FILE "\t".($xcds{acoustics}->[$count]);
 print FILE "\t".($xcds{any_exceeded}->[$count]);
 print FILE "\n";

 print FILE "Under 100% Utilization";
 print FILE "\t".($order-$xcds{downlink}->[$count]);
 print FILE "\t".($order-$xcds{power}->[$count]);
 print FILE "\t".($order-$xcds{keepalive}->[$count]);
 print FILE "\t".($order-$xcds{MTL}->[$count]);
 print FILE "\t".($order-$xcds{LTL}->[$count]);
 print FILE "\t".($order-$xcds{uplink}->[$count]);
 print FILE "\t".($order-$xcds{uplink_peak}->[$count]);
 print FILE "\t".($order-$xcds{downlink_peak}->[$count]);
 print FILE "\t".($order-$xcds{acoustics}->[$count]);
 print FILE "\t".($order-$xcds{any_exceeded}->[$count]);

 close FILE;
 }

$db->disconnect();

###############################################
# Begin calculation funcs #####################
###############################################

sub findxcds{
	my @xcds;
	my $fieldname;
	$fieldname=shift;
	my @table;
	@table=fetchtable('select count(idx) as numexceeded,selectednum from '.$tblname.' where '.$fieldname.'=1 group by selectednum order by selectednum');
	if(@table == 0)
	 {
	 for($n=0;$n!=$max+1;$n++)
	  {
	  $table[$n]={selectednum=>$n,numexceeded=>0};
	  }
	 }
	foreach(@table)
	 {
	 $xcds[$_->{selectednum}]=$_->{numexceeded};
	 }
	foreach(@xcds)
	 {
	 if($_=='')
	  {
	  $_=0;
	  }
	 }
	
	return @xcds;
}

###############################################
# Begin library funcs #########################
###############################################

sub sql_query{
	if($_[0] eq '')
	 {
	 return;
	 }
	if($_[1] eq '')
	 {
	 $_[1]=$db;
	 }
	my $rst=$_[1]->prepare($_[0]);
	$rst->execute();
	return $rst;
}

sub fetchrow{
	my $row=$_[0]->fetchrow_hashref();
	return $row;
}

sub fetch_hash{
	my $row=$_[0]->fetchrow_hashref();
	return %{$row};
}

sub fetchtable{
	my $table=$_[0];
	my $rst=sql_query($table);
	my @rows;
	while($row=fetchrow($rst))
	 {
	 push @rows,$row;
	 }
	$rst->finish();
	return @rows;
}
