#!/bin/sh

# _posts/2017-07-13-csh-sensible-defaults.md

ts=`date +%Y-%m-%d`
title=$1
category=$2
emacs=""

if test -z "$title"; then
    while :; do
        echo -n "Enter blog post title: "
        read title
        echo -n "Title <$title> ok [Yn]? "
        read yesno
        case $yesno in
            N|n|no|No|NO)
                :
                ;;
            *)
                break
                ;;
        esac
    done
else
    # we're running from within emacs
    emacs=1
fi

if test -z "$category"; then
    while :; do
        echo -n "Enter category: "
        read category
        echo -n "Category <$category> ok [Yn]? "
        read yesno
        case $yesno in
            N|n|no|No|NO)
                :
                ;;
            *)
                break
                ;;
        esac
    done
fi

slug=`echo "$title" | tr '[A-Z]' '[a-z]' | sed -e 's/ /-/g'`
post="_posts/${ts}-$slug.md"

cat << EOF > $post
---
layout: post
title: "$title"
tags:
- $category
---


EOF

if test -n "$emacs"; then
    echo $post
else
    $EDITOR $post

    echo "Wrote $post"
    echo "Consider running 'make force' inside jail"
fi
