Fixed bug in `getopt` call, and some code relocated
[todo.git] / todo.sh
blob37b040e55941b9da6924bc4052cc3a950cf4f59d
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 todo_usage()
20 [ $# -eq 0 ] || echo "ERROR: $*" >&2
21 echo "Usage: $0 [-t tag]* [message]" >&2
23 exit 1
26 todo_trap()
28 echo "Ctrl-C pressed, aborting." >&2
29 rm -rf "$TODOTMP"
30 exit 1
33 trap ':' INT
35 options=$( getopt -o "t:" -- "$@" )
36 if [ $? -ne 0 ]; then
37 todo_usage
40 # load new arguments list
41 eval set -- "$options"
43 TAGS=
44 MODE=new
46 while [ $# -gt 0 ]; do
47 case "$1" in
48 -t) TAGS="$TAGS $2"; shift ;;
50 --) shift; break ;;
51 -*) todo_usage unknown option "$1"
53 esac
54 shift
55 done
57 TAGS=$( echo "$TAGS" | tr 'a-z' 'A-Z' )
59 TODODIR="$HOME/.todo"
61 todo_new()
63 TODOTMP="${TMP:-/tmp}/.todo.$$"
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