#!/usr/bin/perl
# $Id: remindd,v 1.3 2002/07/16 19:27:03 nomis80 Exp $
#
# Copyright (C) 2002  Linux Qubec Technologies
#
# This file is part of Chronos.
#
# Chronos is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Chronos is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use strict;
use Date::Calc qw(:all);
use Chronos::Static qw(dbh from_datetime gettext);
use POSIX qw(setsid);

unless ( $ARGV[0] eq '-D' ) {
    print "Forking...\n";
    my $pid = fork;
    exit if $pid;
    die "Couldn't fork: $!\n" unless defined $pid;
    setsid() or die "Can't start a new session: $!\n";
    open PID, "> /var/run/remindd.pid";
    print PID "$pid\n";
    close PID;
}

our $dbh = dbh();
$SIG{HUP} = sub { $dbh = dbh() };

my $sth_events       = $dbh->prepare("SELECT * FROM events WHERE reminder_sent = 'N' AND reminder <= NOW()");
my $sth_participants = $dbh->prepare("SELECT eid, user FROM participants WHERE reminder_sent = 'N' AND reminder <= NOW()");
my $sth_user         = $dbh->prepare("SELECT name, email, lang FROM user WHERE user = ?");
my $sth_events_eid   = $dbh->prepare("SELECT * FROM events WHERE eid = ?");
my $sth_update_events = $dbh->prepare("UPDATE events SET reminder_sent = 'Y' WHERE eid = ?");
my $sth_update_participants = $dbh->prepare("UPDATE participants SET reminder_sent = 'Y' WHERE eid = ? AND user = ?");

while (1) {
    # On commence par les initiateurs
    $sth_events->execute;
    while (my $event = $sth_events->fetchrow_hashref) {
        $sth_user->execute($event->{initiator});
        my ($name, $email, $lang) = $sth_user->fetchrow_array;
        $sth_user->finish;
        sendmail($event, $email, $lang);
        $sth_update_events->execute($event->{eid});
    }

    # On fait les participants
    $sth_participants->execute;
    while (my ($eid, $user) = $sth_participants->fetchrow_array) {
        $sth_events_eid->execute($eid);
        my $event = $sth_events_eid->fetchrow_hash;
        $sth_events_eid->finish;
        $sth_user->execute($user);
        my ($name, $email, $lang) = $sth_user->fetchrow_array;
        $sth_user->finish;
        sendmail($event, $email, $lang);
        $sth_update_participants->execute($eid, $user);
    }

    sleep 60;
}

sub sendmail {
    my ($event, $email, $lang) = @_;
    Language( Decode_Language($lang) );

    my $text = gettext($lang);
    my $mail = $text->{remindd_template};

    $mail =~ s/\%\%TO\%\%/$email/g;

    $mail =~ s/\%\%NAME\%\%/$event->{name}/g;

    my ($syear, $smonth, $sday, $shour, $smin) = from_datetime($event->{start});
    my $start = sprintf '%s %d:%02d', Date_to_Text_Long( $syear, $smonth, $sday ), $shour, $smin;
    $mail =~ s/\%\%START\%\%/$start/g;

    my ($eyear, $emonth, $eday, $ehour, $emin) = from_datetime($event->{end});
    my $end = sprintf '%s %d:%02d', Date_to_Text_Long( $eyear, $emonth, $eday ), $ehour, $emin;
    $mail =~ s/\%\%END\%\%/$end/g;

    $mail =~ s/\%\%DESCRIPTION\%\%/$event->{description}/g;
    $mail =~ s/\%\%VERSION\%\%/$Chronos::Static::VERSION/g;

    open MAIL, "| /usr/sbin/sendmail -oi -t";
    print MAIL $mail;
    close MAIL;
}

# vim: set et ts=4 sw=4 ft=perl:
