#!/bin/bash
# A bash script to make posting new phlog posts a bit
# easier.
#
# To set up a new phlog, do
#
#    whip setup
#
# This makes a 'phlog' directory in your 'public_gopher'
# directory and creates two files inside it,
# 'gophermap', and '.phlog_index'.
#
# To write a new phlog post, do
#
#    whip new "My Phlog Post Title"
#
# This makes a new text file for your post, opens it for
# editing and, once you've quit your text editor,
# updates the '.phlog_index' file. (You can set your
# preferred text editor below, it defaults to `nano -r
# 56`).
#
# NB: Phlog posts should be hard wrapped at less than 70
# characters.  You'll probably want to add a link to
# your phlog from your main gophermap
# (~/public_gopher/gophermap)
#
# TODO: add whip_html and whip_rss as subcommands

set -o errexit
set -o pipefail

# Config
gopher_dir="$HOME/public_gopher"
phlog_dir="$gopher_dir/phlog"
phlog_gophermap="$phlog_dir/gophermap"
phlog_index_file="$phlog_dir/.phlog_index"
phlog_header="!Welcome to ~$USER's phlog."
phlog_index_cmd="=/usr/bin/cat $phlog_index_file"
phlog_footer="1Back\t/~$USER/\n."
phlog_editor="/usr/bin/vim +6 "

if [ -z ${phlog_editor+x} ]; then
       phlog_editor="/usr/bin/nano -r 56"
fi
if [ -d "$phlog_dir" ] && \
 [ -f "$phlog_gophermap" ] && \
 [ -f "$phlog_index_file" ];then
 phlog_exists="true"
else
 phlog_exists="false"
fi

usage() {
 cat << END
USAGE:
 ${0##*/} [command]

COMMANDS:
 help                  Show this help message
 setup                 Create a phlog directory and gophermap
 new <title>           Add a new phlog post
END
}

phlog_setup() {
 mkdir -p "$phlog_dir"
 touch "$phlog_dir/.phlog_index"
 {
   printf "\n"
   printf "%s\n\n" "$phlog_header"
   printf "%s\n\n" "$phlog_index_cmd"
   printf "%b\n" "$phlog_footer"
 } >> "$phlog_gophermap"
 printf "%s\n\n" "Phlog setup successful!"
 printf "%s\n" "Don't forget to link to your"
 printf "%s\n" "phlog from your main gophermap:"
 printf "%s\n" "$gopher_dir/gophermap"
}

phlog_new() {
 post_title="$*"
 post_slug="$(echo "$post_title" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')"
 post_date="$(date +"%Y-%m-%d")"
 post_file="$post_date-$post_slug.txt"
 touch "$phlog_dir/$post_file"
 {
   printf "\n%s\n" "# $post_title"
   printf "%s\n" "## $post_date"
 } >> "$phlog_dir/$post_file"
$phlog_editor "$phlog_dir/$post_file"
}

update_index() {
 cp "$phlog_index_file" "$phlog_index_file.backup"
 printf "%b" "0$post_date $post_title\t$post_file"\
   >> "$phlog_index_file"
     sort -o "$phlog_index_file" -r "$phlog_index_file"
}

shift $((OPTIND - 1))
subcommand=$1
if [ -z "$1" ]; then
 printf "%s\n" "Command missing!"
 usage
 exit 1
elif [ "$1" != "help" ] && \
 [ "$1" != "setup" ] && \
 [ "$1" != "new" ]; then
 printf "%s\n" "Invalid command: $1"
 usage
 exit 1
fi
shift
case "$subcommand" in
 help)
   usage
   exit 0
   ;;
 setup)
   if [ "$phlog_exists" == "true" ];then
     printf "%s\n" "It looks like you already have a phlog set up!"
     printf "%s\n\n" "To start again, remove $phlog_dir"
     usage
     exit 1
   fi
   phlog_setup
   ;;
 new)
   if [ "$phlog_exists" == "false" ];then
     printf "%s\n" "Phlog files missing!"
     printf "%s\n\n" "Run 'whip setup' to get started"
     usage
     exit 1
   fi
   phlog_new "$@"
   update_index
   ;;
esac