#!/bin/sh
#
# ual - notes that can be viewed just like man pages
# dependencies: pandoc | trash-cli (optional)

CHEATSHEET_PATH="${CHEATSHEET_PATH:-${XDG_DATA_HOME:-${HOME}/.local/share}/ual}"

usage() {
  printf '%s' "\
$(basename "$0")
  a simple script to write/manage notes that can be viewed like man pages

commands:
  [e]dit [name] - Edit a page with \$EDITOR.
  ls            - List all pages.
  rm     [name] - Delete a page.
  sync          - Sync pages with git.

flags:
  -h, --help    - Display this message.
"
  exit 0
}

ual() {
  if [ "$#" -lt 1 ]; then
    echo "Which ual page do you want?"
    return
  fi

  case "$1" in
    -h | --help)
      usage
      ;;
    e | edit)
      if [ "$#" -lt 2 ]; then
        echo "Which ual page do you want to edit?"
        return
      fi

      mkdir -p "${CHEATSHEET_PATH}"
      cd "${CHEATSHEET_PATH}" || exit 1

      note="$2.md"
      if [ ! -f "${note}" ]; then
        printf '# NAME\n\n%s\n\n' "$2" >"${note}"
      fi
      ${EDITOR:-vi} "${note}"
      ;;
    rm)
      if [ "$#" -lt 2 ]; then
        echo "Which ual page do you want to remove?"
        return
      fi
      note="${CHEATSHEET_PATH}"/$2.md
      if [ ! -f "${note}" ]; then
        echo "No ual entry for $1" >&2
        exit 1
      fi
      if command -v trash-put >/dev/null 2>&1; then
        trash-put "${note}"
      else
        rm "${note}"
      fi
      ;;
    ls)
      for note in "${CHEATSHEET_PATH}"/*; do
        command basename -- "${note%.*}"
      done
      ;;
    sync)
      printf 'Syncing notes... '
      cd "${CHEATSHEET_PATH}" || exit 1
      git stash push --quiet \
        && git pull --rebase >/dev/null 2>&1 \
        && git stash pop --quiet 2>/dev/null
      git add -- *.md
      if ! git diff-index --quiet HEAD --; then
        git commit --quiet --verbose 2>/dev/null
        git push >/dev/null 2>&1
      fi
      echo "Done."
      ;;
    *)
      note="$1"
      if [ ! -f "${note}" ]; then
        note="${CHEATSHEET_PATH}"/$1.md
      fi

      # note="${CHEATSHEET_PATH}"/$1.md
      if [ ! -f "${note}" ]; then
        printf '%s\n' "No ual entry for $1" >&2
        exit 1
      fi

      if ! command -v pandoc >/dev/null 2>&1; then
        printf '%s\n' "warning: pandoc not found, viewing note as plain text" >&2
        "${PAGER:-less}" "${note}"
      fi

      title=$(printf '%s' "$1" | tr '[:lower:]' '[:upper:]')
      # display modification time
      date=$(date -r "${note}" "+%B %d, %Y")
      # author=$(id -F 2>/dev/null || getent passwd "${LOGNAME:-${USER}}" | cut -d ':' -f 5)
      author=human
      tmp=$(mktemp)

      # shellcheck disable=SC2016
      sed -E 's/`[^`]+`/_&_/g' "${note}" \
        | pandoc \
          --standalone \
          --from markdown \
          --to man \
          --metadata title="${title}" \
          --metadata date="${date}" \
          --metadata author="${author}" \
          --metadata section=ual \
          --output "${tmp}"

      man "${tmp}"
      rm -f "${tmp}"
      ;;
  esac
}

ual "$@"
