#!/bin/sh
# phlog -- lowering the barrier to phlogging
# see gopher://alexkarle.com/phlog/011.txt
die() {
   echo "$*"
   exit 1
}

[ -z "$1" ] && die "usage: phlog TITLE"

PHLOG=$(dirname "$(dirname "$(readlink -f "$0")")")/phlog

i=1
nextfile() {
   file=$(printf "%03d.txt" $i)
   : $((i+=1))
}

title="$*"
underline="$(echo "$title" | sed 's/./-/g')"

nextfile
while [ -e "$PHLOG/$file" ]; do
   nextfile
done

echo "$title" >"$PHLOG/$file"
echo "$underline" >>"$PHLOG/$file"
date >>"$PHLOG/$file"

ed  "$PHLOG/index.gph" <<EOM
/^\[0|Atom Feed/+a
[0|[$(date +%F)] $title|/phlog/$file|server|port]

wq
EOM

exec "${EDITOR:-vi}" "$PHLOG/$file"