summaryrefslogtreecommitdiff
path: root/pim/rc/next
blob: 0436cb3859d4f7ccc8fb8e51fe17a055fc1ab14c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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