#!/bin/bash
# /usr/local/bin/triweekly
# http://crystalfaeries.net/posix/bin/triweekly
# celeste:crystalfaery 2016-10-27 21:29:00+00:00
# We maintain event timestamps, for which,
# if our timestamp is 21 days old,
# we notify, and reset the timestamp
# option -l displays all timestamps,
# which are displayed anyway if any new event added
# Crontab is a wonderful facility but unable to choose an "every 3 weeks" schedule...
# therefore, we assist cron with this script, which is executed nightly by cron.
# 00 00 * * * /usr/local/bin/triweekly
# manual invocation to list timestamps being tracked:
# triweekly -l
# manual invocation to initiate a new, or reset timer of, existing item(s):
# triweely alpha beta
temporary=`mktemp` || exit -1
cd # there's no place like home, where the user timestamps live
# if called with arguments, those are the names of the events we are timing
if [ "$1" == "-v" ]
then
echo "`ls -Flad $0`"
head $0
exit 0
fi
if [ "$1" == "--version" ]
then
echo "`ls -Flad $0`"
head $0
exit 0
fi
if [ "$1" == "-h" ]
then
echo "$0 accepts as arguments the names of events for which you are requesting a 3 week timer."
echo "If the event already existed, its timer is reset to 3 weeks."
echo "If any event was specified, all events are listed."
echo "Optional -l flag forces listing of timestamps."
exit 0
fi
if [ "$1" == "--help" ]
then
echo "$0 accepts as arguments the names of events for which you are requesting a 3 week timer."
echo "If the event already existed, its timer is reset to 3 weeks."
echo "If any event was specified, all events are listed."
echo "Optional -l flag forces listing of timestamps."
exit 0
fi
let list=1 # false
if [ "$1" == "-l" ]
then
let list=0 # true
shift
fi
while [[ $# -gt 0 ]] ;
do
/usr/local/bin/now >> ."$1".triweekly.txt # UTC Timestamp inside file
shift # we time-stamped our named event, will notify in 3 weeks
let list=0 # true
done
if [[ $list -eq 0 ]]
then
ls -Flart .*.triweekly.txt # all my triweekly events are stored in my home directory
fi
# now find any timestamp three weeks old and notify
find . -maxdepth 1 -iname '.*.triweekly.txt' -daystart -mtime +20 | sed 's/^\.\/\.//' | sed 's/.triweekly.txt$//' > \
$temporary
cat "$temporary" 1>&2
if [ -s $temporary ]
then # a 3 week timer expired, therefore notify in multiple ways
# delete old TriWeekly events from Calendar
grep -v TriWeekly ~/.calendar/calendar > /tmp/$$
for item in `cat $temporary`;
do
# append new TriWeekly events onto Calendar
echo "`date -u --rfc-3339=date | sed 's/^[0-9][0-9][0-9][0-9]-//g;s/-/ /g'` TriWeekly $item" >> /tmp/$$
/usr/local/bin/now >> .$item.triweekly.txt && \
mail -s "TriWeekly $item" `/usr/bin/whoami` < .$item.triweekly.txt || \
exit -1 # if notify was successful then reset the timer else error exit
done
# install new Calendar file
mv /tmp/$$ ~/.calendar/calendar
fi
rm /tmp/$$ $temporary 2>/dev/null
exit 0 # pau
syntax highlighted by Code2HTML, v. 0.9.1