# v2.1

$symlinkFlag = 1;

@YDbMo = (0,31,59,90,120,151,181,212,243,273,304,334);
@DpMo = (0,31,28,31,30,31,30,31,31,30,31,30,31);

($curSec,$curMin,$curHr,$curDay,$curMo,$curYr,$curDoW,$curDoY) = localtime();
$curYr += 1900;
$curMo ++;
$curHr += $timeAdj;

while($curHr >= 24) {
	$curHr -= 24;
	($curYr,$curMo,$curDay) = &AddDeltaDays($curYr,$curMo,$curDay,1);
}

while($curHr < 0) {
	print "$curHr<br>";
	$curHr += 24;
	($curYr,$curMo,$curDay) = &AddDeltaDays($curYr,$curMo,$curDay,-1);
}
$dateStamp = "$curYr/$curMo/$curDay";

sub gen_error {
	($msg) = @_;
	print header;
	print "res=Err&msg=$msg&msg2=<p>see <a href=http://www.websitedesignpromotion.org/apps/L10_Hit_Counter/help_troubleshooting.html>L10HC Help</a> to troubleshoot";
	exit 0;
}

sub log_error {
	($msg) = @_;
	open(ERROR_FH, ">>L10HC_Errors.log");
	print ERROR_FH "$curMo/$curDay/$curYr $curHr:$curMin:$curSec - $msg\n";
	close(ERROR_FH);
}

sub log_event {
	($msg) = @_;
	open(ERROR_FH, ">>L10HC_Events.log");
	print ERROR_FH "$curMo/$curDay/$curYr $curHr:$curMin:$curSec - $msg\n";
	close(ERROR_FH);
}

sub writeParams () {
	$phr = "(\'".join("\',\'",@phrases)."\')";
	open (PARAM_FH, ">L10HC_Params.pl") || &gen_error("Cannot open L10HC_Params.pl at writeParams()");
	print PARAM_FH "\$startDate = \'$startDate\'\;\n";
	print PARAM_FH "\$startDateDisplay = \'$startDateDisplay\'\;\n";
	print PARAM_FH "\$timeAdj = $timeAdj\;\n"; #new
	print PARAM_FH "\@phrases = $phr\;\n";
	print PARAM_FH "\$bTagClr = \"$bTagClr\"\;\n";
	print PARAM_FH "\$removeDisplay = $removeDisplay\;\n";
	print PARAM_FH "\$removeBranding = $removeBranding\;\n"; #new 
	print PARAM_FH "\$password = \"$password\"\;\n";
	print PARAM_FH "\$logLimit = $logLimit\;\n";
	print PARAM_FH "\$allowLocalFiles = $allowLocalFiles\;\n";
	print PARAM_FH "\$protectStats = $protectStats\;\n";
	print PARAM_FH "\$adminURL = \'$adminURL\'\;\n";
	print PARAM_FH "\$enableAdminLink = $enableAdminLink\;\n";
	print PARAM_FH "\$displaySize = \'$displaySize\'\;\n";
	print PARAM_FH "\$fileLockTO = $fileLockTO\;\n";
	print PARAM_FH "\$keyCode = \'$keyCode\'\;\n"; #new
	print PARAM_FH "\$kcErr = $kcErr\;\n"; #new
	print PARAM_FH '1;';
	close (PARAM_FH);
}

sub moreThan24Hrs () {
	($y0,$m0,$d0,$h0,$y1,$m1,$d1,$h1) = @_;
	if($y1 > $y0) {return 1;}
	if($y1 < $y0) {return 0;}
	if($m1 > $m0) {return 1;}
	if($m1 < $m0) {return 0;}
	if($d1 <= $d0) {return 0;}
	if($d1 > ($d0 +1)) {return 1;}
	if($h1 > $hr0) {return 1;}
	return 0;
}



sub MondayOfWeek() {
	my ($y,$m,$d) = @_;
	$DoW = &DayOfWeek($y,$m,$d);
	unless($DoW == 0) {
		$d -= $DoW;
		if($d < 1) {
			$m --;
			if($m>0) {
				$d += $DpMo[$m];
				if(($m == 2) && &isLeapYr($y)) {
					$d++;
				}
			} else {
				$y--;
				$m = 12;
				$d += 31;
			}
		}
	}
	@r = ($y,$m,$d);
	return @r;
}

