#!/usr/bin/perl
#<mon2pre: read text calendar for a month, write preformatted HTML page.

use Modern::Perl;
use Getopt::Long qw(GetOptions);
use Pod::Usage;
use File::Basename;
use Carp;
use subs qw(footer header);

use subs qw(manpage myuuid usage version where);
local $ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin /opt/sfw/bin);

my $myname = basename($0);
$myname =~ s/\.\w*$//;   # strip any extension

my %options;
my @getopt_args = (
    'b',      # skip HTML header
    'h|?',    # print usage
    'm',      # print manpage
    'u',      # print UUID
    'v',      # print version
    'w',      # print source location
    );

Getopt::Long::config("noignorecase", "bundling");
usage unless GetOptions(\%options, @getopt_args);

manpage if $options{'m'};
myuuid  if $options{'u'};
version if $options{'v'};
where   if $options{'w'};
usage   if $options{'h'};

my $skip_header = 0;
$skip_header = 1 if $options{'b'};

my %months = (
    'January', '01', 'February', '02', 'March',     '03',
    'April',   '04', 'May',      '05', 'June',      '06',
    'July',    '07', 'August',   '08', 'September', '09',
    'October', '10', 'November', '11', 'December',  '12'
    );

my ($abmon, $mon, $cmonth, $yr);
my $spaces = ' ' x 20;

# Real work starts here.

while (<>) {
    chomp;
    last unless length;
    s/^/ /;    # easier to split out the numbers.

    # Month and year
    if (/\d\d\d\d\s+$/) {
        ($cmonth, $yr) = split;
        $mon = $months{$cmonth};
        header($myname, $cmonth, $yr);
    }

    # Weekdays
    elsif (/Sa  $/) {
        next;
    }

    # Should be a regular week -- pad with spaces in case it's
    # not a full 7 days.  "class=e" is for an empty day, "class=m"
    # is for a date that should be marked in some way.
    else {
        my $d;
        $_ .= $spaces;

        foreach my $k (0,3,6,9,12,15,18) {
            $d = substr($_, $k, 3);

            if ($d eq "   ") {
                print "$d";
            }
            else {
                my $orig = $d;
                $d =~ s/ //g;
                printf "<a class=\"e\" href=\"%s/%d/%2.2d%2.2d\">%s</a>",
                    'TOP', $yr, $mon, $d, $orig;
            }
        }

        print "\n";
    }
}

footer();
exit(0);

# -------------------------------------------------------------------------
# HTML header.
 
sub header {
    my ($prog, $m, $y) = @_;
    my $abmon = substr($m, 0, 3);

    unless ($skip_header) {
        print <<"EndHTML";
<!DOCTYPE html>
<!-- Generator: $prog -->
<html><head><title>$m $y</title><style>
a         { text-decoration: none; }
a:link    { color: blue; }
a:visited { color: red; }
a:hover   { background-color: black; color: white; }
</style></head><body><pre>
EndHTML
    }

    print <<"EndTOP";
          $abmon
 Su Mo Tu We Th Fr Sa
EndTOP
}

# -------------------------------------------------------------------------
# HTML footer.

sub footer {
    unless ($skip_header) {
        print "</pre></body></html>\n";
    }
}
exit(0);

#---------------------------------------------------------------------
# Print a usage message from the comments and exit.

sub usage {
    my ($emsg) = @_;
    use Pod::Usage qw(pod2usage);
    warn "$emsg\n" if defined $emsg;
    pod2usage(-verbose => 99, -sections => "NAME|SYNOPSIS|OPTIONS");
    return;
}

sub manpage {
    my @args = ("perldoc", "$0");
    exec { $args[0] } @args;          # safe even with one-arg list
    die("should not get here\n");
}

#---------------------------------------------------------------------
# Print the UUID, current version, or source location.

sub myuuid {
    my $UUID = (qw$UUID: 7fd2eaed-471c-53ab-b5a0-99d415faa909 $)[-1];
    print "$UUID\n";
    exit(0);
}

sub version {
    use version; our $VERSION = qv( (qw$Revision: 2.5 $)[-1] );
    my $DATE = join(" ", (qw$Date: 2026-04-16 07:03:35-04 $)[-2, -1]);
    print "$myname $VERSION $DATE\n";
    exit(0);
}

sub where {
    no warnings 'qw';
    my $SOURCE = (qw$Source: /home/vogelke/projects/autoindex/local-cgi/css/RCS/mon2pre,v $)[-1];
    my $HOST   = (qw$Host: bsd118.wpafb.af.mil $)[-1];
    print "file://$HOST", "$SOURCE\n";
    exit(0);
}

#---------------------------------------------------------------------
__END__

=head1 NAME

mon2pre - read text calendar for a month, write preformatted HTML page.

=head1 SYNOPSIS

mon2pre [-bhmuvw] [file ...]

=head1 OPTIONS

=over 4

=item B<-b>

Skip the HTML header; results will be used as part of a larger page.

=item B<-h>

Print a brief help message and exit.

=item B<-m>

Print the manual page and exit.

=item B<-u>

Print the script UUID and exit.

=item B<-v>

Print the version and exit.

=item B<-w>

Print the canonical source location and exit.

=back

=head1 DESCRIPTION

B<mon2pre> reads a simple text calendar generated by B<cal> and writes
a preformatted HTML page suitable for standalone use or inclusion in a table.

A text calendar looks like this:

  ....+....1....+....2....+
      October 2015
   Su Mo Tu We Th Fr Sa
                1  2  3
    4  5  6  7  8  9 10
   11 12 13 14 15 16 17
   18 19 20 21 22 23 24
   25 26 27 28 29 30 31
  ....+....1....+....2....+

=head1 AUTHOR

 Karl Vogel <vogelke+unix@pobox.com>

=cut
