#!/usr/local/bin/rexx

SAY 'Content-type: text/html'
SAY ''

parse upper arg month','year
IF STRIP(month)='' THEN DO
   PARSE UPPER VALUE DATE('n') WITH . month year
   END 
IF DATATYPE(month)<>'NUM' THEN 
   month=(index('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC', month)+3)/4
ELSE DO
   IF year='' THEN 
      PARSE UPPER VALUE DATE('n') WITH . . year
   END 
YEAR=RIGHT('9'year, 2)
If month=0
   Then exit

lines.=''
lines.0=0
call doone month, year, 1
call incrmon
SAY '<TITLE>'
SAY 'CGI-BIN Calendar made in REXX by Tony'
SAY '</TITLE>'
SAY '<BODY BGCOLOR=WHITE BACKGROUND="/address/slike/music3.gif">'
SAY '<CENTER>'
SAY '<TABLE BORDER=0><TR><TD><CENTER>'

DO i=1 TO lines.0
   SAY lines.i
END 

SAY '</TD></TR></TABLE>'
SAY '<BR>'
SAY '<TABLE><TR><TD><CENTER>'
SAY '<TABLE BORDER=0>'
line=''

DO i=month-3 TO month+3 
   yr=year
   ms=i
   IF ms<1 THEN DO
      ms=12+i
      yr=yr-1
      END 
   ELSE IF ms>12 THEN DO
      ms=i-12
      yr=yr+1
      END 
   mes=WORD('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC' , ms)
   line=line'<TD WIDTH=40 BGCOLOR=99ccff><CENTER><A HREF="/cgi-bin/addr_calendar?'ms','yr'">'mes'</CENTER></TD>'
END 
SAY line

SAY '</TABLE>'
SAY '</TD></TR></TABLE>'

SAY '<BR><BR><BR><BR><IMG SRC="/address/reklame/sezfile.gif">'

exit

incrmon:
month=month+1
If month<13
   Then return
month=1
year=year+1
return

doone: procedure expose lines.
parse arg month, year, position
todo=subword('31 28 31 30 31 30 31 31 30 31 30 31', 1, month-1)
days=word('31 28 31 30 31 30 31 31 30 31 30 31', month)
/* Days since the beginning of the century                           */
sum=365*year + (year-1)%4 +1
If (year//4)=0              /* Leap year?                  */
   Then
      select
         when month=1
            then nop
         when month=2
            then days=29
         otherwise
         sum=1+sum
      end

Do while todo<>''
   parse var todo d todo
   sum=sum+d
End

day=(sum-1)//7
call load 1, '<TABLE BORDER=0>'
call load 2, '<TR><TD COLSPAN=7 BGCOLOR=#31639c><CENTER><FONT SIZE=5 COLOR=White>'word('January February  March   April    May      June July',
        'August  September October November December',,
        month) '19'year'</FONT></CENTER></TD></TR>'

call load 3, '<TD BGCOLOR=99ccff WIDTH=40><CENTER>Mon</CENTER></TD><TD BGCOLOR=99ccff WIDTH=40><CENTER>Tue</CENTER></TD><TD BGCOLOR=99ccff WIDTH=40><CENTER>Wen</CENTER></TD><TD BGCOLOR=99ccff WIDTH=40><CENTER>Thu</CENTER></TD><TD BGCOLOR=99ccff WIDTH=40><CENTER>Fri</CENTER></TD><TD BGCOLOR=99ccff WIDTH=40><CENTER>Sat</CENTER></TD><TD BGCOLOR=99ccff WIDTH=40><CENTER>Sun</CENTER></TD>'
line='<TR>'
ix=4
DO r=1 TO day
   line=line'<TD BGCOLOR=99ccff><CENTER>-</CENTER></TD>'
END

do i=1 for days
   line=line'<TD BGCOLOR=99ccff><CENTER>'i'</CENTER></TD>'
   day=(day+1)//7
   If day>0
      Then iterate
   line=line'</TR>'
   call load ix, line
   ix=ix+1
   line='<TR>'
end

If line<>'' & day>0 Then DO
   DO r=day+1 TO 7
   line=line'<TD BGCOLOR=99ccff><CENTER>-</CENTER></TD>'
   END 
   call load ix, line   
   END 
ix=ix+1
call load ix, '</TABLE>'
return

load: procedure expose lines. position
parse arg which, data
lines.0=max(lines.0, which)
lines.which=overlay(data, lines.which, position)
return
