#!/bin/bash cmd=$1 shift function do_init { if [[ $# -gt 0 ]] ; then echo "No arguments expected" >&2 exit 1 fi if [[ -d $HOME/.notes ]] ; then echo "Directory ~/.notes already exists!" >&2 exit 1 fi git init $HOME/.notes } function do_new { cd "$HOME/.notes" tmpfile="$(mktemp '/tmp/.notes.tmpfile.XXXX.md')" rm -f "$tmpfile" file="$(date -u '+%Y-%m-%dT%H%M%S')".md "$EDITOR" "$tmpfile" if [[ "$?" -eq 0 && -f "$tmpfile" ]] ; then mv "$tmpfile" "$file" git add "$file" git commit -m "New note taken: $file" else echo "Nothing added" fi rm -f "$tmpfile" } function do_grep { cd "$HOME/.notes" grep "$@" * } function do_git { cd $HOME/.notes git "$@" } function do_edit { cd "$HOME/.notes" file="$1" if [[ "$#" -ne 1 ]] ; then echo "Exactly one argument required">&2 exit 1 fi if [[ ! -f "$file" ]] ; then echo "No such file. Please use notes new.">&2 exit 1 fi "$EDITOR" "$file" if [[ "$?" -eq 0 ]] ; then git add "$file" git commit -m "Updated $file" fi } case $cmd in init) do_init "$@" ;; new) do_new "$@";; grep) do_grep "$@" ;; edit) do_edit "$@" ;; git) do_git "$@" ;; *) echo "$cmd: unknown command" >&2 exit 1;; esac