#!/bin/sh
#
# ablog_item - create a new item for the ACME Blogmaker
#
# This script figures out the next item number to be used and creates
# a blank item, then runs your editor on it.

if [ ! -d .ablog_items ] ; then
    echo "$0: .ablog_items directory not found" >&2
    exit 1
fi
if [ ! -d .ablog_templates ] ; then
    echo "$0: .ablog_templates directory not found" >&2
    exit 1
fi
. .ablog_templates/setup.sh

date=`date -u "+$rfc822_format"`

n=`ls .ablog_items | egrep '^[1-9][0-9]*$' | sort -nr | head -1`
if [ "$n" = '' ] ; then
    n=1
else
    n=`expr "$n" + 1`
fi

i=".ablog_items/$n"
if [ -f "$i" ] ; then
    echo "$0: item file already exists - shouldn't happen" >&2
    exit 1
fi

cat < .ablog_templates/item > "$i"

${VISUAL:-${EDITOR:-vi}} "$i"

echo 'Running "ablog"...'
ablog
