Description: Monolithic patch that combines multiple combinations.
 Warning: the list below may be incomplete.
Bug-Debian: http://bugs.debian.org/312452
Author: Toni Timonen <ttimonen@users.sourceforge.net>
Bug-Debian: http://bugs.debian.org/194322
Author: Sam Hocevar <sam@zoy.org>
Bug-Debian: http://bugs.debian.org/274722 or http://bugs.debian.org/344550
Author: Matthew Wilcox <matthew@wil.cx>
Forwarded: do not know…

--- mencal-2.3.orig/mencal
+++ mencal-2.3/mencal
@@ -100,22 +100,15 @@
 
   sub new {
     my $type=shift;
+    my @starts;
     my $self={
-      'start'     => shift || '',
+      'start'     => \@starts,
       'length'    => shift || 28,
       'duration'  => shift || 4,
       'name'      => shift || 'Unknown',
       'color'     => lc(shift) || 'red',
       'days'      => ''
     };
-    if($self->{'start'}!~/^[0-9]{8}$/) {
-      my @localtime=localtime();
-      if($self->{'start'}=~/^[0-1][0-9][0-3][0-9]$/) {
-        $self->{'start'}=($localtime[5]+1900).$self->{'start'};
-      } else {
-        $self->{'start'}=($localtime[5]+1900).sprintf("%02d", $localtime[4]+1).sprintf("%02d", $localtime[3]);
-      }
-    }
     $self->{'color'}=~/^(nocolor|red|green|blue|yellow|violet|cyan|shiny|bold)$/ || ($self->{'color'}='red');
     $self->{'length'}=~/^[0-9]+$/ || do { $self->{'length'}=28; };
     $self->{'duration'}=~/^[0-9]+$/ || do { $self->{'duration'}=4; };
@@ -127,10 +120,11 @@
     my $self=shift;
     (my $filename=shift) || return(0);
     open(FILE, $filename) || return(0);
+    my $starts = $self->{'start'};
     while(<FILE>) {
       chomp;
       (/^#/ || /^$/) && next;
-      /^start ([0-9]{8})$/ && do { $self->{'start'}=$1; next; };
+      /^start ([0-9]{8})$/ && do { push @{$starts},$1; next; };
       /^length ([0-9]+)$/ && do { $self->{'length'}=$1; next; };
       /^duration ([0-9]+)$/ && do { $self->{'duration'}=$1; next; };
       /^color (nocolor|red|green|blue|yellow|violet|cyan|shiny|bold)$/ && do { $self->{'color'}=$1; next; };
@@ -147,13 +141,14 @@
     my $self=shift;
     (my $str=shift) || return(0);
     my $filename='';
+    my $starts = $self->{'start'};
     foreach(split(',', $str)) {
       /^(s|start)=([0-9]{4})$/ && do {
         my @localtime=localtime();
-        $self->{'start'}=($localtime[5]+1900).$2;
+        push @{$starts},($localtime[5]+1900).$2;
         next;
       };
-      /^(s|start)=([0-9]{8})$/ && do { $self->{'start'}=$2; next; };
+      /^(s|start)=([0-9]{8})$/ && do { push @{$starts},$2; next; };
       /^(l|length)=([0-9]+)$/ && do { $self->{'length'}=$2; next; };
       /^(d|duration)=([0-9]+)$/ && do { $self->{'duration'}=$2; next; };
       /^(c|color)=(nocolor|red|green|blue|yellow|violet|cyan|shiny|bold)$/ && do { $self->{'color'}=$2; next; };
@@ -165,7 +160,10 @@
     ($self->{'duration'}>=$self->{'length'}) && do { $self->{'duration'}=$self->{'length'}; };
     if($filename) {
       if(open(RC, ">$filename")) {
-        foreach my $key ('start', 'length', 'duration', 'color', 'name') {
+        foreach my $date (@{$starts}) {
+	  print RC 'start '.$date."\n";
+	}
+        foreach my $key ('length', 'duration', 'color', 'name') {
           print RC $key.' '.$self->{$key}."\n";
         }
         close(RC);
@@ -177,27 +175,76 @@
     return(1);
   }
 
+  sub date2sec {
+    my $date = shift;
+    $date =~ /^([0-9]{4})([0-9]{2})([0-9]{2})$/;
+    my ($sy, $sm, $sd) = ($1, $2, $3);
+    return mktime(0, 0, 0, $sd, ($sm-1), ($sy-1900));
+  }
+
+  sub markdays {
+    my ($self, $begin, $end, $date, $length) = (shift, shift, shift, shift, shift);
+    my $day = ($date - $begin) / (24 * 3600) + 1;
+    my $limit = $date + $length;
+    $limit = $end if ($end < $limit);
+    while ($date < $limit) {
+      $self->{'days'} .= $day.'#' if ($day > 0);
+      $day++;
+      $date += 24 * 3600;
+    }
+  }
+
+  sub getnext {
+    my $self = shift;
+    my $first = shift;
+    my $starts = $self->{'start'};
+    my $length = $self->{'length'} * 24 * 3600;
+    my $n_s = scalar @{$starts};
+    my $best;
+
+    if (($n_s > 0) && (date2sec($starts->[0]) <= $first) &&
+        ($first <= date2sec($starts->[($n_s - 1)]))) {
+      foreach (@{$starts}) {
+        $best = date2sec($_);
+        last if $best >= $first;
+      }
+      return $best;
+    }
+
+    if ($n_s == 0) {
+      $best = strftime "%Y%m%d", localtime;
+    } elsif ($first < date2sec($starts->[0])) {
+      $best = date2sec($starts->[0]);
+    } else {
+      $best = date2sec($starts->[$n_s - 1]);
+    }
+
+    while ($best > ($first + $length)) {
+      $best -= $length;
+    }
+
+    while ($best < $first) {
+      $best += $length;
+    }
+
+    return $best;
+  }
+
   sub getdays {
-    my $self=shift;
-    my ($year, $month)=(shift, shift);
-    $self->{'days'}='#';
-    $self->{'start'}=~/^([0-9]{4})([0-9]{2})([0-9]{2})$/;
-    my ($sy, $sm, $sd)=($1, $2, $3);
-
-    my $month_first=mktime(0, 0, 0, 1, ($month-1), ($year-1900));
-    my $month_next=mktime(0, 0, 0, 1, $month, ($year-1900));
-    my $actday=mktime(0, 0, 0, $sd, ($sm-1), ($sy-1900));
-    while($actday>=$month_first) { $actday-=24*3600*$self->{'length'}; }
-    while(($actday+24*3600*$self->{'length'})<$month_first) { $actday+=24*3600*$self->{'length'}; }
-    $self->{'start'}=strftime("%Y%m%d", localtime($actday));
-
-    my ($i, $duration, $first, $onmonth)=(0, 0, 0, 0);
-    while($actday<$month_next) {
-      (!$onmonth) && ($actday>=$month_first) && do { $first=$i; $onmonth++; };
-      ($duration<$self->{'duration'}) && ($actday>=$month_first) && ($self->{'days'}.=($i-$first+1).'#');
-      $duration++; $i++;
-      $actday+=24*3600;
-      ($duration==$self->{'length'}) && ($duration=0);
+    my $self = shift;
+    my ($year, $month) = (shift, shift);
+    my $duration = $self->{'duration'} * 24 * 3600;
+    $self->{'days'} = '#';
+
+    my $month_first = mktime(0, 0, 0, 1, ($month-1), ($year-1900));
+    my $month_next = mktime(0, 0, 0, 1, $month, ($year-1900));
+    my $first_interesting = $month_first - $duration;
+
+    my $actday = $self->getnext($first_interesting);
+
+    while ($actday < $month_next) {
+      markdays($self, $month_first, $month_next, $actday, $duration);
+      $actday = $self->getnext($actday + $duration + 1);
     }
   }
 
@@ -223,12 +270,19 @@
     'topline'           => 'Su Mo Tu We Th Fr Sa',
     'month_delimiter_h' => "   ",
     'month_delimiter_v' => "\n",
-    'nocolor'           => 0,
+    'nocolor'           => (not POSIX::isatty('STDOUT')),
     'icolor'            => 'red',
     'quiet'             => 0
   );
   my @confs=();
 
+  #There exists a config file
+  $_ = "$ENV{HOME}/.mencalrc";
+  if (-f $_ && -r $_) {
+    push(@confs,Conf->new());
+    $confs[$#confs]->parsefile($_);
+  }
+
   for(my $i=0; $i<=$#ARGV; $i++) {
     foreach($ARGV[$i]) {
       /^(-h|--help)$/ && &Main::usage;
@@ -241,6 +295,7 @@
       };
       /^(-q|--quiet)$/ && do { $config{'quiet'}=1; last; };
       /^(-m|--monday)$/ && do { $config{'monday_first'}=1; last; };
+      /^(-C|--color)$/ && do { $config{'nocolor'}=0; last; };
       /^(-n|--nocolor)$/ && do { $config{'nocolor'}=1; last; };
       /^(-i|--icolor)$/ && do {
         (($#ARGV>$i) && ($ARGV[++$i]=~/^(red|green|blue|yellow|violet|cyan|shiny|bold)$/)) || &Main::out("Invalid intersection color, see '-h' for more details");
@@ -362,6 +417,7 @@
       "  -3                  previous, current and next month\n".
       "  -y [YYYY]           all-year calendar (default YYYY is current year)\n".
       "  -q, --quiet         no top information will be printed\n".
+      "  -C, --color         colored output (default)\n".
       "  -n, --nocolor       noncolored output\n".
       "  -i, --icolor COLOR  intersection color (default red)\n".
       "    available colors: red, green, blue, yellow, violet, cyan, shiny, bold\n".
