#!/usr/bin/perl
# weekly-set-date.pl
# runs every Saturday to update the current weekfile
# creates a new subdirectory for next week's log files
# separate directories for each web server host

if ($#ARGV > -1) {
    die "usage: weekly-set-date.pl\n";
}

$| = 1;					# unbuffered output
umask(022);				# files are world readable
chop($CHGRP=`which chgrp`);
die("can't find chgrp\n") if $CHGRP =~ /^no /;
chop($RM=`which rm`);
die("can't find rm\n") if $RM =~ /^no /;

$GBIN='/opt/ACISweb/bin/getstats';
$weekfile="/www/data/httpd/log/thisweek"; # the week that ends today
$nextfile="/www/data/httpd/log/nextweek"; # the week that ends next Saturday
$LOG='/www/data/httpd/log/fu';
$SEC='/www/data/httpd/log/sec';
$thisgroup='www';			# put new subdir in this group

				# today's date in the form "Sep 13, 1997"
				# today's date in the form "19970913"
($thiswk, $thisrep) = &makdate(time);

if (open(DATEFILE, "<$weekfile")) {
    chop($lastwk=<DATEFILE>);
    chop($lastrep=<DATEFILE>);
    close(DATEFILE);
}

$rerun='';
if ($thisrep eq $lastrep) {
  $rerun=1;
  print "weekly-set-date has already run this week, will recheck symlinks\n\n";
  ($lastwk, $lastrep) = &makdate(time - 7*24*3600); # 7 days ago
}
($nextwk, $nextrep) = &makdate(time + 7*24*3600); # 7 days from now

print "last week was     $lastwk ($lastrep)\n";
print "this week is      $thiswk ($thisrep)\n";
print "next week will be $nextwk ($nextrep)\n";


unless ($rerun) {
  die "can't overwrite $weekfile: $!" unless open(DATEFILE,">$weekfile");
  print DATEFILE "$thiswk\n$thisrep\n";
  close(DATEFILE);
}

unless ($rerun) {
  die "can't overwrite $nextfile: $!" unless open(DATEFILE,">$nextfile");
  print DATEFILE "$nextwk\n$nextrep\n";
  close(DATEFILE);
}

die "can't open $LOG: $!\n" unless opendir(LDIR, $LOG);
@ldirs = readdir(LDIR);
closedir(LDIR);
foreach $host (@ldirs) {		# non-secure server hosts
  next if $host =~ /^\./;
  &next_week($LOG, $host, $lastrep, $thisrep, $nextrep);
}


die "can't open $SEC: $!\n" unless opendir(LDIR, $SEC);
@ldirs = readdir(LDIR);
closedir(LDIR);
foreach $host (@ldirs) {		# secure server hosts
  next if $host =~ /^\./;
  &next_week($SEC, $host, $lastrep, $thisrep, $nextrep);
}


######################################################################
sub makdate {
    my($tim) = @_;
    my(@mont) = ('Jan','Feb','Mar','Apr','May','Jun',
		 'Jul','Aug','Sep','Oct','Nov','Dec');

    my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
				localtime($tim);
    $year += 1900;
    $datstr = sprintf("%s %2d, %4d",	$mont[$mon], $mday, $year);
    $datnum = sprintf("%4d%02d%02d",	$year, $mon+1, $mday);
    return($datstr, $datnum);
}



sub next_week {
    my($logd, $host, $lastsat, $thissat, $nextsat) = @_;
    if (!chdir("$logd/$host")) {
	print "Can't cd to $logd/$host because $!\n";
	return;
    }
    if (readlink('prev') eq $thissat) {
	print "$host directory has already been updated, skipping\n";
	return;
    }
    unless (-d $thissat) {
	print "No such directory $host/$thissat\n";
	return;
    }
    if (-e $nextsat) {				# create a new directory
	print "$host/$nextsat exists\n" ;
    } elsif (!mkdir($nextsat, 0750)) {
	print "Can't create $host/$nextsat because $!\n";
	return;
    }
    system("$CHGRP $thisgroup $nextsat");	# put new dir in this group


					# prev
    if (-l 'prev' && !unlink('prev')) {	# if prev symlink exists remove it
	print "Can't remove symlink $host/prev because $!\n";
	return;
    }
				# prev symlink shall now point to thissat
    if (!symlink($thissat, 'prev')) {
	print "Can't create symlink $host/prev -> $thissat because $!\n";
	return;
    }
    print "created symlink $host/prev -> $thissat\n";


					# curr
    if (-l 'curr' && !unlink('curr')) {	# if curr symlink exists remove it
	print "Can't remove symlink $host/curr because $!\n";
	return;
    }
				    # curr symlink will now point to nextsat
    if (!symlink($nextsat, 'curr')) {
	print "Can't create symlink $host/curr -> $nextsat because $!\n";
	return;
    }
    print "created symlink $host/curr -> $nextsat\n";

    if (-d $lastsat) {
	system("$RM -rf $lastsat");	# delete last week's files
	print "removed $host/$lastsat\n";
    }
    return;
} # next_week
