#!/bin/sh
#
# Original From: patwood at unirot /dot/ UUCP (Patrick Wood)
# 
# The following is a PostScript program to print calendars.  It doesn't
# work on or before 1752.
# 
# Shell stuff added 3/9/87 by King Ables
# 
# Made pretty by tjt 1988
#
# Brian Ward (http://www.o--o.net/)
# 1991 Got rid of stupid printer stuff
# 2003 Fixed stupid 2000/2001 day offset bug
# 2003 Converted to sh
# 2003 Default month/year
# 2003 Added some dumb options
# 2004 Maybe actually fixed the day offset bug for real
#

# CONFIGURE: custom fonts -- if you define these and use the -f flag,
#            mocal will use these fonts instead of the defaults

TITLEFONT=LucidaSans
TITLEFONTFILE=/home/bri/Mail/spam/lsr.pfa
DAYFONT=LucidaSans
#    DAYFONT=LucidaSans-Bold
#    DAYFONTFILE=/home/bri/Mail/spam/lsb.pfa

# end of configuration

USE_MYFONTS=0

while [ $# -gt 0 ]; do
    NOT_OK=1
    case $1 in
	-f)
	    USE_MYFONTS=1
	    ;;
	*)
	    if [ -z "$MONTH" ]; then
		MONTH=$1
		NOT_OK=0
	    else if [ -z "$YEAR" ]; then
		YEAR=$1
		NOT_OK=0
		fi
	    fi

	    if [ $NOT_OK -eq 1 ]; then
		echo "usage: $0 [-f] [month [year]]" >&2
		exit 1
	    fi
	    ;;
    esac
    shift
done

if [ -z $YEAR ]; then
    YEAR=`date +%Y`
fi

if [ -z $MONTH ]; then
    MONTH=`date +%m`
fi

if [ $USE_MYFONTS -eq 0 ]; then
    TITLEFONT=Times-Bold
    DAYFONT=Helvetica-Bold

    # if your printer doesn't have these fonts, fill in the following
    TITLEFONTFILE=
    DAYFONTFILE=
fi

cat <<END-OF-HEADER
%!
% PostScript program to draw calendar
% Copyright (C) 1987 by Pipeline Associates, Inc.
% Permission is granted to modify and distribute this free of charge.

% /month should be set to a number from 1 to 12
% /year should be set to the year you want
% you can change the title and date fonts, if you want
% we figure out the rest
% won't produce valid calendars before 1800 (weird stuff happened
% in September of 1752)

END-OF-HEADER

if [ "$DAYFONTFILE" ]; then
    cat $DAYFONTFILE
fi

if [ "$TITLEFONTFILE" ]; then
    cat $TITLEFONTFILE
fi

cat <<END-OF-CALENDAR

/month $MONTH def
/year $YEAR def
/titlefont /$TITLEFONT def
/dayfont /$DAYFONT def

/month_names [ (January) (February) (March) (April) (May) (June) (July)
		(August) (September) (October) (November) (December) ] def

/prtnum { 3 string cvs show} def

/prtoutnum {
    gsave
	.5 setlinewidth
	0 setgray
	3 string cvs
	true charpath stroke
    grestore
} def

/drawgrid {		% draw calendar boxes
	dayfont findfont 10 scalefont setfont
	0 1 6 {
		dup dup 100 mul 40 moveto
		[ (Sunday) (Monday) (Tuesday) (Wednesday) (Thursday) (Friday) (Saturday) ] exch get
		100 center
		100 mul 35 moveto
		1.0 setlinewidth
		0 1 5 {
			gsave
			100 0 rlineto 
			0 -80 rlineto
			-100 0 rlineto
			closepath stroke
			grestore
			0 -80 rmoveto
		} for
	} for
} def

