#!/usr/bin/perl -w
#
# Perl code
# to make index.html
# for "HallB electronics, Racks layout"
# additional for Inv_mysql2html.pl
# run after Inv_mysql2html.pl
#
# clon10:sergpozd>    cp -pr /u/home/sergpozd/public_html/man/  /scratch/sergpozd/
# clon10:sergpozd>    cp -pr /home/sergpozd/work/perl/MySQL/SP  /scratch/sergpozd/
#
# [sergpozd@clonwiki] cp -pr /scratch/sergpozd/man     /wiki/clondocs/
# [sergpozd@clonwiki] cp -pr /scratch/sergpozd/SP/*    /wiki/clondocs/SP/HallB_Racks
#
# [sergpozd@clonwiki /]$ diff -r /scratch/sergpozd/man /wiki/clondocs/man
# [sergpozd@clonwiki /]$ diff -r /scratch/sergpozd/SP  /wiki/clondocs/SP/HallB_Racks | more
#
#
#  SP, 08-Dec-2008
#  SP, 01-Jun-2011  -  now two Hall-B Racks Layout: CLAS6 and CLAS12


sub getHTML_list();
sub LAYOUT_to_indexHTML();
sub indexHTML_print_list();
sub indexHTML_close();


#  Perl code:
#  ----------

#  init
$HallB_RACKs_NN  = 20;

#  init common
require "Inv_mysql2html_init.pl";


#  decode command line args
$line = 0;
$line = join(" ",@ARGV);


#  check command line args
if ($#ARGV != 0) {
    die "\n Usage:  Inv_SP_index.pl  Project\n\n";
}
$Project   = $ARGV[0];
$dirname  .= "_$Project";
$SP_Layout = "index.".$Project."_SP_Layout.html";


#  get list of HTML files in dir SP_Project
print "\n";
getHTML_list();


#  design HallB electronics Table in SP/index.html based on "SP_Layout"
LAYOUT_to_indexHTML();


#  design HallB electronics List in SP/index.html based on list of HTML files in SP dir
indexHTML_print_list();


#  design Footnote in SP/index.html and close it
indexHTML_close();


exit(0);


# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------


sub getHTML_list() {

#  check for directory with HallB electronics HTML files
    if (! -e $dirname) {die $!;}

#  get list of HTML files in SP
#    @ff_list = `ls -1 $dirname | grep -v index.html | grep -v *.jpg `;
#    $ff_nn   = `ls -1 $dirname | grep -v index.html | grep -v *.jpg | wc -l`;
    @ff_list = `ls -1 $dirname | grep 'html' | grep -v index  `;
    $ff_nn   = `ls -1 $dirname | grep 'html' | grep -v index  | wc -l `;
    if ($ff_nn == 0) {
	die "No files in directory \"$dirname\" \n";
    }
}


