#!/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 "bday" - A Birthday            #
#   Reminder's main program.  You need to    #
# run bday before you put bdayd into your    #
#                 crontab.                   #
#                                            #
##############################################

use Shell qw(date touch mkdir);

# Initialize some variables
my $home = $ENV{HOME};

my $thepath = "$home/.bday";
my $prefs = "$thepath/bday.prefs";
my $people = "$thepath/people";
my $returnflag = 0;

# sub-declaration
sub editPrefs;
sub showPrefs;
sub addPerson;
sub removePerson;
sub showPeople;
sub enterEmail;

# let's see if all files are there
-d $thepath or mkdir $thepath;
touch $people;
touch $prefs;

# open a bunch of files
open(PREFS, "$prefs") or die "Cannot open file $prefs!\n"; 
open(PEOPLE, "$people") or die "Cannot open file $people for reading!\n";

# some initial values
my $choice = 1;
my $days =0;
my $num_of_people = 0;
my $tmp = "";

# check existing preferences
my @allprefs = split /:/, <PREFS>;

$days = $allprefs[1];
chomp($days);
if ($days eq "") {
  $tmp = 1;
  $days=3;
}

while ($lines = <PEOPLE>) {
  $num_of_people++;
}

my $email = $allprefs[5];
chomp($email);
if ($email eq "") {
  $email = $ENV{LOGNAME} . "@" . $ENV{HOSTNAME};
}

#this is the first time, we need to write the initial data
if ($tmp==1) {
  open(PREFS, ">$prefs") or die "Cannot open $prefs for writing!\n";
  print PREFS "Remind:$days:Number of People:$num_of_people:Email:$email";
}

# begin main menu
while ($choice!=7) {
  print "\nbday 0.1 -- Main Menu\n";
  print "----------------------------";
  print "\nPlease choose from the following menu.\n";
  print "\n1. Change User Preferences";
  print "\n2. Show current settings";
  print "\n3. Add a person to your list";
  print "\n4. Show all people on your list";  
  print "\n5. Remove a person from your list";
  print "\n6. About bday 0.1";
  print "\n7. Quit";
  print "\n\nYour choice: ";
    
  my $firstperson = 0;

  $choice = <STDIN>;
  chomp($choice);
  
  # we want to edit the preferences
  if ($choice==1) {
    editPrefs;
  }
  
  # let's see what our preferences are
  elsif ($choice==2) {
    showPrefs;
  }
  
  # we want to rent a book
  elsif ($choice==3) {
    addPerson;
  }
  
  # list all the books that are checked out
  elsif ($choice==4) {
    showPeople;
  }
  
  # return a book
  elsif ($choice==5) {
    removePerson;
  }
  
  # what is bday?
  elsif ($choice==6) {
    print "\nAbout bday ";
    print "\n------------------------";
    print "\nThis is Version 0.1 of \"bday\" - A Birthday Reminder.";
    print "\nbday is written by Jan Schaumann <jschauma\@netmeister.org> and published under the GNU Public License and thus comes without any warranty whatsoever. You can feel free to modify and\/or redistribute this code as specified in the GPL. Bday is available for free from http:\/\/www.netmeister.org.\nPlease send Bug-reports to <jschauma\@netmeister.org>.\nFor more information, please see the man-pages (bday(1)).\n";
  }
  
  # we're done - write some data and say bye bye
  elsif ($choice==7) {
    open(PREFS, "$prefs") or die "Cannot open $prefs for writing!\n";
    $_ = <PREFS>;	
    if (m/Number of People:\d/) {
      s/Number of People:\d/"Number of People:$num_of_people"/e;
      open(PREFS, ">$prefs") or die "Cannot open $prefs for writing!\n";
      print PREFS "$_";
    }
  }	
}

sub editPrefs {
  open(PREFS, ">$prefs") or die "Cannot open $prefs for writing!\n";
  print "\n\nbday 0.1 -- Edit User Preferences:\n";
  print "-----------------------------------------\n";
  print "\nHow many days in advance would you like to be reminded of upcoming birthdays? [current setting: $days]";
  $days = <STDIN>;
  chomp($days);
  while (($days<=0)||($days eq "")) {
    print "\nHow many days in advance would you like to be reminded of upcoming birthdays? [current setting: $days]";
    $days = <STDIN>;
    chomp($days);
  }
  
  print PREFS "Remind:$days:";
  
  print PREFS "Number of People:$num_of_people:";
  enterEmail;
  print PREFS "Email:$email:";
}

sub showPrefs {
  print "\nbday 0.1 -- Show current settings\n";
  print "---------------------------------------";
  print "\n\nYour current settings are:";
  print "\nEmail-Address: $email";
  print "\nDays you are reminded in advance: $days";
  print "\nNumber of People on file: $num_of_people\n\n";
}


sub enterEmail {
  print "\nWould you like me to send the reminding emails to $email? [y/n] ";
  chomp($input = <STDIN>);
  while (!(($input eq "y") || ($input eq "n") || ($input eq "Y") || ($input eq "N"))) {
    print "\nWould you like me to send the reminding emails to $email? [y/n] ";
    chomp($input = <STDIN>);
  }
  
  if (($input eq "n") || ( $input eq "N")) {
    print "\nPlease enter the email-address you would like the reminders to be sent to now: ";
    chomp($email = <STDIN>);
    while ($email eq "") {
      print "\nPlease enter the email-address you would like the reminders to be sent to now: ";
      chomp($email = <STDIN>);
    }
  }
}

