chiark / gitweb /
Declare and document the dependency of hexterm on tcl8.4.
[chiark-utils.git] / scripts / palm-datebook-reminders
1 #!/usr/bin/perl
2
3 # Copyright 2003 Ian Jackson <ian@chiark.greenend.org.uk>
4 #
5 # This script and its documentation (if any) are free software; you
6 # can redistribute it and/or modify them under the terms of the GNU
7 # General Public License as published by the Free Software Foundation;
8 # either version 3, or (at your option) any later version.
9
10 # chiark-named-conf and its manpage are distributed in the hope that
11 # it will be useful, but WITHOUT ANY WARRANTY; without even the
12 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 # PURPOSE.  See the GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, consult the Free Software Foundation's
17 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
18
19 use Palm::PDB;
20 use Palm::Datebook;
21 use Time::Local;
22 use POSIX;
23
24 $us= $0 =~ m,[^/]+$, ? $& : $0;
25
26 $detail= 86400*2;
27 $lookforward= 86400*(7*4+1);
28 # run at 15:00 daily
29
30 sub setfilename($) {
31     die "$us: only one filename at a time please\n" if defined $filename;
32     $filename= $_[0];
33 }
34
35 while (@ARGV) {
36     $_= shift @ARGV;
37     if (m,^(?:\-t)?(\d+)/(\d+)$,) {
38         ($detail,$lookforward)=($1+0,$2+0);
39     } elsif (m,^[./],) {
40         setfilename($_);
41     } elsif (m/^\-f/) {
42         setfilename($');
43     } else {
44         die "$us: unknown argument/option \`$_'\n";
45     }
46 }
47
48 $filename= "DatebookDB.pdb" if !defined $filename;
49
50 defined($now= time) or die $!;
51 stat $filename or die "$us: $filename: $!\n";
52 $backuptime= (stat _)[10];
53
54 $pdb = new Palm::PDB or die $!;
55 $pdb->Load($filename) or die $!;
56
57
58
59 foreach $record (@{ $pdb->{records} }) {
60     if ($record->{start_hour} != 255) {
61         $timestr= sprintf("%02d:%02d-%02d:%02d",
62                           $record->{start_hour},
63                           $record->{start_minute},
64                           $record->{end_hour},
65                           $record->{end_minute});
66         @lt=(0, $record->{start_minute}, $record->{start_hour});
67     } else {
68         @lt=(0, 0, 9);
69         $timestr= "           ";
70     }
71     if ($record->{repeat}{type}) {
72         $timestr= "onwards    ";
73     }
74     
75     push @lt, $record->{day}, $record->{month}-1,  $record->{year}-1900;
76     defined($ettt= timelocal @lt) or die $!;
77
78     next if ($ettt < $now - 86400 ||
79              $ettt > $now + $lookforward);
80
81     @lt2= localtime($ettt) or die $!;
82     defined($dowstr= strftime "%a", @lt2) or die $!;
83
84     $datestr= sprintf("%04d-%02d-%02d",
85                       $record->{year},
86                       $record->{month},
87                       $record->{day});
88     
89     $timesortkey= "$datestr $timestr";
90     $evhead= "$datestr $dowstr $timestr";
91
92     $desc= $record->{description};
93     $desc =~ s/\x93/\~/g;
94     $desc =~ s/\xa3/L/g;
95     $desc =~ s/[^\n -\176]/ sprintf "\\x%02x", ord $& /eg;
96     $desc =~ s/^ +//;
97     $desc =~ s/\n+$//;
98     $desc .= "\n";
99
100     if ($desc =~ m/^(.{0,51})\n/) {
101         $descsumm= $1;
102     } elsif ($desc =~ m/^.{48}/) {
103         $descsumm= $&.'...';
104     } else {
105         $descsumm= " --- no description ?! ---";
106     }
107     $kind= $ettt < $now + $detail ? 'detail' : 'forward';
108
109     push @{ $events{$kind} }, {
110         TSK => $timesortkey,
111         Headline => sprintf("%s  %s", $evhead, $descsumm),
112         Desc => $desc
113         };
114 }
115
116 sub sectline_detail(){ return "Imminent events"; }
117 sub sectline_forward(){ return "Forthcoming events"; }
118
119 sub headline(){
120     printf("%s\n", $ev->{Headline})
121         or die $!;
122 }
123
124 sub print_forward(){ headline(); }
125 sub print_detail(){
126     print "\n" or die $!;
127     headline();
128     $_= $ev->{Desc};
129     s/^/    /gm;
130     print $_ or die $!;
131 }
132
133 foreach $kind (qw(detail forward)) {
134     $sectline= &{"sectline_$kind"};
135     printf("%s\n%s\n",
136            $sectline,
137            '-'x(length $sectline))
138         or die $!;
139     if (!@{ $events{$kind} }) {
140         printf("None scheduled.\n")
141             or die $!;
142     }
143     foreach $ev (sort { $a->{TSK} cmp $b->{TSK} } @{ $events{$kind} }) {
144         &{"print_$kind"};
145     }
146     print "\n" or die $!;
147 }
148
149 @lt2= localtime $backuptime or die $!;
150 defined ($syncstr= strftime "%Y-%m-%d %a %H:%M", @lt2) or die $!;
151
152 print("Date of last synch: $syncstr\n".
153       "Events entered on PDA after this date are omitted.\n")
154     or die $!;
155        
156 close STDOUT or die $!;