#!/usr/bin/perl ######################################################################### # calgen - calender generator # 14 November 2001, Arthur Keys # Modified December2002 # Purpose: automate schedule generation for rotating shift ######################################################################### # Default Variables @monthconv = qw(. January Febuary March April May June July August September October November December); @crew = qw(andy bob tom); $beginday = "28"; # \ $beginmonth = "2"; #-- Date to count from $beginyear = "2002"; # / $rotate = "3"; # Day That Begins New Shift, 0 = sunday # Default Values $debug = "0"; # print bug catches $hash = "0"; # print data hash $plain = "0"; # print plain text schedule $html = "1"; # print html schedule get_date(); #$month = "4"; $year = "2002"; # Read Options @args = @ARGV; # Hash of options $option{'-h'} = opt_help; $option{'-help'} = opt_help; $option{'-H'} = opt_header; $option{'-F'} = opt_footer; $option{'-p'} = opt_plain; $option{'-w'} = opt_html; # Options Subroutines sub opt_help { print< 12){$month = 12}; if ($year < $beginyear){$year = $beginyear}; if ($year > 4000){$year = 4000}; # Get current month and year sub get_date { @cal = `cal`; @a = split (/\s/,$cal[0]); $year = $a[4]; for $i (0 .. $#monthconv){ if ($a[3] eq $monthconv[$i]){$month = $i;} } if ($debug == "1"){print "\n\nCurrent Month-Year -$month-$year-\n\n";} } # Generate Calender Data @monthdata = `cal $date[$datecount]`; # Calculate Offset $offsetyear = $year - $beginyear; $offsetmonth = $month - $beginmonth; # Count Switch Days $crewcount = 0; for $yearcount($beginyear .. $year){ if ($debug == 1){print "$yearcount\n";} $frommonth = 1; if ($yearcount == $beginyear) {$frommonth = $beginmonth;} #$frommonth = $yearcount == $beginyear ? $month :$beginmonth; if ($yearcount == $year) {$tomonth = $month;} for $monthcount($frommonth .. $tomonth){ if ($debug == 1){print " $monthcount\n";} @current = `cal $monthcount $yearcount`; if ($monthcount == $beginmonth){ $fromweek = $beginweek; } for $weekcount(1 .. 6){ # 2-7 if ($debug == 1){print " $weekcount\n";} parse_day(); } } } sub parse_day{ #$cday = substr($current[$week], ($rotate - 1)*3,3); # get date *3 #chop $cday; #if ($monthcount == $beginmonth and $daycount == $beginday){$firstswitch = 1;} for $daycount(0 .. 6){ $date = substr($current[$weekcount+1], $daycount * 3,3); chop $date; #$tru = 0; #if ($date =~ /\d/){$tru = 1}; if ($debug == 1){print " $daycount date:$date $crewcount\n"}; if ($monthcount == $beginmonth and $date == $beginday){$firstswitch = 1;} if ($firstswitch == 1 and $date=~ /\d/){ if ($daycount == $rotate){ $crewcount++; if ($crewcount == $#crew + 1){$crewcount = 0}; } $datelist{$yearcount}{$monthcount}{$weekcount}{$daycount} = $date; $crewlist{$yearcount}{$monthcount}{$weekcount}{$daycount} = $crew[$crewcount]; } } #if ($cday =~ /\d/ and $firstswitch == 1){ # $friday[$day] = $cday; # $pin = $crew[$crewcount]; # for $wc ( 0 .. 6){ # print " $wc\n"; # $crewset[$monthcount][$friday[$day]+$wc] = $pin; # $crewset[$monthcount+1][$wc] = $pin; # if($monthcount == 12){$crewset[1][$wc] = $pin};#debug # } # $crewcount++; # if ($crewcount == $#crew+1){ # $crewcount = 0; # } # $day++; #} } # Output #@current = `cal $month $year`; #for $week (2 .. 7){ #print "\n\n Week " . ($week-1) . "\n"; #print "\n"; # for $daycount (0 .. 6){ # $day = substr($current[$week], $daycount*3,3); # # chop $day; # if ($day =~ /\d/){ # #print "$day $crewset[$month][$day] "; # $daydata[$day] = $crewset[$month][$day]; # $weekdata[$week-2][$daycount] = $crewset[$month][$day]; # $weekdays[$week-2][$daycount] = $day; # } # } #} # Print Data Hash if ($hash == 1){ for $keya (keys %datelist){ print "$keya\n"; for $keyb (keys %{$datelist{$keya}}){ print " $keyb\n"; for $keyc (keys %{$datelist{$keya}{$keyb}}){ print " $keyc\n"; for $keyd (keys %{$datelist{$keya}{$keyb}{$keyc}}){ print " $keyd"; print " $datelist{$keya}{$keyb}{$keyc}{$keyd}"; print " $crewlist{$keya}{$keyb}{$keyc}{$keyd}\n"; } } } } print "\n"; } # Convert output: weekdays, weekdata for $weekcount(0 .. 6){ for $daycount(0 .. 6){ $weekdays[$weekcount][$daycount] = $datelist{$year}{$month}{$weekcount}{$daycount}; $weekdata[$weekcount][$daycount] = $crewlist{$year}{$month}{$weekcount}{$daycount}; } } # Plain Text Output if ($plain == 1){ $wei = "5"; if ($weekdays[5][0] == ""){$wei = "4"}; for $n(1 .. $wei){ print "Week $n\n"; for $n2(0 .. 6){ print "$n2: $weekdays[$n][$n2]"; print " $weekdata[$n][$n2]\n"; } print "\n"; } } # HTML Output if ($html == 1){ print< Scheduled Center: Oncall Schedule Oncall Schedule

Oncall Cell Phone: xxx.xxx.xxxx
Rotation changes at 9am

EOF print " "; print< EOF $wei = "5"; if ($weekdays[5][0] == ""){$wei = "4"}; for $n(1 .. $wei){ print " "; for $n2(0 .. 6){ print ' '; print "\n"; } print ' '; print "\n"; } print< EOF } # End of HTML Output
$monthconv[$month]
Sun Mon Tue Wed Thu Fri Sat
'; print $weekdays[$n][$n2]; print '
'; print $weekdata[$n][$n2]; print '