sub addPerson {
  open(PEOPLE, ">>$people") or die "Cannot open $people for appending!\n";
  unless ($firstperson>0) {
    print "\n\nbday 0.1 -- Add a person\n";
    print "-----------------------------";
  }
  print "\nPlease enter the name of the person [enter CANCEL to abort]: ";
  chomp($name = <STDIN>);
  while ($name eq "") {
	print "\nPlease enter the name of the person [enter CANCEL to abort]: ";
	chomp($name = <STDIN>);
  }
  unless ($name eq "CANCEL") {
	#$curdate = `date +%D`;
	@currdate = split /\//, `date +%D`;
	$year = $currdate[2] + 2000;
  	print "\nPlease enter the birthday of this person (mm/dd/yyyy): ";
  	chomp($date = <STDIN>);
  	@thedate = split //, $date;
  	$mm = $thedate[0] . $thedate[1];
  	$dd = $thedate[3] . $thedate[4];
  	$yyyy = $thedate[6] . $thedate[7] . $thedate[8] . $thedate[9];
  	while (($thedate[10]) || ($mm>12) || ($dd>31) || ($mm<0) || ($dd<0) || ($yyyy<0) || ($yyyy>$year) || ($thedate[2] eq "")||($thedate[3] eq "") || ($thedate[4] eq "") || ($thedate[5] eq "") || ($thedate[6] eq "") || ($thedate[7] eq "") || ($thedate[8] eq "") || ($thedate[9] eq "")) {
    	print "\nNot a balid Date!\nPlease enter the birthday of this person (mm/dd/yyyy): ";
    	chomp($date = <STDIN>);
    	@thedate = split //, $date;
    	$mm = $thedate[0] . $thedate[1];
    	$dd = $thedate[3] . $thedate[4];
    	$yyyy = $thedate[6] . $thedate[7] . $thedate[8] . $thedate[9];
  	}	
  	$num_of_people++;
  	print PEOPLE "Name:$name:Birthday:$date:\n";
  	print "\nWould you like to add another person? [y/n] ";
  	chomp($input = <STDIN>);
  	while (!(($input eq "y") || ($input eq "n") || ($input eq "Y") || ($input eq "N"))) {
    	print "\nWould you like to add another person? [y/n] ";
    	chomp($input = <STDIN>);
  	}
  
  	if (($input eq "y")||($input eq "Y")) {
    	$firstperson=1;
    	addPerson;
    	close PEOPLE;
  	}
  }
  close PEOPLE;
}

sub showPeople {
  my $found = 0;
  open(PEOPLE, "$people") or die "Cannot open $people for reading!\n";
  unless ($returnflag==1) {
    print "\nbday 0.1 -- List of people on file\n";
    print "---------------------------------------";
  }
  @theline = split /:/, <PEOPLE>;
  if ($theline[0] eq "") {
    print "\nYou do not seem to have anybody on file.\n";
  }
  else {
    print "\nPlease enter a keyword to search the list of people [none to view all]: ";
    chomp($key = <STDIN>);
    open(PEOPLE, "$people") or die "Cannot open $people for reading!\n";
    while ($line = <PEOPLE>) {
      $_ = $line;
      if (m/$key/i) {
		($foo, $name, $foo, $bdate) = split /:/, $line;
		print "\nName:\t\t$name";
		print "\nBirthday:\t$bdate\n";
		$found = 1;
      }
      close LIBS;
    }
    if ($found==0) {
      print "\nNo matches for the keyword \"$key\".\n";
    }
  }
}

sub removePerson {
  $tmp = "";
  $found = 0;
  if ($num_of_people==0) {
    print "\nYou do not seem to have anybody on file.\n";
  }
  else {
    if ($firstperson==0) {
      print "\nbday 0.1 -- Remove a person\n";
      print "---------------------------------";
    }
    $returnflag=1;
    showPeople;
    print "\nPlease enter the full name of the person you wish to remove from the list [enter CANCEL to abort]: ";
    chomp($thename = <STDIN>);
    while ($thename eq "") {
    	print "\nPlease enter the full name of the person you wish to remove from the list [enter CANCEL to abort]: ";
    	chomp($thename = <STDIN>);
    }
    unless($thename eq "CANCEL") {
    	open(PEOPLE, "$people") or die "Cannot open $people for reading!\n";
    	$thename = $thename . ":";
    	while ($line = <PEOPLE>) {
      		$_ = $line;
      		if (m/.*?$thename/) {
				print "\n$thename Removed from your record.";
				$num_of_people--;
				$found = 1;
      		}
      		else {
				$tmp = $tmp . $line;
      		}
    	}
    	if ($found==0) {
      		print "\n$thename No such Person on file - please check the name for spelling!";
    	}
    	open(PEOPLE, ">$people") or die "Cannot open $people for writing!\n";
    	print PEOPLE "$tmp";
    	print "\nWould you like to remove another person? [y/n] ";
    	$input = <STDIN>;
    	chomp($input);
    	while (!(($input eq "y") || ($input eq "n") || ($input eq "Y") || ($input eq "N"))) {
      		print "\nWould you like to remove another person? [y/n] ";
      		chomp($input = <STDIN>);
    	}
    	if (($input eq "y") || ($input eq "Y")) {
      		removePerson;
      		$firstperson = 1;
    	}
  	}
  }
}
