#!/usr/bin/env perl
#
# Jan 19, 2009  David Lawrence
#
# Create a set of XML files with a different length CDC.
#
# This script is needed because the number of places in the XML that must be
# changed in order to modify the length of the CDC are quite numerous. Since
# several lengths will be studied, it seemed to make sense to automate this
# process. See the file:
# https://halldsvn.jlab.org/repos/trunk/Studies/2009.01.CDC_FDC_geom/README
#
# For more details.
#
# This could probably be done more elegantly, but this way at least gets
# the job done. We take as a single argument, the new CDC length cm. The
# files CentralDC_HDDS.xml.tmpl, ForwardDC_HDDS.xml.tmpl and main_HDDS.xml.tmpl
# must all exist in the current working directory. These files are read in
# and the various strings are globally replaced in each with numbers calculated
# from the new CDC length.
#

if($#ARGV != 0) {
	printf("Please enter a CDC length in cm!\n");
	exit(-1);
}

$Lcdc = $ARGV[0];
$Lfdc = 357 - ($Lcdc+40.0);
$fdc1 = sprintf("%5.2f",($Lfdc -2*(4.5+3))/2);
$fdc2 = sprintf("%5.2f",$fdc1/3);

printf("Lcdc = $Lcdc cm (input value)\n");
printf("Lfdc = $Lfdc cm\n");
printf("fdc1 = $fdc1 cm\n");
printf("fdc2 = $fdc2 cm\n");
print "\n";

$main_a = sprintf("%4.1f",17+$Lcdc+23);

$cdc_a = sprintf("%4.1f",$Lcdc/2);
$cdc_b = sprintf("%+5.1f",$Lcdc+10.5);
$cdc_c = sprintf("%+5.2f",-($Lcdc/2+0.45));
$cdc_d = sprintf("%+5.2f",$Lcdc/2+0.20);
$cdc_e = sprintf("%5.2f",$Lcdc/2+0.5);
$cdc_f = sprintf("%5.2f",$Lcdc/2+0.92);
$cdc_g = sprintf("%5.1f",$Lcdc+24.0);
$cdc_h = sprintf("%5.1f",$Lcdc+22.0);
$cdc_i = sprintf("%5.1f",$Lcdc+2.0);
$cdc_j = sprintf("%5.1f",$Lcdc);
$cdc_k = sprintf("%5.2f",$Lcdc+2.84);
$cdc_l = sprintf("%5.2f",$Lcdc+2.83);
$cdc_m = sprintf("%5.2f",$Lcdc+0.83);
$cdc_n = sprintf("%5.1f",$Lcdc+2.5);
$cdc_o = sprintf("%+5.1f",$Lcdc+1.5);

$fdc_a = sprintf("%4.1f", $fdc1+4.5+3.0);
$fdc_b = sprintf("%4.1f", 2*($fdc1+4.5+3.0)+5);

# main_HDDS.xml
$ifname = "main_HDDS.xml.tmpl";
print "Reading from $ifname ...";
open(FILE, $ifname);
@lines = <FILE>;
close(FILE);
print "done\n";

$ofname = "main_HDDS_cdc$Lcdc.xml";
print "Writing $ofname ...";
open(FILE, ">$ofname");
foreach $s (@lines){
	$s =~ s/main_a/$main_a/g ;
	
	print FILE "$s";
}
close(FILE);
print "done\n";
print "\n";

# CentralDC_HDDS.xml
$ifname = "CentralDC_HDDS.xml.tmpl";
print "Reading from $ifname ...";
open(FILE, $ifname);
@lines = <FILE>;
close(FILE);
print "done\n";

$ofname = "CentralDC_HDDS_cdc$Lcdc.xml";
print "Writing $ofname ...";
open(FILE, ">$ofname");
foreach $s (@lines){
	$s =~ s/cdc_a/$cdc_a/g ;
	$s =~ s/cdc_b/$cdc_b/g ;
	$s =~ s/cdc_c/$cdc_c/g ;
	$s =~ s/cdc_d/$cdc_d/g ;
	$s =~ s/cdc_e/$cdc_e/g ;
	$s =~ s/cdc_f/$cdc_f/g ;
	$s =~ s/cdc_g/$cdc_g/g ;
	$s =~ s/cdc_h/$cdc_h/g ;
	$s =~ s/cdc_i/$cdc_i/g ;
	$s =~ s/cdc_j/$cdc_j/g ;
	$s =~ s/cdc_k/$cdc_k/g ;
	$s =~ s/cdc_l/$cdc_l/g ;
	$s =~ s/cdc_m/$cdc_m/g ;
	$s =~ s/cdc_n/$cdc_n/g ;
	$s =~ s/cdc_o/$cdc_o/g ;
	
	print FILE "$s";
}
close(FILE);
print "done\n";
print "\n";

# ForwardDC_HDDS.xml
$ifname = "ForwardDC_HDDS.xml.tmpl";
print "Reading from $ifname ...";
open(FILE, $ifname);
@lines = <FILE>;
close(FILE);
print "done\n";

$ofname = "ForwardDC_HDDS_cdc$Lcdc.xml";
print "Writing $ofname ...";
open(FILE, ">$ofname");
foreach $s (@lines){
	$s =~ s/fdc1/$fdc1/g ;
	$s =~ s/fdc2/$fdc2/g ;
	$s =~ s/fdc_a/$fdc_a/g ;
	$s =~ s/fdc_b/$fdc_b/g ;

	print FILE "$s";
}
close(FILE);
print "done\n";
print "\n";

