#!/usr/local/bin/perl
#
# cache-expire.pl ver 0.52 --- "Pigeon Post" Mail-list Driver
#                ( 1999.07.02 named by Masato MINDA <minmin@wide.ad.jp> )
# Copyright (c)1999 by Kazunori ANDO <ando@kk.iij4u.or.jp> all rights reserved.
# 
#
################
$DIR = $ARGV[0];
if ( $0 =~ /^(\S+)\/cache-expire\.pl/ ){ $ORIGIN = $1 };
$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;
$locktmp = "L$$";
##### main #####
&ReadSiteConfig;

opendir(ML,$ML_SETTING_ROOT);
@alldir = grep(!/^\./,readdir(ML));
closedir(ML);

foreach $dir (@alldir){
    chdir $ML_SETTING_ROOT;
    if ((-d $dir) && (-x $dir)){
	print "processing $dir\n";
        chdir $dir;
	&PPLockWait;         # Lock 
	if (-f $Cache){
	    open(CACHE,">$Cache");
	    close(CACHE);
	    &Log("CACHE-EXPIRE: Clear $Cache in $dir");
	}else{
	    &Log("CACHE-EXPIRE: Not found $Cache in $dir");
	}
	&UnlockDone;         # Unlock
    }else{
        print STDERR "$0:WARN: Can't chdir to $dir.";
    }
}
exit 0;
################

sub ReadSiteConfig{
    $0 = "ReadSiteConfig";
    unshift(@INC,$ORIGIN);
    eval("require(\"sitedef.ph\")");
    if ($@) {
	&Log("$0:ERROR:$@");
	print STDERR $@;
	exit 2;
    }
    return 1;
}

sub PPLockWait{
    $0 = "PPLockWait";
    local($wait) = 0;
    unless(-d "$Savedir"){
	mkdir("$Savedir",0750);
	&Log("$0:FILE: \$Savedir($Savedir) is created.");
    }
    if ($USE_LOCKFILE == 1){
	while (-f "$Savedir/.lockfile"){
	    sleep 15;
	    $wait++;
	    if ($wait > 20){
		&Log("$0:Fail: Timeout to wait unlock ($Savedir/.lockfile)");
		print STDERR "$0:Fail: Timeout to wait unlock ($Savedir/.lockfile)\n";
		exit 2;
	    }
	}
	open (LOCK, "> $Savedir/$locktmp");
	print LOCK "$$\n";
	close (LOCK);
	while (!link("$Savedir/$locktmp", "$Savedir/.lockfile")){
	    sleep 15;
	    $wait++;
	    if ($wait > 40){
		&Log("$0:Fail: Timeout to link ($Savedir/.lockfile)");
		print STDERR "$0:Fail: Timeout to link ($Savedir/.lockfile)\n";
		exit 2;
	    }
	}
	&Log("$0:LOCK: lock Lockfile($Savedir/.lockfile) -- session start");
    }else{
	while (!open(LOCK,"+< config.ph")){
	    sleep 3;
	    $wait++;
	    if ($wait > 100){
		&Log("$0:Fail: Timeout to open (config.ph)");
		print STDERR "$0:Fail: Timeout to open (config.ph)\n";
		exit 2;
	    }
	}
	while (!flock(LOCK,$LOCK_EX)){
	    sleep 3;
	    $wait++;
	    if ($wait > 200){
		&Log("$0:Fail: Timeout to lock (config.ph)");
		print STDERR "$0:Fail: Timeout to lock (config.ph)\n";
		exit 2;
	    }
	}
	&Log("$0:LOCK: lock config.ph -- session start");
    }
    return 1;
}

sub UnlockDone{
    $0 = "UnlockDone";
    &Log("$0:DONE: unlock -- session completed.");
    if ($USE_LOCKFILE == 1){
	unlink("$Savedir/.lockfile");
	unlink("$Savedir/$locktmp");
    }else{
	unless (flock(LOCK,$LOCK_UN)){
	    &Log("$0:Fail: Can't unlock (config.ph)") if $debug;
	}
	close(LOCK);
    }
}

sub Log{
    local($s) = @_;
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0..6];
    $now = sprintf("%04d.%02d.%02d %02d:%02d:%02d",
		   1900 + $year, $mon + 1, $mday, $hour, $min ,$sec);
    $s =~ s/\r//;
    $s =~ s/\n//;
    open(LOG,">>$Logfile");
    print LOG "$now $s\n";
    close(LOG);
    return 1;
}
