#!/bin/sh
#
# todo
#
# ---
# source: https://lyngvaer.no/pub/code/scripts/todo

umask 0177

: "${TODO:=${HOME}/TODO}"
: "${TEMP:=/tmp/todo.tmp}"

[ $# -eq 0 ] && {
  nl -n ln <"${TODO}"
  exit 0
}

# portable alternative for `sed` in-place flag
in_place() {
  : >"${TEMP}" || exit 1
  # shellcheck disable=SC2015
  sed "$@" <"${TODO}" >"${TEMP}" && mv "${TEMP}" "${TODO}" || rm "${TEMP}"
}

# shellcheck disable=SC2249
case "$1" in
  add)
    shift
    echo "$@" >>"${TODO}"
    ;;
  rm)
    shift
    in_place "$*d"
    ;;
esac
