

#!/bin/sh

TODO_FILE="$HOME/docs/todo.txt"
ARCHIVE_FILE="$HOME/docs/todo-archive.txt"
TODAY="$(date +%Y-%m-%d)"

case "$1" in
  add)
    shift
    echo "($TODAY) $*" >> "$TODO_FILE"
    echo "Added: ($TODAY) $*"
    ;;
  lsa)
    nl -w2 -s'. ' "$TODO_FILE"
    ;;
  ls)
    grep -v '^x ' "$TODO_FILE" | nl -w2 -s'. '
    ;;
  done)
    if [ -z "$2" ]; then
      echo "Usage: $0 done <task-number>"
      exit 1
    fi
    TASK=$(sed -n "${2}p" "$TODO_FILE")
    [ -z "$TASK" ] && { echo "Invalid task number."; exit 1; }
    sed -i "${2}s/^/x ($TODAY) /" "$TODO_FILE"
    echo "Marked done: $TASK"
    ;;
  archive)
    grep '^x ' "$TODO_FILE" >> "$ARCHIVE_FILE"
    sed -i '/^x /d' "$TODO_FILE"
    echo "Archived completed tasks to $ARCHIVE_FILE"
    ;;
  tags)
    if [ -z "$2" ]; then
      echo "Usage: $0 tags <+tag|@context>"
      exit 1
    fi
    grep "$2" "$TODO_FILE" | grep -v '^x ' | nl -w2 -s'. '
    ;;
  help|*)
    echo "Usage: $0 {add <task> | list | done <number> | archive}"
    ;;
esac

