chiark / gitweb /
e9804296a3a8d0d9a09e5833e4e1d154d5533dd3
[chiark-utils.git] / scripts / palm-datebook-reminders
1 #!/usr/bin/perl
2
3 use Palm::PDB;
4 use Palm::Datebook;
5 use Time::Local;
6 use POSIX;
7
8 $us= $0 =~ m,[^/]+$, ? $& : $0;
9
10 $detail= 86400*2;
11 $lookforward= 86400*(7*4+1);
12 # run at 15:00 daily
13
14 sub setfilename($) {
15     die "$us: only one filename at a time please\n" if defined $filename;
16     $filename= $_[0];
17 }
18
19 while (@ARGV) {
20     $_= shift @ARGV;
21     if (m,^(?:\-t)?(\d+)/(\d+)$,) {
22         ($detail,$lookforward)=($1+0,$2+0);
23     } elsif (m,^[./],) {
24         setfilename($_);
25     } elsif (m/^\-f/) {
26         setfilename($');
27     } else {
28         die "$us: unknown argument/option \`$_'\n";
29     }
30 }
31
32 $filename= "DatebookDB.pdb" if !defined $filename;
33
34 defined($now= time) or die $!;
35 stat $filename or die "$us: $filename: $!\n";
36 $backuptime= (stat _)[10];
37
38 $pdb = new Palm::PDB or die $!;
39 $pdb->Load($filename) or die $!;
40
41
42
43 foreach $record (@{ $pdb->{records} }) {
44     if ($record->{start_hour} != 255) {
45         $timestr= sprintf("%02d:%02d-%02d:%02d",
46                           $record->{start_hour},
47                           $record->{start_minute},
48                           $record->{end_hour},
49                           $record->{end_minute});
50         @lt=(0, $record->{start_minute}, $record->{start_hour});
51     } else {
52         @lt=(0, 0, 9);
53         $timestr= "           ";
54     }
55     if ($record->{repeat}{type}) {
56         $timestr= "onwards    ";
57     }
58     
59     push @lt, $record->{day}, $record->{month}-1,  $record->{year}-1900;
60     defined($ettt= timelocal @lt) or die $!;
61
62     next if ($ettt < $now - 86400 ||
63              $ettt > $now + $lookforward);
64
65     @lt2= localtime($ettt) or die $!;
66     defined($dowstr= strftime "%a", @lt2) or die $!;
67
68     $datestr= sprintf("%04d-%02d-%02d",
69                       $record->{year},
70                       $record->{month},
71                       $record->{day});
72     
73     $timesortkey= "$datestr $timestr";
74     $evhead= "$datestr $dowstr $timestr";
75
76     $desc= $record->{description};
77     $desc =~ s/\x93/\~/g;
78     $desc =~ s/\xa3/L/g;
79     $desc =~ s/[^\n -\176]/ sprintf "\\x%02x", ord $& /eg;
80     $desc =~ s/^ +//;
81     $desc =~ s/\n+$//;
82     $desc .= "\n";
83
84     if ($desc =~ m/^(.{0,51})\n/) {
85         $descsumm= $1;
86     } elsif ($desc =~ m/^.{48}/) {
87         $descsumm= $&.'...';
88     } else {
89         $descsumm= " --- no description ?! ---";
90     }
91     $kind= $ettt < $now + $detail ? 'detail' : 'forward';
92
93     push @{ $events{$kind} }, {
94         TSK => $timesortkey,
95         Headline => sprintf("%s  %s", $evhead, $descsumm),
96         Desc => $desc
97         };
98 }
99
100 sub sectline_detail(){ return "Imminent events"; }
101 sub sectline_forward(){ return "Forthcoming events"; }
102
103 sub headline(){
104     printf("%s\n", $ev->{Headline})
105         or die $!;
106 }
107
108 sub print_forward(){ headline(); }
109 sub print_detail(){
110     print "\n" or die $!;
111     headline();
112     $_= $ev->{Desc};
113     s/^/    /gm;
114     print $_ or die $!;
115 }
116
117 foreach $kind (qw(detail forward)) {
118     $sectline= &{"sectline_$kind"};
119     printf("%s\n%s\n",
120            $sectline,
121            '-'x(length $sectline))
122         or die $!;
123     if (!@{ $events{$kind} }) {
124         printf("None scheduled.\n")
125             or die $!;
126     }
127     foreach $ev (sort { $a->{TSK} cmp $b->{TSK} } @{ $events{$kind} }) {
128         &{"print_$kind"};
129     }
130     print "\n" or die $!;
131 }
132
133 @lt2= localtime $backuptime or die $!;
134 defined ($syncstr= strftime "%Y-%m-%d %a %H:%M", @lt2) or die $!;
135
136 print("Date of last synch: $syncstr\n".
137       "Events entered on PDA after this date are omitted.\n")
138     or die $!;
139        
140 close STDOUT or die $!;