#!/usr/local/bin/bash
############################################################################
# gtd: A simple tool for managing GTD next action lists on Unix systems.
#      v1.0, by Tammy Cravit <tammy@wordsofwonder.net>, 2005/05/10
#      Some rights reserved. Licensed under a Creative Commons license.
#      See http://creativecommons.org/licenses/by-sa/2.0/ for details.
#
# "gtd help" for help
############################################################################
# $Id: gtd.sh,v 1.3 2005/05/11 17:56:07 tlcravit Exp $
############################################################################
# Revision History
# ----------------
#
# $Log: gtd.sh,v $
# Revision 1.3  2005/05/11 17:56:07  tlcravit
# Merged some changes contributed by Karl Vogel <vogelke@pobox.com>:
# * Set PATH and umask for safety.
# * Use EDITOR with a safe default value for the text editor.
# * Usage message in its own function.
# * Version information derived from RCS strings.
# * Removed some cat commands.
#
# Revision 1.2  2005/05/11 17:08:38  tlcravit
# Added code to change the prefix for entries
#
# Revision 1.1  2005/05/10 23:40:50  tlcravit
# Initial revision
#
############################################################################

GTD_FILE="$HOME/.gtd"
ENTRY_PREFIX="at "
PATH=/bin:/usr/bin:/usr/local/bin:$HOME/bin
EDITOR=${EDITOR:-vi}

export PATH EDITOR
umask 022

usage () {
        mysedscr='s/.RCSfile: //
        s/,v . .Revision: / v/
        s/ \$//g'
        myrevno='$RCSfile: gtd.sh,v $ $Revision: 1.3 $'
        ver=`echo "$myrevno" | sed -e "$mysedscr"`

    cat <<EndUsage
Usage: `basename $0` <command> [context] [args]

Database file is $GTD_FILE

Command is one of:
      add - add an entry to a context. Context will be prompted for if
        not supplied.
     list - list all entries in the database, or all entries in a 
        specified context.
     find - find all entries in the database, or all entries in the
        supplied context matching the supplied search text.
     edit - edit all entries in the database, or all entries in the
        specified context.
     sort - re-sort the database file.
 contexts - list all the contexts defined in the database file.
        (new contexts are created by adding items to them)
     help - display this help message.

$ver by Tammy Cravit <tammy@wordsofwonder.net>
Licensed under a Creative Commons License. See
   http://creativecommons.org/licenses/by-sa/2.0/
EndUsage
}

[ -f $GTD_FILE ] || touch $GTD_FILE

case $1 in
    add)
    if [ "$2" = "" ]
        then
        echo -n "at: "
        read context
    else
        context=$2
    fi
    echo -n "do: "
    read action
    echo "$ENTRY_PREFIX$context: $action" >> $GTD_FILE
    ;;
    list)
    if [ "$2" = "" ]
        then
        cat $GTD_FILE
    else
        grep "^$ENTRY_PREFIX$2" $GTD_FILE
    fi
    ;;
    find)
    if [ "$2" = "" ]
        then
        cat $GTD_FILE | egrep "$3"
    else
        grep "^$ENTRY_PREFIX$2" $GTD_FILE | egrep "$3"
    fi
    ;;
    edit)
    tmp_file=/tmp/.gtd.$LOGNAME.$$
    if [ "$2" = "" ]
        then
        cat $GTD_FILE > $tmp_file
    else
        grep "^$ENTRY_PREFIX$2" $GTD_FILE > $tmp_file
    fi
    $EDITOR $tmp_file
    if [ "$2" = "" ]
        then
        cp $GTD_FILE $GTD_FILE.bak
        cp $tmp_file $GTD_FILE
    else
        grep -v "^$ENTRY_PREFIX$2" $GTD_FILE >> $tmp_file
        cp $GTD_FILE $GTD_FILE.bak
        cp $tmp_file $GTD_FILE
    fi
    rm $tmp_file
    ;;
    sort)
    [ -f $GTD_FILE.bak ] && rm $GTD_FILE.bak
    mv $GTD_FILE $GTD_FILE.bak
    sort $GTD_FILE.bak > $GTD_FILE
    ;;
    contexts)
    cut -d":" -f1 $GTD_FILE | sed "s/$ENTRY_PREFIX//" | sort | uniq
    ;;
*)
    usage
    ;;
esac

exit 0