#!/usr/local/bin/perl
#!/apps/perl/PRO/bin/perl
#!/apps/perl/bin/perl

#########################################################################################
# This is the download of run script for the PrimEx DAQ system. It is run during the
# CODA "download" transition. The primary function is to insert the module map for
# each crate into the database for use in offline (and online) analysis.
# 
# 8/13/02  D. Lawrence
#########################################################################################

$msql_host = clon10;
$mysql_host = clas-sc0;
$mysql_user = primex_user;

### perl modules built for PrimEx use on clon cluster
#use lib '/usr/local/primex/online/scripts/perlmod/lib/site_perl/5.005/sun4-solaris';

use Pezca;
#use DBI ();

### Connect to both the msql and the mysql databases
#$dbh_msql = DBI->connect('DBI:mSQL:primex:clon10.jlab.org', primex,'') or die $DBI::errstr;
#$dbh_mysql = DBI->connect('DBI:mysql:primex_online:clas-sc0', primex,'') or die $DBI::errstr;

# Geez, it's ridiculous what we havce to do to get the run number
$sql = "SELECT runNumber FROM sessions WHERE name='primex'\\g";
$result = `echo \"$sql\" | msql -h clon10 primex | grep \"|\" | grep -v runNumber`;
@tmp = split(/\s+/, $result);
$runNumber =  $tmp[2];


exit();

########################## Collect values to insert in database ####################
### Get Current run number
($runNumber) = $dbh_msql->selectrow_array("SELECT runNumber FROM sessions");

### Read ROC maps and insert values into database
&ReadROCMap(1);
&InsertToDB;
&ReadROCMap(15);
&InsertToDB;



#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SUBROUTINES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#### This subroutine inserts the values in @modules into the ROC_Map table
#### of the database. It assumes $roc_id is defined (it should be in 
#### ReadROCMap() below) and that $runNumber is valid.
sub InsertToDB{

	$length = @modules;
	$length--;	# I'm not sure why $length is 1 unit too long, but it is.
	for($i=0; $i<($length/2); $i++)
	{
		$name	= pop(@modules);
		$slot	= pop(@modules);

		$query  = "INSERT INTO ROC_Map(Run_Number,roc_id,slot,type,Time) ";
		$query .= "VALUES($runNumber,$roc_id,$slot,'$name',NOW())";
		$dbh_mysql->do($query) or die $DBI::errstr;
	}
}

### Read the values written to the ROC map file by the ROC at download time.
sub ReadROCMap{

	$roc_id = $_[0];

	@modules = "";

	open(FILE, "/usr/local/clas/parms/feconf/rocmap/primex/ROC".$roc_id.".hardmap");
	$unused = <FILE>; # first two lines have column lables
	$unused = <FILE>;
	while($line = <FILE>){ # Loop over lines
		@v = split(/\s+/,$line);
		#$nvals = @v;
		#print "nvals= $nvals\n";
		#for($i=0;$i<$nvals;$i++){print "v[$i] = $v[$i]\n";}
		$slot = $v[1];
		if ($v[2]==1){$name = "ADC1881";}
		if ($v[3]==1){$name = "TDC1872";}
		if ($v[4]==1){$name = "TDC1875";}
		if ($v[5]==1){$name = "TDC1877";}
		if ($v[6]==1){$name = "TDC1877S";}
		if ($v[7]==1){$name = "Unknown";}
		if ($v[8]==1){$name = "Empty";}

		if($name ne "Empty"){push(@modules,$slot,$name);}
	}
	close(FILE);

	#print "modules for roc ID $roc_id :\n @modules\n\n";
}
