#
# Create HOT BACKUP every N min
# use for this tbx.nlm
# Ver 1.00 22.03.2007 07:49 (removed debug print`s and stopkran )

use Time::Local;
my $stime; # 08:00
my $etime; # 21:00
my $ptime; # 15
my $sfile; # docum:\\wrk\\home\\user1\\test.doc
my $bfolder; # docum:\\wrk\\home\\user1\\bcp
my $bexten;  # sxc
my $tfile;   # docum:\\wrk\\home\\user1\\bcp\\log\\tfile.tsk
my $lfile; # docum:\\wrk\\home\\user1\\bcp\\log\\lfile.tsk
my $stop_file="sys:\\bcpspc.txt";
my $stopkran=4;

my $tcurentdate;
my $READCONF="sys:\\etc\\1bcpspc\\bcpspc.cfg";
my $delta;
my $waitetime;
my $ctime;

open (FL1, "<$READCONF") || die("Did not Open Source File !!");
$cmd1="";
$cmd1=readline(FL1);
$cmd1 =~ s/\n//g;
while (not eof(FL1) ) 
{
	if ( $cmd1 eq 'start' ) 
	{
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$stime = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$etime = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$ptime = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$sfile = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$bfolder = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$bexten = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$tfile = $cmd1;
		$cmd1=readline(FL1);
		$cmd1 =~ s/\n//g;
		$lfile = $cmd1;
M0:
		$tcurrentdate=getcurdate();
		$ttfile=getdatetfile();
		if ($tcurrentdate eq $ttfile) {
	        	goto M1;
		}
		else {
# THis FIRST START at this DAY !
			putdatetfile($tcurrentdate);
			system("del $bfolder /q");
		}
M1:	
		$currenttime=getcurtime();
		$currenttime=tcompare ($stime,$currenttime);
		if ($currenttime == 1){
			$currenttime=getcurtime();	
			$currenttime=tcompare ($currenttime,$etime);	
			if ($currenttime == 1){
				dohotbcp ($sfile, $bexten, $bfolder);
				goto M2;
			}
			else{
				goto M2;
			}
		}
		else{
			goto M2;
		}
M2:		check_stop();
		$waitetime=$ptime*60;
		$delta=$waitetime/10;
		while ($delta > 12) {
			check_stop();
			sleep(10);
			$delta=$delta-1;
		}
		check_stop();
#-D->	$stopkran=$stopkran-1;
#-D->	if ($stopkran == 1) {exit;}
	goto M0;
	}
	$cmd1=readline(FL1);
	$cmd1 =~ s/\n//g;
}
close(FL1);
exit(0);




sub dohotbcp()
{
#  create Hot copy
#
#	$_[0] = from this FULL file name
#	$_[1] = Destenition File Extension
#	$_[2] = to this folder

 $sec;
 $min;
 $hour;
 $mday;
 $mon;
 $year;
 $wday;
 $yday;
 $isdst;
 $out;
 $LOGFILE;
 $ex;
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
   $out=sprintf("%04d",1900+$year);
   $out.=sprintf("%02d",$mon+1);
   $out.=sprintf("%02d",$mday);
   $out.=sprintf("%02d",$hour);
   $out.=sprintf("%02d",$min);
#-D->   print "This SubRoutime DoHotBcp";
#-D->   print "\n".$out."\n";
   $LOGFILE=$_[2]."\\".$out."\.".$_[1];
   $ex="cp ".$_[0]." ".$LOGFILE;	
#-D->   print "SRC=[".$_[0]."]\n";
#-D->   print "LOGFILE=[".$LOGFILE."]\n";
#-D->   print "EX=[".$ex."]\n";
   system("$ex");
}

sub getcurdate()
{
 $sec;
 $min;
 $hour;
 $mday;
 $mon;
 $year;
 $wday;
 $yday;
 $isdst;
 $out;
 $LOGFILE;
 $ex;
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
   $out=sprintf("%04d",1900+$year);
   $out.=sprintf("%02d",$mon+1);
   $out.=sprintf("%02d",$mday);
return $out;
}

sub getdatetfile()
{
$FL1;
$BASESTR;
open (FL1, $tfile) || die("Did not Open Source File %tfile !!");
$BASESTR="";
$BASESTR=readline(FL1);
$BASESTR =~ s/\n//g;
close FL1;
return $BASESTR;
}

sub putdatetfile()
{
# $_[0] - string for write;
$FL1;
$BASESTR=" ";
open (FL1, ">".$tfile) || die("Did not Open Source File %tfile !!");
$BASESTR=$_[0];
$BASESTR =~ s/\n//g;
print FL1 $BASESTR;
close FL1;
}

sub tcompare()
{
# compare HH:MM with H1H1:M1M1
#$_[0] - hh:mm
#$_[1] - h1h1:m1m1
# return 
# 0 - Equal
# 1 if [0] < [1]
# 2 if [0] > [1]
 $H;
 $M;
 $H1;
 $M1;
 $T;
 $T1;
 $sec;
 $min;
 $hour;
 $mday;
 $mon;
 $year;
 $wday;
 $yday;
 $isdst;
 $out;
 $LOGFILE;
 $ex;
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;

 @MASS=split(/\:/,$_[0]);
#-D->print "[ $MASS[0] ][ $MASS[1] ]\n";
 $T=timelocal($sec,$MASS[1],$MASS[0],$mday,$mon,$year);
 @MASS=split(/\:/,$_[1]);
#-D->print "[ $MASS[0] ][ $MASS[1] ]\n";
 $T1=timelocal($sec,$MASS[1],$MASS[0],$mday,$mon,$year);
#-D->print "[$T]\n" ;
#-D->print "[$T1]\n";
 $out = $T-$T1;
 if ( $out == 0 ) { 
#-D->print "RAVNO\n";
 return (0);
 }
 if ( $out < 0 ) { 
#-D->print "T1 Bolshe\n";
 return (1);
 }
#-D-print "T1 MENSHE\n";
return (2);
}

sub getcurtime()
{
 $sec;
 $min;
 $hour;
 $mday;
 $mon;
 $year;
 $wday;
 $yday;
 $isdst;
 $out;
 $LOGFILE;
 $ex;
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
   $out=sprintf("%02d",$hour);
   $out.=":";
   $out.=sprintf("%02d",$min);
return $out;
}

sub check_stop {
  # Check to see whether the 'abort' file exists and exit if it does

  if (-e $stop_file) {
#    mylog("STOP file ($stop_file) exists. Terminating.\n", $log_file);
    exit;
    }
}

