summaryrefslogtreecommitdiff
path: root/pim/rc/agenda
blob: ec38ad2a7a4f6ea9931a581154db4d635bb4dbe0 (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
82
83
84
85
86
87
88
#!/bin/rc
# pim/agenda -- what is happening, across every mounted calendar.
rfork e

mtpt=/mnt/pim
days=7
off=0
only=()

while(~ $1 -*){
	switch($1){
	case -m
		mtpt=$2; shift
	case -n
		days=$2; shift
	case -o
		off=$2; shift
	case -c
		only=($only $2); shift
	case *
		echo 'usage: agenda [-m mtpt] [-n days] [-o dayoffset] [-c cal]' >[1=2]
		exit usage
	}
	shift
}

cals=$only
if(~ $#cals 0)
	cals=`{pim/calendars -m $mtpt}
if(~ $#cals 0){
	echo 'pim/agenda: no calendars under '^$mtpt^'/calendars' >[1=2]
	exit nocal
}
# only worth naming the calendar when more than one is in play
tag=0
if(! ~ $#cals 1)
	tag=1

tmp=/tmp/agenda.$pid
fn sigexit { rm -f $tmp }

now=`{date -n}
i=$off
last=`{echo $off + $days | bc}
n=0

while(test $i -lt $last){
	sec=`{echo $now + $i '*' 86400 | bc}
	day=`{date -f YYYY/MM/DD $sec}

	# name, calendar, summary, location -- one line per occurrence,
	# keyed by the file name so a sort interleaves the calendars by time
	{
		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}
				loc=`{sed -n 's/^location: //p' $f}
				echo $"b^'	'^$"c^'	'^$"sum^'	'^$"loc
			}
		}
	} | sort >$tmp

	if(test -s $tmp){
		if(test $n -gt 0)
			echo
		n=1
		date -f 'WWW DD MMM YYYY' $sec
		awk -F'	' -v 'tag='^$tag '{
			if ($1 ~ /^0000-allday-/)
				t = "all day"
			else
				t = substr($1,1,2) ":" substr($1,3,2)
			s = "\t" t "\t" $3
			if ($4 != "")
				s = s "  (" $4 ")"
			if (tag)
				s = s "\t" $2
			print s
		}' $tmp
	}
	i=`{echo $i + 1 | bc}
}
if(~ $n 0)
	echo 'nothing in the next '^$days^' days'
exit 0