#!/bin/bash
#
# todo - manage a todo list
# the file is just plain text, with one line per task.
# this script just provides "shorter" commands to append to file + display its content.
# for more complex tasks, use other tools like `sed`.

# where's the file ?
TODO=${TODO:-${HOME}/.todo}

list() {
  # woah much clever!!
  test -f "${TODO}" && nl "${TODO}"
}

append() {
  # append all arguments "as-is" to the file
  echo "$*" >> "${TODO}"
}

delete() {
  test -n "$1" || exit 1
  sed -i "${1}d" "${TODO}" 
}

# delete line "$2" (see delete() function)
test "$1" = '-d' && delete "$2" && exit 0

# append arguments to the file, or print it otherwise
# shellcheck disable=SC2015
test -n "$*" && append "$*" || list

exit 0
