#!/bin/sh -e
#
# allow creation of journal entry:
#  - can specify title on invocation (journal This Is The Title)
#  - edit the entry in Markdown
#  - checks there is some content
#  - confirm title (defaulting to any title given earlier)
#  - checks there is a non-empty title
#  - creates journal entry as issue in journal repo
#
# ---
# see: https://github.com/qmacro/thinking-aloud/issues/28

journalrepo="${NOUS-}/thinking-aloud"

main() {
  tmpfile=$(mktemp /tmp/journal.XXXXXX)

  # shellcheck disable=SC2312
  vim -c 'set ft=markdown' "${tmpfile}" \
    && test -s "${tmpfile}" \
    && printf '%s' "Title${*:+ ($*)}? " \
    && read -r t \
    && title="${t:-$*}" \
    && test -n "${title}" \
    && gh issue create \
      --repo "${journalrepo}" \
      --label entry \
      --title "$(date '+%Y-%m-%d %H:%M:%S') ${title}" \
      --body-file "${tmpfile}" \
    && sleep 2 \
    && rm "${tmpfile}"
}

main "$@"
