#!/bin/sh -u # # Stash strings for later. # Adapted from noteurl. # - Cameron Simpson 15nov1996 # # Streamlined the logic. - cameron, 16feb1998 # Fix race condition on tag existence. - cameron, 14sep1998 # : ${TMPDIR:=/tmp} : ${LOGDIR:=$HOME/var/log} cmd=`basename "$0"` usage="Usage: $cmd [-t tag] notefile [strings...] -d date Make tag line for specified date. -p pfx Addenda prefix (default: a tab). -s sfx Addenda suffix. -t tag Tag line. -H hhmm hh:mm string." notedir=$LOGDIR/notes notefile= when=now tag= tagfmt='%a %b %d %Y' hhmm= pfx=' ' sfx= locked= badopts= while [ $# -gt 0 ] do case $1 in -d) when=$2; shift ;; -p) pfx=$2; shift ;; -s) sfx=$2; shift ;; -t) tag=$2; shift ;; -H) hhmm=$2; shift ;; -l) locked=1 ;; --) shift; break ;; -?*) echo "$0: unrecognised option: $1" >&2 badopts=1 ;; *) break ;; esac shift done [ $# = 0 ] || { notefile=$1; shift; } [ -n "$notefile" ] || { echo "$0: missing notefile" >&2; badopts=1; } [ $badopts ] && { echo "$usage" >&2; exit 2; } # clean up notefile spec case $notefile in /*) ;; *) notefile=$notedir/$notefile ;; esac [ -f "$notefile" ] || { echo "$0: missing notefile: $notefile" >&2 exit 1 } # default tag if none supplied [ -n "$tag" ] || tag=`date -d "$when" "+$tagfmt"` || exit 1 [ -n "$hhmm" ] || hhmm=`date -d "$when" '+%H:%M'` || exit 1 # loopback - lock the file and rerun [ $locked ] \ || exec lock "$notefile" "$0" -l -p "$pfx" -s "$sfx" -t "$tag" -H "$hhmm" -- "$notefile" ${1+"$@"} # collect lines to add tmpf=$TMPDIR/$cmd.$$ trap 'rm -f "$tmpf"' 0 trap 'rm -f "$tmpf"; exit 1' 1 2 13 15 if [ $# = 0 ] then cat else for line do printf "%s\\n" "$line" done fi \ | sed -e "s^$hhmm " -e "s^$pfx" -e "s\$$sfx" >"$tmpf" || exit 1 [ -s "$tmpf" ] || exit 0 # nothing to add # ensure the tagline exists case "$tag" in */*) tagptn=`sedstrs "s|/|\\\\\\\\/|g" "$tag"` ;; *) tagptn=$tag ;; esac grep "^$tagptn" "$notefile" >/dev/null \ || bsed -s "1i\ $tag" "$notefile" \ || exit 1 ## NB: no exec because the trap needs to run ##set -x bsed -s "/^$tagptn/r $tmpf" "$notefile"