#!/bin/bash
# Source this and then run `todo`

# a simple way to manage your todo list
# Usage:
#  todo                    -- pull latest repo version and edit ~/todo
#  todo [any git command]  -- manage todo for easy syncing, assuming ~/todo is
#                                a symlink that points to a file in a git repo
#
# Suggested use: make a git repo for holding your todo list, then make
# a symlink called 'todo' that points to your todo list in your home directory
#

todo() {
   TODO_DIR="${FDOC:-"$(dirname "$(realpath ~/todo)")"}"
   TODO_FILE="${FTD:-"$(realpath ~/todo)"}"
   if [ -z "$*" ];then
      ( # subshell to protect against directory changes
      cd "$TODO_DIR" || return

      # pull the latest commits
      git rev-parse &&
         git pull

      # open the file
      $EDITOR "$TODO_FILE"

      # commit and push the file if it's in a git repo and the file has changed
      if git rev-parse && ! git diff --exit-code "$TODO_FILE"; then
         git commit "$TODO_FILE" -m 'todo' &&
            git push
      fi
      )

   elif [ "$*" = "cd" ]; then
      cd "$TODO_DIR" || return
   else
      git -C "$TODO_DIR" $@
   fi
}