sub DayOfWeek () {
	my ($y,$m,$d) = @_;
	$ds01_01_01 = &DaysSince01_01_01($y,$m,$d);
	my @w = &intDiv($ds01_01_01,7);
	my $c = substr($w[1],0,1);
	if($c == 0) {
		return 6;
	} elsif($c == 1) {
		return 0;
	} elsif($c == 2) {
		return 1;
	} elsif($c == 4) {
		return 2;
	} elsif($c == 5) {
		return 3;
	} elsif($c == 7) {
		return 4;
	} else {
		return 5;
	}
	my $dow = ($w[1]); 
	return $dow;
}

sub DeltaDays() {
	my ($y0,$m0,$d0,$y1,$m1,$d1) = @_;
	$ds = &DaysSince01_01_01($y1,$m1,$d1) - &DaysSince01_01_01($y0,$m0,$d0);
	return $ds;
}

sub AddDeltaDays() {
	my ($y,$m,$d,$add) = @_;
	my $d = &DaysSince01_01_01($y,$m,$d) + $add;
	my @r = &DateFromDay($d);
	return @r;
}

sub DaysSince01_01_01 () {
	my ($y,$m,$d) = @_;
	my $Ys01 = $y - 2001;
	my @val = &intDiv($Ys01,4);
	my $ds01_01_01 = $Ys01*365 + $val[0];
	$ds01_01_01 += $YDbMo[$m-1];
	if(&isLeapYr && ($m > 2)) {
		$ds01_01_01 ++;
	}
	$ds01_01_01 += $d;
	return $ds01_01_01;
}

sub DateFromDay () {
	my ($d) = @_;
	my $y = 2001;
	my $m = 1;
	while($d > (365 + &isLeapYr($y))) {
		$d -= (365 + &isLeapYr($y));
		$y ++;
	}
	if(&isLeapYr($y)) {
		$DpMo[2] = 29;
	}
	while($d > ($DpMo[$m])) {
		$d -= $DpMo[$m];
		$m ++;
	}
	$DpMo[2] = 28;
	my @r = ($y,$m,$d);
	return @r;
	
}

sub intDiv () {	
	my ($n,$d) = @_;
	# print "n=$n,d=$d<br>";
	my $v = $n/$d;
	return  split(/\./,$v);	
}

sub isLeapYr () {
	($y) = @_;
	my $Ys01 = $y - 2001;
	my @val = &intDiv($Ys01,4);
	if($val[1] == 75) {
		return 1;
	} else {
		return 0;
	}
	
}

sub roundNum() {
	my ($val,$dec) = @_;
	if($dec > 0) {
		$val += (0.5 * (.1 ** $dec));
	} else {
		$val += 0.5;
	}
	my ($n,$d) = split(/\./,$val);
	if(($dec > 0) && (length($d) > 0)) {
		return $n.".".substr($d,0,$dec);
	} else {
		return $n;
	}
}

sub getFileDateStr() {
	my ($y,$m,$d) = @_;
	my $rStr = "";
	if(length($y) == 4) { $rStr = substr($y,2); } else {$rStr = "$y"; }
	if(length($m) == 1) { $rStr = $rStr."0$m"; } else {$rStr = "$rStr$m";}
	if(length($d) == 1) { $rStr = $rStr."0$d"; } else {$rStr = "$rStr$d";}
	return $rStr;
}

sub lock_stats() {
	if(!$symlinkFlag) {
		return 1;
	}
	for($i = 0; $i <= $lockTO; $i++) { 
		if(symlink( "L10HC_Hits.dat", "H10HC_Hits.dat.lck")) {
			return 1;
		}
		sleep 1;
	}
	return 0;
}

sub unlock_stats() {
	if(!$symlinkFlag) {
		return 1;
	}
	unlink ("H10HC_Hits.dat.lck");
}


1;