summaryrefslogtreecommitdiff
path: root/pim/rc/next
diff options
context:
space:
mode:
Diffstat (limited to 'pim/rc/next')
-rwxr-xr-xpim/rc/next81
1 files changed, 81 insertions, 0 deletions
diff --git a/pim/rc/next b/pim/rc/next
new file mode 100755
index 0000000..0436cb3
--- /dev/null
+++ b/pim/rc/next
@@ -0,0 +1,81 @@
+#!/bin/rc
+# pim/next -- the next thing, one line, for a window label.
+rfork e
+
+# test(1) reads a leading zero as octal: 0700 compares as 448 and 0900
+# is a syntax error. Strip the padding before any arithmetic.
+fn num {
+ echo $1 | sed 's/^0*//; s/^$/0/'
+}
+
+mtpt=/mnt/pim
+only=()
+while(~ $1 -*){
+ switch($1){
+ case -m
+ mtpt=$2; shift
+ case -c
+ only=($only $2); shift
+ case *
+ echo 'usage: next [-m mtpt] [-c cal]' >[1=2]
+ exit usage
+ }
+ shift
+}
+cals=$only
+if(~ $#cals 0)
+ cals=`{pim/calendars -m $mtpt}
+
+now=`{date -n}
+hhmm=`{num `{date -f hhmm $now}}
+tmp=/tmp/next.$pid
+fn sigexit { rm -f $tmp }
+
+i=0
+while(test $i -lt 14){
+ sec=`{echo $now + $i '*' 86400 | bc}
+ day=`{date -f YYYY/MM/DD $sec}
+ {
+ for(c in $cals){
+ d=$mtpt/calendars/$c/events/date/$day
+ if(test -d $d)
+ for(f in $d/*){
+ b=`{basename $f}
+ sum=`{sed -n 's/^summary: //p' $f}
+ echo $"b^' '^$"c^' '^$"sum
+ }
+ }
+ } | sort >$tmp
+ if(test -s $tmp){
+ day=''
+ if(test $i -gt 0){
+ d=`{date -f 'WWW DD MMM' $sec}
+ day=$"d^' '
+ }
+ # awk prints the finished line: a value carried back through
+ # an rc variable would be split on its tabs and rejoined
+ # with spaces
+ out=/tmp/next.out.$pid
+ awk -F' ' -v 'first='^$i -v 'now='^$"hhmm -v 'day='^$"day '
+ {
+ t = $1 + 0
+ if (first > 0 || t >= now) {
+ split($1, a, "-")
+ when = substr(a[1],1,2) ":" substr(a[1],3,2)
+ if ($1 ~ /^0000-allday-/)
+ when = "all day"
+ printf "%s%s %s\n", day, when, $3
+ exit
+ }
+ }' $tmp >$out
+ if(test -s $out){
+ cat $out
+ rm -f $out $tmp
+ exit 0
+ }
+ rm -f $out
+ }
+ i=`{echo $i + 1 | bc}
+}
+echo 'nothing scheduled'
+exit 0