#!/usr/bin/perl -w
#========================================================#
#   This is the coffee program.   It sends email to      #
#   whomever needs to make coffee for that day.  Names   #
#   are read in from a file                              #
#   /home/astro/jmb6r/perlnames.txt.  The first person   #
#   in the file gets the message and his name is then    #
#   placed on the bottom of the stack.  This program     #
#   checks for colloquium, and office hours too.         #
#========================================================#     

$date = `date`;
#$date = "Thu Mar  4 15:14:56 EST 1999";
 
#=======================================================#
#   Send message only if it's Mon, -> Fri.              #
#=======================================================#

if ($date =~ /^Mon/ || $date =~ /^Tue/ || $date =~ /^Wed/ 
    || $date =~ /^Thu/ || $date =~ /^Fri/ ){

$coffee = 1;    # True, we are making coffee (may change)

#======================================================#
#      Put date into array                             #
#======================================================#

@date = split(/ +/,$date);
chomp(@date);

#==========================================================#
#  Now we have to change array element 2 of @date.         #
#  It needs to read "2" instead of "02", "3" instead of    #
#  "03", etc...                                            #
#==========================================================#

#if ($date[2] == "01"){
#    $date[2] = 1;
#} elsif ($date[2] == "02"){
#    $date[2] = 2;
#} elsif ($date[2] == "03"){
#    $date[2] = 3;
#} elsif ($date[2] == "04"){
#    $date[2] = 4;
#} elsif ($date[2] == "05"){
#    $date[2] = 5;
#} elsif ($date[2] == "06"){
#    $date[2] = 6;
#} elsif ($date[2] == "07"){
#    $date[2] = 7;
#} elsif ($date[2] == "08"){
#    $date[2] = 8;
#} elsif ($date[2] == "09"){
#    $date[2] = 9;
#}

#====================================================#
#  We have to check if there is a colloquium on      #
#  Tuesdays and Thursdays.                           #
#  Must look in the directory /home/www/events/      #
#  for for correct file.                             #
#  This is done ONLY if it is Tue or Wed             #
#                                                    #
#  This long "switch" statement will put the date    #
#  into the correct format to be found in Josh's     #
#  HTML files for the astro WEB page                 #
#====================================================#

if ($date[1] eq Jan){
    $file = "$date[5]"."01.htmlf";
} elsif ( $date[1] eq Feb ){
    $file = "$date[5]"."02.htmlf";
} elsif ( $date[1] eq Mar ){
    $file = "$date[5]"."03.htmlf";
} elsif ( $date[1] eq Apr ){
    $file = "$date[5]"."04.htmlf";
} elsif ( $date[1] eq May ){
    $file = "$date[5]"."05.htmlf";
} elsif ( $date[1] eq Jun ){
    $file = "$date[5]"."06.htmlf";
} elsif ( $date[1] eq Jul ){
    $file = "$date[5]"."07.htmlf";
} elsif ( $date[1] eq Aug ){
    $file = "$date[5]"."08.htmlf";
} elsif ( $date[1] eq Sep ){
    $file = "$date[5]"."09.htmlf";
} elsif ( $date[1] eq Oct ){
    $file = "$date[5]"."10.htmlf";
} elsif ( $date[1] eq Nov ){
    $file = "$date[5]"."11.htmlf";
} elsif ( $date[1] eq Dec ){
    $file = "$date[5]"."12.htmlf";
}

 
#============================================================#
#   Now that we have the file name, lets open that file.     #
#============================================================#

$fileopen = "/home/www/events/today.htmlf";
#print "$fileopen\n";

open (INFILE, "$fileopen") || die "can't open input file: $!";
@infile = <INFILE>;

#print "$#infile\n";
#print "$date[2]-$date[1]\n";


for ($i=0; $i<($#infile-1); $i++){
    if ( $infile[$i] =~ /^<!-- $date[2]-$date[1]/ ){
        $k = $i + 1;
	until ( $infile[$k] =~ /^<!--/ || $k == $#infile){
            #print "$infile[$k]\n";
	    if ($infile[$k] =~ /\bColloquium\b/){
                $coffee = 0;   # No coffee today!!
		}
	    $k = $k + 1;
	}
    }
}

#=============================================================#
#   If $coffee is still = 1, we are making coffee, go ahead   #
#   with the rest of the script.  If $coffee = 0, then exit   #
#=============================================================#

if ($coffee == 1){

open(NAMES, "/home/astro/jmb6r/perl/names.txt");
@names = <NAMES>;
chomp ($names[0]);
chomp ($names[1]);
chomp ($names[2]);
chomp ($names[3]);
chomp ($names[4]);
chomp ($names[5]);

#========================================================#
# make sure the chosen one doesn't have office hours.    #
#========================================================#

if ($names[0] eq Jeff ){
    @names[0,1,2,3,4,5] = @names[1,2,3,4,5,0];
}
if ($names[0] eq Ian && $date =~ /^Fri/ ){
    @names[0,1,2,3,4,5] = @names[1,0,2,3,4,5];
}
if ($names[0] eq John && $date =~ /^Wed/ ){
    @names[0,1,2,3,4,5] = @names[1,0,2,3,4,5];
}
if ($names[0] eq Jeff && $date =~ /^Mon/ ){
    @names[0,1,2,3,4,5] = @names[1,0,2,3,4,5];
}


#=======================================================#
# send mail to the chose one and to the Master          #
#=======================================================#

if ($names[0] eq Ian){
    system ("mail isf2f < /home/astro/jmb6r/perl/coffee4.txt");
    system ("echo Ian is making coffee | mail jmb6r ");
} elsif ($names[0] eq Scott){
    system ("mail swr3p < /home/astro/jmb6r/perl/coffee4.txt");
    system ("echo Scott is making coffee | mail jmb6r ");
} elsif ($names[0] eq John){
    system ("mail jds6h < /home/astro/jmb6r/perl/coffee4.txt");
    system ("echo John is making coffee | mail jmb6r" );
} elsif ($names[0] eq Jeff){
    system ("mail jmb6r < /home/astro/jmb6r/perl/coffee4.txt");
    system ("echo Jeff is making coffee | mail jmb6r" ); 
} elsif ($names[0] eq Riccardo){
    system ("mail ra3a < /home/astro/jmb6r/perl/coffee4.txt");
    system ("echo Riccardo is making coffee | mail jmb6r" );
} elsif ($names[0] =~ /^Hung/){
    system ("mail hj2n < /home/astro/jmb6r/perl/coffee4.txt");
    system ("echo Hung-Yu is making coffee | mail jmb6r" );
}

#================================================================#
#   If it's in the AM then don't change the ordering of the      #
#   names since you will need to send out a message in the       #
#   afternoon too.                                               #
#================================================================#

@time = split(/:/, $date[3]);

if ($time[0] > 12){
    @names[0,1,2,3,4,5] = @names[1,2,3,4,5,0];
    open(NAMEFILE, ">>/home/astro/jmb6r/perl/log");
    print NAMEFILE
    "$names[0]\n$names[1]\n$names[2]\n$names[3]\n$names[4]\n$names[5]\n";
    system("mv /home/astro/jmb6r/perl/log /home/astro/jmb6r/perl/names.txt");
}

}    # this is for the if ($coffee == 1) statement.
}    # this is for the if statment at the begining of the program

