Enhanced `todo` by using `getopt`
[todo.git] / todo.sh
blobb191bee735936aab043b983106af347c97bdb937
1 #!/bin/sh
2 # TODO, a cli GTD toy
3 # Copyright (C) 2008 Alejandro Mery <amery@geeks.cl>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 TODOTMP="${TMP:-/tmp}/.todo.$$"
19 TODODIR="$HOME/.todo"
21 TAGS=
22 MODE=new
24 todo_usage()
26 [ $# -eq 0 ] || echo "ERROR: $*" >&2
27 echo "Usage: $0 [-t tag]* [message]" >&2
29 exit 1
32 todo_trap()
34 echo "Ctrl-C pressed, aborting." >&2
35 rm -rf "$TODOTMP"
36 exit 1
39 trap ':' INT
41 options=$( getopt -o "$t:" -- "$@" )
42 if [ $? -ne 0 ]; then
43 todo_usage
46 # load new arguments list
47 eval set -- "$options"
49 while [ $# -gt 0 ]; do
50 case "$1" in
51 -t) TAGS="$TAGS $2"; shift ;;
53 --) shift; break ;;
54 -*) todo_usage unknown option "$1"
56 esac
57 shift
58 done
60 TAGS=$( echo "$TAGS" | tr 'a-z' 'A-Z' )
62 todo_new()
64 DATE=
65 # find a unique date/hash
67 while [ -z "$DATE" ]; do
68 DATE=$(date --rfc-3339=seconds)
69 DATEHASH=$( echo "$DATE" | sha1sum | cut -d' ' -f1 )
70 DATEHASH_1=$( echo "$DATEHASH" | cut -c 1 )
71 ENTRYDIR="$TODODIR/entries/$DATEHASH_1"
72 ENTRY="$ENTRYDIR/$DATEHASH"
74 if [ -e "$ENTRY" ]; then
75 DATE=
76 sleep 1
77 else
78 mkdir -p "$ENTRYDIR"
80 done
82 if [ $# -gt 0 ]; then
83 echo "$*" > "$TODOTMP"
84 echo "Added: $DATE" >> "$TODOTMP"
85 else
86 echo "Subject: ..." > "$TODOTMP.empty"
87 echo "Added: $DATE" >> "$TODOTMP.empty"
88 cp "$TODOTMP.empty" "$TODOTMP"
90 ${EDITOR:-vi} "$TODOTMP"
92 if cmp -s "$TODOTMP" "$TODOTMP.empty"; then
93 rm -f "$TODOTMP" "$TODOTMP.empty"
94 return
98 mv "$TODOTMP" "$ENTRY"
100 for tag in $TAGS INBOX; do
101 [ -d "$TODODIR/$tag" ] || mkdir "$TODODIR/$tag"
103 ln -s "../entries/$DATEHASH_1/$DATEHASH" "$TODODIR/$tag/$DATEHASH"
104 done
106 echo "$DATEHASH"
109 case "$MODE" in
110 new) todo_new "$@"
112 esac