#!/usr/bin/env zsh ## email-tickler-update -- manage a set of 43 mailboxes as a 'tickler' file ## Written by and for Christopher League ## but released to the public domain. ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ### Settings ### mail_spool=/var/mail mail_dir=mail today=$(date +%d) # 03, 14, 31, ... month=$(date +%m-%b) # 02-Feb, 11-Nov, ... day_mbox=${mail_dir}/days/${today} month_mbox=${mail_dir}/months/${month} users=($*) ### Helper functions ### ## Maybe run a command; maybe print it instead. If this script is run ## with DRYRUN set (to anything except empty string), it will avoid ## taking any real actions, and just print the commands. run() { if [[ -z $DRYRUN ]]; then # do it for real $* else # output only print '%' $* fi } ## Run a command, but exit with message on error. guard() { run $* if [[ $? != 0 ]]; then print "FATAL($?): $*" exit $? fi } readable() { if [[ ! -r $1 ]]; then print unreadable: $1 exit 1 fi } ### Primary actions ### ## Append given file to mail spool, then empty it out. move_to_spool() { cat $1 >>${user_mail_spool} print -n >$1 } ## This wraps the above with the proper locking protocol, and does it ## only if given mailbox is non-empty. protected_move() { guard lockfile $1.lock if [[ -s $1 ]]; then guard lockfile ${user_mail_spool}.lock guard move_to_spool $1 guard rm -f ${user_mail_spool}.lock fi guard rm -f $1.lock } ### Main loop ### for u in $users; do user_mail_spool=${mail_spool}/$u readable ${user_mail_spool} readable ~$u/${day_mbox} readable ~$u/${month_mbox} protected_move ~$u/${day_mbox} if [[ $today == 01 ]]; then protected_move ~$u/${month_mbox} fi done exit 0