-
Notifications
You must be signed in to change notification settings - Fork 7
/
tickler
executable file
·212 lines (196 loc) · 6.89 KB
/
tickler
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
# Tickler action script, makes, then processes the tickler files
# Copyright 2011 Paul Mansfield <paul.mansfield@mansteck.co.uk>
# License: GPL, http://www.gnu.org/copyleft/gpl.html
useage()
{
sed -e 's/^ //' <<EndUsage
tickler [init]
Create and process tickler files in tickler files: $TICKLER_DIR.
Set $TICKLE_PREPEND if you want prepend something to the moved lines.
EndUsage
exit 1
}
# Setup some variables
variables () {
todayday=$(date '+%d');
todaymonth=$(date '+%m');
todayyear=$(date '+%Y');
now="$today-$month-$year"
statefile="$TICKLER_DIR/.tickler_state.txt"
if [ -f ${statefile} ]; then
statefile_day=$(awk -F - '{print $1}' ${statefile})
statefile_month=$(awk -F - '{print $2}' ${statefile})
statefile_year=$(awk -F - '{print $3}' ${statefile})
else
statefile_day=01
statefile_month=01
statefile_year=1970
fi
}
# Update the state file
set_state () {
echo "${todayday}-${todaymonth}-${todayyear}" > ${statefile}
exit 0
}
# Turn on messaging if verbose is set
message () {
if [ $TODOTXT_VERBOSE -eq 1 ]; then
echo $@
fi
}
# Create the directories etc if needed
init () {
# Check then initialize the directories
if [ -e $TICKLER_DIR ]; then
if [ ! -d $TICKLER_DIR/days ] ; then
for i in $(seq -w 1 31) ; do
mkdir -p $TICKLER_DIR/days/$i || exit 1
done
fi
if [ ! -d $TICKLER_DIR/months ] ; then
for i in $(seq -w 1 12) ; do
mkdir -p $TICKLER_DIR/months/$i || exit 1
done
fi
fi
}
# Function to prepend some text to moved items if variable set
prepend () {
if [ ! -z "${TICKLER_PREPEND}" ]; then
sed "s/^/${TICKLER_PREPEND}/"
else
sed 's///g'
fi
}
do_all () {
# Treat as first run, so move all to current todo file
for file in $TICKLER_DIR/${1}/*/*.txt ; do
if [ -f $file ]; then
cat $file | prepend >> "$TODO_FILE" || exit 1
message "Tickle file for `echo ${1} | sed 's/s$//'` `echo $file | awk -F '/' '{print $NF-1+1}'` moved to todo file"
message $(cat $file | prepend)
rm -rf $file || exit 1
fi
done
}
some_days () {
# Been completed this month, just run for required days
for day in $(seq -w ${statefile_day} ${todayday}) ; do
if [ -f $TICKLER_DIR/days/${day}/${day}.txt ]; then
cat $TICKLER_DIR/days/${day}/${day}.txt | prepend >> "$TODO_FILE" || exit 1
message "Tickle file for day `echo ${day} | awk '{print $NF-1+1}'` moved to todo file"
message $(cat $TICKLER_DIR/days/${day}/${day}.txt | prepend)
rm -rf $TICKLER_DIR/days/${day}/*.txt || exit 1
fi
done
}
catchup_to_end_of_month () {
# Catch up to the end of the month
for day in $(seq -w ${statefile_day} 31) ; do
if [ -f $TICKLER_DIR/days/${day}/${day}.txt ]; then
cat $TICKLER_DIR/days/${day}/${day}.txt | prepend >> "$TODO_FILE" || exit 1
message "Tickle file for day `echo ${day} | awk '{print $NF-1+1}'` moved to todo file"
message $(cat $TICKLER_DIR/days/${day}/${day}.txt | prepend)
rm -rf $TICKLER_DIR/days/${day}/*.txt || exit 1
fi
done
}
catchup_from_beginning_of_month () {
# Catchup from start of month to now
for day in $(seq -w 1 ${todayday}) ; do
if [ -f $TICKLER_DIR/days/${day}/${day}.txt ]; then
cat $TICKLER_DIR/days/${day}/${day}.txt | prepend >> "$TODO_FILE" || exit 1
message "Tickle file for day `echo ${day} | awk '{print $NF-1+1}'` moved to todo file"
messae $(cat $TICKLER_DIR/days/${day}/${day}.txt | prepend)
rm -rf $TICKLER_DIR/days/${day}/*.txt || exit 1
fi
done
}
not_this_month () {
# Not run this month, so move required months
for month in $(seq -w ${statefile_month} ${todaymonth}) ; do
if [ -f $TICKLER_DIR/months/${month}/${month}.txt ]; then
cat $TICKLER_DIR/months/${month}/${month}.txt | prepend || exit 1
message "Tickle file for month `echo ${month} | awk '{print $NF-1+1}'` moved to todo file"
message $(cat $TICKLER_DIR/months/${month}/${month}.txt | prepend)
rm -rf $TICKLER_DIR/months/${month}/${month}.txt || exit 1
fi
done
}
till_end_of_year () {
# Not run this current year, so catch up the rest of the year
for month in $(seq -w ${statefile_month} 12); do
if [ -f $TICKLER_DIR/months/${month}/${month}.txt ]; then
cat $TICKLER_DIR/months/${month}/${month}.txt | prepend >> "$TODO_FILE" || exit 1
message "Tickle file for month `echo ${month} | awk '{print $NF-1+1}'` moved to todo file"
message $(cat $TICKLER_DIR/months/${month}/${month}.txt | prepend)
rm -rf $TICKLER_DIR/months/${month}/${month}.txt || exit 1
fi
done
}
from_beginning_of_year () {
# Not run since start of year, so catch up required months
for month in $(seq -w 1 ${todaymonth}); do
if [ -f $TICKLER_DIR/months/${month}/${month}.txt ]; then
cat $TICKLER_DIR/months/${month}/${month}.txt | prepend >> "$TODO_FILE" || exit 1
message "Tickle file for month `echo ${month} | awk '{print $NF-1+1}'` moved to todo file"
message $(cat $TICKLER_DIR/months/${month}/${month}.txt | prepend)
rm -rf $TICKLER_DIR/months/${month}/${month}.txt || exit 1
fi
done
}
# Main piece of work
variables
# Check if being run for help useage
if [ "$2" = "useage" ]; then
useage
fi
# Should we run init
if [ "$2" = "init" ]; then
init
exit 0
fi
# No state file, treat as new, so run init, incase we haven't been run
if [ ! -f ${statefile} ] ; then
init
do_all days
do_all months
set_state
fi
# Not run for over a year
if [ ${statefile_year} -ne ${todayyear} -a ${statefile_month} -lt ${todaymonth} ]; then
do_all months
do_all days
set_state
fi
# Last run last month
if [ ${statefile_year} -eq ${todayyear} -a ${statefile_month} -eq $((${todaymonth#0}-1)) ]; then
catchup_to_end_of_month
catchup_from_beginning_of_month
not_this_month
set_state
fi
# Not run this year, but less than a year.
if [ ${statefile_year} -ne ${todayyear} -a ${statefile_month} -gt ${todaymonth} ]; then
do_all days
till_end_of_year
from_beginning_of_year
set_state
fi
# This year, but not this month
if [ ${statefile_year} -eq ${todayyear} -a ${statefile_month} -ne ${todaymonth} ]; then
not_this_month
set_state
fi
# This year and this month
if [ ${statefile_year} -eq ${todayyear} -a ${statefile_month} -eq ${todaymonth} -a ${statefile_day} -ne ${todayday} ]; then
some_days
set_state
fi
# Today
if [ ${statefile_year} -eq ${todayyear} -a ${statefile_month} -eq ${todaymonth} -a ${statefile_day} -eq ${todayday} ]; then
exit 0
fi
# Something wrong, exit
exit 1