#!/usr/bin/perl
use strict; use warnings;

use lib '/home/pcrow/srcgantry/docs/book/studies/Contact/lib';

use Date::Manip qw( ParseDate DateCalc UnixDate );

use Contact::Model::contact qw( $CONTACT );
use Contact::Model::bday    qw( $BDAY    );

use Gantry::Conf;
use Gantry::Utils::DBConnHelper::Script;

my $instance = shift;
my $conf     = Gantry::Conf->retrieve( { instance => $instance } );

Gantry::Utils::DBConnHelper::Script->set_conn_info( $conf );

my $today = ParseDate( "today" );
my $comp_err;

my @bdays = $BDAY->retrieve_all();

foreach my $bday_row ( @bdays ) {
    my $bday   = ParseDate( $bday_row->bdate );
    my $name   = $bday_row->name;
    my $family = $bday_row->contact_id->foreign_display;

    my $separation = DateCalc( $today, $bday, \$comp_err );

    my $days = ( split /:/, $separation )[3];

    my $short_bdate = UnixDate( $bday, "%B %e" );

    if ( abs( $days ) < 14 ) {
        print "$name of $family has a birth day $days days from now on "
                . "$short_bdate.\n";
    }
}