sub LAYOUT_to_indexHTML() {
    $Loc2 = "OOO";
    $p    = "tmp";

#  open HallB electronics Layout files with Dumy href
#    open LAYOUT, "index.SP_Layout.html" or die $!;
    open LAYOUT, $SP_Layout or die $!;

#  open index.html
#    open indexHTML, ">SP/index.html" or die $!;
    open indexHTML, ">".$dirname."/index.html" or die $!;


#  design HallB electronics list in SP/index.html based on "SP_Layout"
    while ($line_1 = <LAYOUT>) {

	if ($line_1 =~ m/\*\*\*  start/i) {
	    ($p,$Loc2) = split /start /, $line_1;
	       ($Loc2) = split / /,      $Loc2;
#print "$line_1";
#print "Loc2=$Loc2\n";
	}

	if ($line_1 =~ m/SSSP/) {
	    $line_2 = <LAYOUT>;
	    ($Rack_name) = split /</,$line_2;
	    $Rack_name =~ s/\s+//;

#if ($Rack_name =~ m/C3-/) {
#print "C3-5 >>$line_1";
#print "C3-5 >>$line_2";
#print "C3-5 >>Loc2=$Loc2;; Rack_name=$Rack_name;;\n";
#}

	    if    ($Rack_name =~ m/Test/)   { $Rack_name = "TestRack2"; }
	    elsif ($Rack_name =~ m/dapnia/) { $Rack_name = "L3-3"; }

	    for ($ii=0; $ii<$HallB_RACKs_NN; $ii++) {
		if (($HallB_RACKs[$ii] =~ m/$Loc2 /i) &&
		    ($HallB_RACKs[$ii] =~ m/ $Rack_name /i)) { $ii += 200; }
	    }
	    if ($ii < 200) {
		die "\n Wrong combination of HallB_Location and Rack_name: 
 HallB_RACKs[$ii]=$HallB_RACKs[$ii]  Loc2 = $Loc2   Rack_name = $Rack_name \n\n"; }
	    else { $ii -= 201; $Ir = $ii; }

	    @RRR  = split(" ",$HallB_RACKs[$Ir]);
	    $RRRn = $#RRR;


	    for ($ii=0; $ii<$ff_nn; $ii++)
	    {
		$ff_list[$ii] =~ s/\s+//;
		($fn_Loc2,$fn_R1,$fn_R2) = split /\./,$ff_list[$ii];

		if (($HallB_RACKs[$Ir] =~ m/$fn_Loc2 /i) &&
		    ($HallB_RACKs[$Ir] =~ m/ $fn_R1 /i) &&
		    ($HallB_RACKs[$Ir] =~ m/ $fn_R2 /i)) {

		    $RRRn1 = $RRRn2 = 0;
		    $RRRnr = 1000;
		    for ($kk=1; $kk<=$RRRn; $kk++) {
			if ($RRR[$kk] =~ m/$fn_R1/i)     { $RRRn1 = $kk; }
			if ($RRR[$kk] =~ m/$fn_R2/i)     { $RRRn2 = $kk; }
			if ($RRR[$kk] =~ m/$Rack_name/i) { $RRRnr = $kk; }
		    }

#if ($Rack_name =~ m/C3-/) {
#print "C3-5 >>HallB_RACKs[$Ir]=$HallB_RACKs[$Ir];;\n";
#print "C3-5 >>RRRn1=$RRRn1;; RRRn2=$RRRn2;; RRRnr=$RRRnr;;\n\n";
#}
		    if ((($RRRn1 <= $RRRnr) && ($RRRnr <= $RRRn2)) ||
			(($RRRn2 <= $RRRnr) && ($RRRnr <= $RRRn1))) {
			$line_1 =~ s/SSSP/$ff_list[$ii]/;
#print "========== $Ir Rack_name=$Rack_name;; 1=$RRRn1 r=$RRRnr 2=$RRRn2;; $ff_list[$ii]\n";
		    }
		}
	    }

	    print indexHTML "$line_1";
	    print indexHTML "$line_2";
	}
	elsif ($line_1 =~ m/charset/) {
	    print indexHTML "
   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
	}
	else {
	    print indexHTML "$line_1";
	}
    }

    close(LAYOUT);
}


sub indexHTML_print_list() {

    print indexHTML "
<blockquote>
<ul>";

    for ($ii=0; $ii<$ff_nn; $ii++)
    {
	($fn) = split /.html/, $ff_list[$ii];
	print indexHTML "
  <li> <a href=\"$fn.html\"
     >$fn
  </a></li>";
	print "ff_list($ii) = $fn;\n";
    }

    print indexHTML "
</ul>
</blockquote>";
}


sub indexHTML_close() {
    my $SPsec;
    my $SPmin;
    my $SPhour;
    my $SPmday;
    my $SPmon;
    my $SPyear;
    my $SPwday;
    my $SPyday;
    my $SPisdst;

    ($SPsec,$SPmin,$SPhour,$SPmday,$SPmon,$SPyear,$SPwday,$SPyday,$SPisdst) = localtime;
    $SPyear += 1900;
    $month_of_year = ('January', 'February', 'March',
		      'April',   'May',      'June',
		      'July',    'August',   'September',
		      'October', 'November', 'December')[$SPmon];
    $SPmon++;
    $day_of_week   = ('Sunday',   'Monday',
		      'Tuesday',  'Wednesday',
		      'Thursday', 'Friday', 'Saturday')[$SPwday];


#  put a Footnote in index.html and close it
    print indexHTML "\n

<!--  ###############   Signature   ###############  -->

<center>
<br><br>
<a href=\"#top\"><hr></a>
<!-- <font face=\"Helvetica,Arial\"><font size=-1> -->
<h5>
<a href=\"http://www.jlab.org/~sergpozd\">Home page</a>
&nbsp;&nbsp;&nbsp;
<font color=\"#FF3300\">|</font>
&nbsp;&nbsp;&nbsp;
Last updated: $month_of_year $SPmday, $SPyear.
&nbsp;&nbsp;&nbsp;
<font color=\"#FF3300\">|</font>
&nbsp;&nbsp;&nbsp;
<a href=\"mailto:sergpozd\@jlab.org\">
For more information send mail to Serguei A. Pozdniakov</a>
</h5>
<!-- </font></font> -->
</center>\n

</body>
</html>\n";

    close indexHTML;
    print "\nDone for today $day_of_week $SPmon/$SPmday/$SPyear".
	" ($month_of_year $SPmday, $SPyear)\n";
}

