#! /bin/bash

############################################################################################
#
# genentry.sh - Script to generate a new entry source file for SBWG from v0.8 onwards
# Version: 0.2
# Last edited: 2020-04-02
#
# Expected arguments:
# $1: File name of the entry source file.
# $2: Title of the entry. Just put it in quotes.
# $3 and every argument after that: tags (e.g.: cat:Category top:Topic1 lang:en)
#
############################################################################################

if [ $# -eq 0 ]; then
  echo
  echo "This script creates a template for a entry source file for SBWG."
  echo
  echo "Expected title of blog entry as argument(s). The title does not have to be escaped."
  echo
  echo "A file with a name corrosponding to the title will be written to the current working"
  echo "directory immediately. Then a text editor is opened."
  echo
  echo "Examples:"
  echo "$0 \"An Exciting New Blog Post\""
  echo "$0 My Views On Presidential Pets"
  echo
  exit
fi

title="${@}"
filename=$(echo "${title}"|sed -e 's: :_:g'|tr -d '.!\"§$%&()[]{}?´`#\\/:*?"<>|[:space:]')
#filename=$(echo "${title}"|tr -d '.!\"§$%&()[]{}?´`#-\\/:*?"<>|[:space:]')
#filename="${filename:-$(date +%Y-%m-%d_%H-%M-%S_%N)}"

[ -f "${filename:=$(date +%Y-%m-%d_%H-%M-%S_%N)}" ] \
  && { echo "A file with the name '${filename}' already exists. No, I'm not going to overwrite it."; exit 1; }

{
  echo "note:Source file generated by genentry.sh"
  echo "title:${@}"
  echo "created:$(date -I)"
  echo "${authorname:+author:$authorname}"
  echo
  echo "<p></p>"
} > "${filename}"

echo "Created file '${filename}'."

#"${EDITOR:-nano} -u +6,4 ${filename}"
nano -u +6,4 "${filename}"
