#!/usr/bin/perl
##
## Script to purge old acknowledged log entries
## from the database
##
## 16 Jan 04	1.0	Initial version
##
## written by Matt White (whitem@arts.usask.ca)
## 
## Remember to edit the settings below to your taste
##

use DBI;

## 
## User Configurable Settings
##

# How many days of events do you want to keep (in days?)
# The script will delete all acknowledged events older
# than this
$howmanydays = 14;

# Absolute outer bound of how long to keep an event.
# The script will delete any events older than this, 
# even if it has not been acknowledged.
$maxage = 30;

# Database connection details for Myth DB
$dbhost = "localhost";
$dbname = "mythconverg";
$dbuser = "mythtv";
$dbpass = "mythtv";

##
## End of User-configurable settings
##

my $dbh = 
DBI->connect("dbi:mysql:database=$dbname:host=$dbhost","$dbuser","$dbpass") or
		 die "Cannot connect to database ($!)\n";

$oneday = 60*60*25;
$now = time();
$ack = $now - ($oneday * $howmanydays);
$max = $now - ($oneday * $maxage);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
        localtime($ack);
$ackdate = sprintf("%4d-%02d-%02d",$year+1900,$mon+1,$mday);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
        localtime($max);
$maxdate = sprintf("%4d-%02d-%02d",$year+1900,$mon+1,$mday);

$q = "delete from mythlog where logdate<'$ackdate' and acknowledged=1";
$sth = $dbh->prepare($q);
$sth->execute or die "Could not execute ($q)\n";

$q = "delete from mythlog where logdate<'$maxdate'";
$sth = $dbh->prepare($q);
$sth->execute or die "Could not execute ($q)\n";

exit(0);
