#!/usr/bin/perl -w

use strict;
use diagnostics;
#Number of days the files have to be untouched before deleting
my $auto_day = 180;
#Make sure the strings in the next 2 lines end with a /
my $dir = "/home/dba/qmud/player/";
my $errordir = "/home/dba/qmud/log/";

use Date::Calc qw(:all);

open(ERROR, join('', ">>", $errordir, "deleter.log")) || die "Cannot open deleter.log: $!\n";

#Set current time to test against files for deletion
my ($cur_year, $cur_mon, $cur_day) = Today();

#Get all the files in the directory stored in $dir and stat each 1 and
#compare it to the current time and delete if nessary.
my $file;
while ($file= glob(join('', $dir, "*")))
{
#Add in imm players that are exempt from auto deleter
    if ($file eq "Gokou")
    {
	next;
    }

    my ($file_time) = (stat($file))[9];
    my $filetime = scalar localtime($file_time);
    my ($file_year, $file_mon, $file_day) = Parse_Date($filetime);
    my $dif_days = Delta_Days($file_year, $file_mon, $file_day, $cur_year, $cur_mon, $cur_day);
    if ($dif_days > $auto_day)
    {
	my $num = unlink $file;
	if ($num < 1)
	{
	    printf ERROR "Couldn't delete ", $file, "\n";
	}
    }
    close(ERROR);
}