#!/usr/bin/perl
#
#
############################################
#                                          #
#     bday 0.1 - A Birthday Reminder       #
#         written by Jan Schaumann         #
#                                          #
#     bday 0.1 is published under the      #
#     GPL and comes with no warranty       #
#                 whatsoever               #
#                                          #
#     You can obtain bday 0.1 (or maybe    #
#      even more recent versions) from     #
#         http://www.netmeister.org        #
#                                          #
#       Please send all Bug-Reports to     #
#          <jschauma@netmeister.org>       #
#                                          #
#     This is "bdayd" - the Birthday       #
#      Reminder Daemon. You need to        #
#      run bdayd from your crontab         #
#                                          #
#       See crontab(5) and bdayd(1) for    #
#              more information            #
#                                          #
############################################

############################################
#                                          #
#       Change the location fo sendmail    #
#               if not corrent             #
#                                          #
############################################
$sendmail = "/usr/sbin/sendmail";

use Shell qw(date touch rm mkdir cat);

my $home = $ENV{HOME};
my $thepath = "$home/.bday";
my $prefs = "$thepath/bday.prefs";
my $people = "$thepath/people";
my $message = "$thepath/message";
sub getDate;

-d $thepath or mkdir $thepath;

touch $message;
my $flag = 0;
my $num_of_people = 0;
my $birthdays = 0;

open(MESSAGE, ">$message") or die "Cannot open $message! Please assure that $message exists, and possibly run \"bday\" again!"; 

open(MESSAGE, ">$message") or die "Cannot open $message! Please assure that $message exists, and possibly run \"bday\" again!"; 
-e $people or print MESSAGE "I could not find $people - this means you have not yet run the main-program (\"bday\"). Please do so before you let me check if any books are due.\nHave a nice day!\n\nYour Birthday Reminder Daemon" and $flag=1;

open(MESSAGE, ">$message") or die "Cannot open $message! Please assure that $message exists, and possibly run \"bday\" again!"; 
-e $prefs or print MESSAGE "I could not find $prefs - this means you have not yet run the main-program (\"bday\"). Please do so before you let me check if any books are due.\nHave a nice day!\n\nYour Birthday Reminder Daemon" and $flag=1; 

open(MESSAGE, ">$message") or die "Cannot open $message! Please assure that $message exists, and possibly run \"bday\" again!"; 

# get the users preferences
my $email = $ENV{LOGNAME} . "@" . $ENV{HOSTNAME};

unless ($flag==1) {
  my $thedate = `date +%D`;
  my $absolutedate = 0;
  my $absolutebdaydate = 0;
  
  $absolutedate = getDate($thedate);
  
  # get the users preferences
  open(PREFS, "$prefs") or print MESSAGE "I could not open $prefs! Please checkthe permissions on this file, and run \"bday\" again.\nHave a nice day!\n\nYour Birthday Reminder Daemon";
  my @prefs = split /:/, <PREFS>;
  $email = $prefs[5];
  
  my $remind = $prefs[1];
    $somebday = 0;
    $firstbdaytoday = 0;
    $firstbdaysoon = 0;
    
    open(MESSAGE, ">>$message") or die;
    print MESSAGE "Subject: It's somebody's Birthday soon!\n";
    print MESSAGE "Hello!\nThis is bdayd, your Birthday Reminder Daemon.\n";
    open(PEOPLE, "$people") or die;
    while ($line = <PEOPLE>) {
      $absolutebirthdate = 0;
      $thedate = `date +%D`;
      @current = split /:/, $line;
      $absolutebdaydate = getDate($current[3]);
      @bdate = split /\//, $current[3];	

	  #calcualte how old they turn
	  @curdate = split /\//, $thedate;
	  $year = $curdate[2] + 2000;
	  $age = $year - $bdate[2];
      
      # it's somebody's birthday today
      if ($absolutebdaydate==$absolutedate) {
		if ($firstbdaytoday == 0) {
	  		print MESSAGE "\nThe following people have their birthday TODAY:\n";
		}
		print MESSAGE "\n$current[1] (turns $age!)";
		$firstbdaytoday = 1;
      }
      
      # it's somebody's birthday soon
      if (($absolutebdaydate-$remind)==$absolutedate) {
		if ($firstbdaysoon==0) {
	  		print MESSAGE "\nThe following people have their birthday on $bdate[0]\/$bdate[1]\/$bdate[2]:\n";
		}
		print MESSAGE "\n$current[1] (turns $age!)";
		$firstbdaysoon = 1;
      }
      
	}
	
    if (($firstbdaytoday==1)||($firstbdaysoon==1)) {
      $somebday = 1;
    }
  
  	print MESSAGE "\n\nThis is an automated message, please do not respond.\nHave a nice day!\n\nYour Birthday Reminder Daemon";
}

close MESSAGE;

if (($flag==0) && ($somebday>0)) {
  `cat $message | $sendmail -F "Your Birthday Reminder Daemon" $email`;
}

sub getDate {
  $returndate = 0;
  @adate = split /\//, @_[0];
  if ($adate[0]==1) {
    $returndate == $adate[1];
  }
  elsif ($adate[0] == 2) {
    $returndate += (31+$adate[1]);
  }
  elsif ($adate[0] == 3) {
    $returndate += (59+$adate[1]);
  }
  elsif ($adate[0] == 4) {
    $returndate += (90+$adate[1]);
  }
  elsif ($adate[0] == 5) {
    $returndate += (120+$adate[1]);
  }
  elsif ($adate[0] == 6) {
    $returndate += (151+$adate[1]);
  }
  elsif ($adate[0] == 7) {
    $returndate += (181+$adate[1]);
  }
  elsif ($adate[0] == 8) {
    $returndate += (212+$adate[1]);
  }
  elsif ($adate[0] == 9) {
    $returndate += (243+$adate[1]);
  }
  elsif ($adate[0] == 10) {
    $returndate += (273+$adate[1]);
  }
  elsif ($adate[0] == 11) {
    $returndate += (304+$adate[1]);
  }
  elsif ($adate[0] == 12) {
    $returndate += (334+$adate[1]);
  }
  
  # add the first 2000 years
#  $returndate += (2000*365);
#  $returndate += ($adate[2]*365);
}






