#!C:/APerl/bin/perl.exe --
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use Socket;

my ($passwd, $date, $lock_ex,$lock_un,$to_begin,$to_end);
my $htmlSubs=""; my $htmlSubs1=""; my $htmlSubs2=""; my $htmlSubs3=""; my $htmlSubs4="";

$lock_ex=2; $lock_un=8; $to_begin=1; $to_end=2;

print header;
 $htmlSubs="#!C:/APerl/bin/perl.exe --~~#!/usr/bin/perl -wT;".
           '\$slHome=\"http://localhost/web\"~~$slHome="http://www.insoft.fi";'.
           '\$slCgiHome=\"http://localhost/cgi-bin\"~~$slCgiHome="http://www.insoft.fi/cgi-bin";'.
           '\$arParDb = \"cgi-bin/arPar.db\"~~$arParDb = "../../db/arPar.db";'.
           '\$arDataDb = \"cgi-bin/arData.db\"~~$arDataDb = "../../db/arData.db";'.
           '\$arLog = \"cgi-bin/arLog.txt\"~~$arLog = "../../db/arLog.txt";'.
           '\$arHtm=\"cgi-bin/arHtm.eng\"~~$arHtm= "../../db/arHtm.eng";'.
           '\$arEmail=\"cgi-bin/arEmail.eng\"~~$arEmail= "../../db/arEmail.eng";'.
           '\$noMail=1~~$noMail=0;';
 &fileOut("cgi-bin/ar.pl","cgi-bin/ardepl/ar.cgi",$htmlSubs);
 $htmlSubs1="http://localhost/cgi-bin/arStyle.css~~http://www.insoft.fi/cgi-bin/arStyle.css;".
           "http://localhost/cgi-bin/ar.pl~~http://www.insoft.fi/cgi-bin/ar.cgi;".
           "ar.pl~~ar.cgi;".
           "http://localhost/cgi-bin/arStyle.css~~http://www.insoft.fi/cgi-bin/arStyle.css;".
            "http://localhost/cgi-bin/ar.pl~~http://www.insoft.fi/cgi-bin/ar.cgi;";
 &fileOut("cgi-bin/ar.htm","cgi-bin/ardepl/ar.htm",$htmlSubs1);
 &fileOut("cgi-bin/arHome.htm","cgi-bin/ardepl/arHome.htm",$htmlSubs1);
 &fileOut("cgi-bin/irHome.htm","cgi-bin/ardepl/irHome.htm",$htmlSubs1);
 &fileOut("cgi-bin/arHtm.eng","cgi-bin/ardepl/arHtm.eng",$htmlSubs1);
 &fileOut("cgi-bin/arEmail.eng","cgi-bin/ardepl/arEmail.eng",$htmlSubs1);



print end_html;
exit;


sub fileOut
{
 my ($fin,$fout,$sdata)=@_; 
 my $rec=""; my $ind; my $subItem;
 if(openFile($fin,"",0,4)==0) {print "<p>Cannot open: $fin"; return;};
 if(openFile($fout,">",0,3)==0) {print "<p>Cannot open: $fout"; return;};
 while ($rec=<FILE4>)
 {
  foreach $subItem (split(";", $sdata))
  {
   my $var=(split("~~", $subItem))[0];
   my $val=(split("~~", $subItem))[1];
   $rec =~ s/$var/$val/g;
  }
  print FILE3 "$rec";
 }
 closeFile(0,4);
 closeFile(0,3);
 print "<p>File: $fin deployed to: $fout</p>";
 return;
}

sub htmlBlockOut
{
 my ($fn,$bn,$sdata)=@_; 
 my $rec=""; my $ind; my $b_begin="<!--BB ".$bn."-->"; my $b_end="<!--EB ".$bn."-->";
 my $subItem;
 if(openFile($fn,"",0,4)==0) {return;};
 while($ind=index($rec,$b_begin) == -1) {if(!($rec=<FILE4>)) {closeFile(0,4);return;}}
 if(length($rec)>length($b_begin)) {$rec=substr($rec,$ind+length($b_begin),length($rec));} 
 else {$rec="";}
 while (index($rec,$b_end) == -1)
 {
  foreach $subItem (split(";", $sdata))
  {
   my $var=(split("~~", $subItem))[0];
   my $val=(split("~~", $subItem))[1];
   $rec =~ s/$var/$val/g;
  }
  print "$rec";
  if(!($rec=<FILE4>)) {closeFile(0,4);return;};
 }
 if(length($rec)>length($b_end)) {$rec=substr($rec,0,length($rec)-length($b_end)-1);print "$rec";}
 closeFile(0,4);
 return;
}


sub openFile
{
 my $fn = shift; my $mode=shift; my $lock=shift; my $fcnt=shift;
 if($fcnt==0) {open(FILE0,$mode." ".$fn) or return 0;}
 elsif($fcnt==1) {open(FILE1,$mode." ".$fn) or return 0;}
 elsif($fcnt==2) {open(FILE2,$mode." ".$fn) or return 0;}
 elsif($fcnt==3) {open(FILE3,$mode." ".$fn) or return 0;}
 elsif($fcnt==4) {open(FILE4,$mode." ".$fn) or return 0;}
 if ($lock == 1) 
  {if($fcnt==0) {flock(FILE0,$lock_ex);}
   elsif ($fcnt==1) {flock(FILE1,$lock_ex);}
   elsif ($fcnt==2) {flock(FILE2,$lock_ex);}
   elsif ($fcnt==3) {flock(FILE3,$lock_ex);}
   elsif ($fcnt==4) {flock(FILE4,$lock_ex);}
  }
 return 1;
}

sub closeFile
{
 my $lock=shift; my $fcnt=shift;
 if ($lock == 1)
  {if($fcnt==0) {flock(FILE0,$lock_un);}
   elsif ($fcnt==1) {flock(FILE1,$lock_un);}
   elsif ($fcnt==2) {flock(FILE2,$lock_un);}
   elsif ($fcnt==3) {flock(FILE3,$lock_un);}
   elsif ($fcnt==4) {flock(FILE4,$lock_un);}
  }
 if($fcnt==0) {close FILE0;}
   elsif ($fcnt==1) {close FILE1;}
   elsif ($fcnt==2) {close FILE2;}
   elsif ($fcnt==3) {close FILE3;}
   elsif ($fcnt==4) {close FILE4;}
}

sub setFile
{
 my $pos=shift; my $fcnt=shift;
 if ($pos == $to_begin) 
   {
   if($fcnt==0) {seek(FILE0,0,0);}
   elsif ($fcnt==1) {seek(FILE1,0,0);}
   elsif ($fcnt==2) {seek(FILE2,0,0);}
   elsif ($fcnt==3) {seek(FILE3,0,0);}
   elsif ($fcnt==4) {seek(FILE4,0,0);}
   }
 else 
   {
   if($fcnt==0) {seek(FILE0,0,2);}
   elsif ($fcnt==1) {seek(FILE1,0,2);}
   elsif ($fcnt==2) {seek(FILE2,0,2);}
   elsif ($fcnt==3) {seek(FILE3,0,2);}
   elsif ($fcnt==4) {seek(FILE4,0,2);}
   }
}