Git init
[external/mawk.git] / examples / hical
1 #!/usr/sh
2
3 # @(#) hical - displays previous, current & next months - today highlighted
4 # @(#) an "internationalizationable" version of a 3-month 'cal' display, it
5 # @(#) may be edited for week to start with Sun or Mon & for local language
6
7 prog=/tmp/hical.$$ ; trap 'rm -f $prog ; trap 0 ; exit' 0 1 2 3 15
8
9 : ${so:=`tput smso`} ${se:=`tput rmso`}
10
11 # USER EDITS MAY BE REQUIRED for the arguments to the 'date' command
12 # the script presumes 'date' recognizes these arguments in these ways:
13 #   w - Day of the week - Sunday = 0
14 #   m - Month of year - 01 to 12
15 #   d - Day of month - 01 to 31
16 #   T - Time as HH:MM:SS
17 #   Y - Year (including century), as decimal numbers
18 DATE_ARGS='%w %m %d %T 19%y'
19
20 # the 'awk' program file is written to a temporary file to avoid any
21 # "arg list too long" error messages, yet have all the code in one file
22 # observe when editing the program file that '\n' must be '\\n'
23 # NOTE: to make this script portable /bin/echo has been used instead of the
24 #  echo built into the shell for the 'bash' shell on Linux, use 'echo -e' in
25 #  the next line (Modification made by Edward Betts <edward@debian.org>)
26 /bin/echo '{
27 # USER EDITS MAY BE REQUIRED (for FMT, day & month names, and the time stuff)
28 # FMT = 0  # for weekdays ordered "Mo Tu We Th Fr Sa Su"
29   FMT = 1  # for weekdays ordered "Su Mo Tu We Th Fr Sa"
30   Header[0] = "Mo Tu We Th Fr Sa Su"
31   Header[1] = "Su Mo Tu We Th Fr Sa"
32   months    = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
33   time_is   = "The time is:" ; time_fmt = "%s   %s  %s %s\\n"
34 # NO MORE USER EDITS REQUIRED (I think!)
35   split(months,M_Name) ; split("31 28 31 30 31 30 31 31 30 31 30 31",M_Len)
36   daynum  = $1 + FMT
37   Mon[2]  = $2 + 0
38   today   = $3 + 0
39   time    = $4
40   Year[1] = Year[2] = Year[3] = $NF
41   if ( Mon[2] ==  1 ) { Year[1] = Year[1] - 1 ; Mon[1] = 12 }
42     else              { Mon[1]  = Mon[2]  - 1 }
43   if ( Mon[2] == 12 ) { Year[3] = Year[3] + 1 ; Mon[3] =  1 }
44     else              { Mon[3]  = Mon[2]  + 1 }
45   if ( Year[2] % 4   == 0 && \
46        Year[2] % 100 != 0 || \
47        Year[2] % 400 == 0 ) M_Len[2] = 29
48   Start[2] = 7 - ( ( today - daynum ) % 7 )
49   Start[1] = 7 - ( ( M_Len[Mon[1]] - Start[2] ) % 7 )
50   Start[3] =       ( M_Len[Mon[2]] + Start[2] ) % 7
51   for (i=1;i<=3;i++) { while ( Start[i] >= 7 ) Start[i] -= 7 }
52   for (mm=1;mm<=3;mm++) {
53     if ( Year[mm] != Year[mm-1] )
54       printf( "%s            %s          %s\\n", so, Year[mm], se )
55     if ( mm == 1 ) printf( "%s    %s  %s\\n", so, Header[FMT], se )
56     j = k = 1
57     while ( j <= M_Len[Mon[mm]] ) {
58       line = ""
59       for (i=1;i<=7;i++) {
60         if ( Start[mm] > 0 || j > M_Len[Mon[mm]] ) { date = "" ; Start[mm]-- }
61           else date = j++
62         if ( mm == 2 && date == today ) { So = so ; Se = se }
63           else { So = Se = "" }
64         line = sprintf( "%s%s%2s%s ", line, So, date, Se )
65       }
66       m1 = substr(M_Name[Mon[mm]],k++,1)
67       printf( "%s %1s %s %s%s %s\\n", so, m1, se, line, so, se )
68     }
69   }
70   printf( time_fmt, so, time_is, time, se )
71 }' >$prog
72
73 date +"$DATE_ARGS" | ${AWK:=mawk} -f $prog so=$so se=$se
74
75 exit 0
76
77 # EOF 'hical'  - Tue Dec 19 19:19:19 EST 1994
78 # Bob Stockler - bob@trebor.iglou.com - CIS: 72726,452