#!/bin/bash

###########################################################################
#
#	Shell program to place TODO reminers at login.
#
#	Copyright 2001, USM Bish, <bish@nde.vsnl.net.in>.
#
#	This program is free software; you can redistribute it and/ or
#	modify it under the terms of the GNU General Public License as
#	published by the Free Software Foundation; either version 2 of
#	the License, or (at your option) any later version. 
#
#	This program is distributed in the hope that it will be useful
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE. See the
#	GNU General Public License for more details.
#
#	Usage:
#
#		ToDo [ -h | --help ] [-a] [-d] [-v]
#
#	Options:
#
#		-h, --help	Display this help message and exit.
#		-a              Add to list
#		-d              Delete from List
#		-v              View the list
#
#
#       Execution: 1. Interactive: ToDo -v 
#                  2. View TODO as log-in message. Append following line
#                     at the end of .bash_profile/ .bashrc  depending on
#                     the way boot (console or X) : "/path/to/ToDo -v"
# 
#	Revisions:
#
#	Feb/19/2001	File created	.... ver 0.1
#       Feb/21/2001	Working version .... ver 0.2
#       Feb/22/2001     X windows added .... ver 0.3 
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.3"

TEMP_FILE1=/tmp/${PROGNAME}.$$.1
TEMP_FILE2=/tmp/${PROGNAME}.$$.2
TODO_FILE=~/.ToDo

###########################################################################
#	Functions
###########################################################################

function init_xrm
{
# xrm resources for the Xmessage panel
fbrg="*$MYWIN*form*background: $FORMBG"
fmbg="*$MYWIN*form*message*background: $MSGBG"
fmfg="*$MYWIN*form*message*foreground: $MSGFG"
foss="*$MYWIN*form*okay*shapeStyle: Oval"
fobg="*$MYWIN*form*okay*background: red"
cbgd="*$MYWIN*Command*background: white"
cfgd="*$MYWIN*Command*foreground: yellow"
chlt="*$MYWIN*Command*highlightThickness: 0"
cbkg="*$MYWIN*Command*background: gray30"
}

function view_list_x 
{
   FILE_IN=$TEMP_FILE1
   MYCOLOR="Grey30"
   FONT="8x13"
   init_xrm

   MYWIN=ToDo

   FORMBG="Grey40"
   MSGBG=$MYCOLOR
   MSGFG="White"

   init_xrm
   xmessage -name $MYWIN -title "$PROGNAME [v-$VERSION]" -fn $FONT \
            -center -xrm "$fbrg" \
            -xrm "$fmbg" \
            -xrm "$fmfg" \
            -xrm "$foss" \
            -xrm "$fobg" \
            -xrm "$cbgd" \
            -xrm "$cfgd" \
            -xrm "$chlt" \
            -xrm "$cbkg" \
-buttons "Exit":101 \
-file $TEMP_FILE1
             
    case $? in
        
    101) rm -f $TEMP_FILE1 
         exit 0
         ;; 
      *) echo "Impossible !"
                         
    esac 
}

function make_list
{
    echo -en "TO DO list for [$USER] as on " > $TEMP_FILE1
    date >> $TEMP_FILE1
    echo >> $TEMP_FILE1
    if [ -s $TODO_FILE ]; then
        nl -ba -nln -w3 $TODO_FILE | tr -d '\t' >> $TEMP_FILE1
        echo >> $TEMP_FILE1
        echo "$PROGNAME [-a|-d] to add and delete entries" >> $TEMP_FILE1 
        echo >> $TEMP_FILE1 
    else    
        echo -en "No entries in $TODO_FILE\n\n" > $TEMP_FILE1
    fi
}
        
function delete_from_list
{
    echo -en "$PROGNAME [Ver : $VERSION]  ... DELETE routine\n\n"
    # Check if run for first time
    if ! [ -s $TODO_FILE ]; then
       echo "No entries in TODO list !"
       exit
    fi
    
    # Display avaiable entries
    cat -n $TODO_FILE > $TEMP_FILE1
    UPTO=$(wc $TODO_FILE | awk '{print $1}')
    cat -n $TODO_FILE
    echo "     0  Abort this DELETE routine !!!"
    echo -en "\nWhich number to delete [ 0 to $UPTO ] ? "
    read NUMBER
    
    # User abort trap
    if [ "$NUMBER" = "0" ]; then
       clean_up
       exit
    fi
    
    # Re-write file after deletion
    BEFORE=$((NUMBER - 1))
    AFTER=$((UPTO - NUMBER))
    head -$BEFORE $TODO_FILE > $TEMP_FILE2
    tail -$AFTER $TODO_FILE >> $TEMP_FILE2
    cat $TEMP_FILE2 > $TODO_FILE 
}

function add_to_list
{
    echo -en "$PROGNAME [Ver : $VERSION]  ... ADD to TODO list\n\n"

    # First time run
    if ! [ -e $TODO_FILE ]; then
       > $TODO_FILE
    fi
         
    # get the entry
    cat -n $TODO_FILE
    ALIGN="\033[24G"
    echo -en "\nNext entry (50 char) : .................................................."
    echo -en $ALIGN
    read ENTRY
    
    # User abort trap
    if [ "$ENTRY" = "" ]; then
       clean_up
       exit
    fi   
    
    # Write the entry
    echo -en
    DAYTE=`date +%d-%b-%y`
    TYME=`date +%H:%M` 
    echo "$DAYTE [$TYME] $ENTRY" >> $TODO_FILE
}

function clean_up
{
    rm -f ${TEMP_FILE1}
    rm -f ${TEMP_FILE2}
}

function usage
{
    echo "Usage: ${PROGNAME} [-h | --help] [-a] [-d] [-v]"
}


function helptext
{
    local tab=$(echo -en "\t\t")
		
    cat <<- -EOF-

        ${PROGNAME} ver. ${VERSION}	
        
        This is a program to place TODO reminers at login.
	
        $(usage)
	
        Options:
	
        -h, --help	Display this help message and exit.
        -a              Add to list
        -d              Delete from List
        -v              View the list
			
		
-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################

if [ "$1" = "--help" ]; then
    helptext
    exit
fi

# Process arguments

while getopts ":hadv" opt; do

    case $opt in

        a ) add_to_list 
            ;;
            
	d ) delete_from_list 
	    ;;
	    
        v ) make_list
            if [ $TERM = "linux" ] || [ $TERM = "screen-w" ]; then
               cat $TEMP_FILE1
            else
               view_list_x
            fi
            ;;
            
        h ) helptext
	    exit 
	    ;;
	    
	* ) usage
	    exit 1
	    
    esac
    
done
clean_up
exit 0

##################################################################
#   Everything below is ignored
##################################################################