/drawnums {		% place day numbers on calendar
	dayfont findfont 30 scalefont setfont
	/start startday def
	/days ndays def
	start 100 mul 5 add 10 rmoveto
	1 1 days {
		/day exch def
		gsave
		/special 0 def
		day start add 7 mod 0 eq
		{
			submonth 0 eq
			{
				% .8 setgray
				/special 1 def
			} if
		} if
		day start add 7 mod 1 eq
		{
			submonth 0 eq
			{
				% .8 setgray
				/special 1 def
			} if
		} if
		special 1 eq {
		    .8 setgray
		} if
		gsave day prtnum grestore
		special 1 eq {
		    day prtoutnum
		} if
		grestore
		day start add 7 mod 0 eq
		{
			currentpoint exch pop 80 sub 5 exch moveto
		}
		{
			100 0 rmoveto
		} ifelse
	} for
} def

/drawfill {		% place fill squares on calendar
	/start startday def
	/days ndays def
	0 35 rmoveto
	1.0 setlinewidth
	0 1 start 1 sub {
		gsave
		.9 setgray
		100 0 rlineto 
		0 -80 rlineto
		-100 0 rlineto
		closepath fill
		grestore
		100 0 rmoveto
	} for
	submonth 1 eq
	{
		/lastday 42 def
		600 -365 moveto
	}
	{
		/lastday 40 def
		400 -365 moveto
	} ifelse
	lastday -1 ndays start 1 add add
	{
		/day exch def
		gsave
		.9 setgray
		100 0 rlineto 
		0 -80 rlineto
		-100 0 rlineto
		closepath fill
		grestore
		day 7 mod 1 eq
		{
			600 -365 80 add moveto
		}
		{
			-100 0 rmoveto
		} ifelse
	} for
} def

/isleap {				% is this a leap year?
	year 4 mod 0 eq			% multiple of 4
	year 100 mod 0 ne 		% not century
	year 400 mod 0 eq or and	% unless it's divisible by 400
} def

/days_month [ 31 28 31 30 31 30 31 31 30 31 30 31 ] def

/ndays {		% number of days in this month
	days_month month 1 sub get
	month 2 eq	% Feb
	isleap and
	{
		1 add
	} if
} def

/startday {		% starting day-of-week for this month
	/off year 9600 sub def	% offset from start of "epoch"
	off
	off 4 idiv add		% number of leap years
	off 100 idiv sub	% number of centuries
	off 400 idiv add	% number of goofy weird days
	% off 1000 idiv add	% number of millenia
	% 7 add 7 mod 6 add 	% offset from Jan 1 2000
	6 add 7 mod 7 add 	% offset from Jan 1 9600

	/off exch def
	1 1 month 1 sub {
		/idx exch def
		days_month idx 1 sub get
		idx 2 eq
		isleap and
		{
			1 add
		} if
		/off exch off add def
	} for
	off 7 mod		% 0--Sunday, 1--monday, etc.
} def

/center {		% center string in given width
	/width exch def
	/str exch def width str 
	stringwidth pop sub 2 div 0 rmoveto str show
} def

/calendar
{
	titlefont findfont 48 scalefont setfont
	0 60 moveto
	/month_name month_names month 1 sub get def
	month_name show
	/yearstring year 10 string cvs def
	700 yearstring stringwidth pop sub 60 moveto
	yearstring show

	0 0 moveto
	drawnums

	0 0 moveto
	drawfill

	0 0 moveto
	drawgrid
} def

90 rotate
50 -120 translate
/submonth 0 def
calendar
month 1 sub 0 eq
{
	/lmonth 12 def
	/lyear year 1 sub def
}
{
	/lmonth month 1 sub def
	/lyear year def
} ifelse
month 1 add 13 eq
{
	/nmonth 1 def
	/nyear year 1 add def
} 
{
	/nmonth month 1 add def
	/nyear year def
} ifelse
/submonth 1 def
/year lyear def
/month lmonth def
500 -365 translate
gsave
.138 .138 scale
10 -120 translate
calendar
grestore
/submonth 1 def
/year nyear def
/month nmonth def
100 0 translate
gsave
.138 .138 scale
10 -120 translate
calendar
grestore

showpage

END-OF-CALENDAR
