#!/bin/bash
#===============================================================================
#   note.sh     |   version 1.0     |   2-Clause BSD License    |   2014-02-08
#   James Hendrie                   |   hendrie dot james at gmail dot com
#===============================================================================

NOTEFILE="$HOME/.notefile"  ##  Note file location
PAGING=0                    ##  Whether or not to use 'less'.  0 == use 'cat'
PAGEPROGRAM=less            ##  Default paging program (less)
CATPROGRAM=cat              ##  Defaul concatenation program (cat)

##  Print help for the user
function print_help
{
    echo -e "Usage:\tnote [OPTION]"
    echo -e "\nThis program will let you keep, read or edit ONE note."
    echo "Your note file is located at $NOTEFILE.  To create a note for the"
    echo "first time, or to read your current note, just run the script without"
    echo "any arguments."

    echo -e "\nOptions:"
    echo -e "  -e\tEdit the note with editor in \$EDITOR environment variable"
    echo -e "  -p\tPaging mode (use 'less' instead of 'cat')"
    echo -e "  -t\tDisplay when your note was last modified (uses 'stat')"
    echo -e "  -h\tThis help text"
}


##  Print the version and author info
function print_version
{
    echo "note.sh, version 1.0"
    echo "James Hendrie - hendrie dot james at gmail dot com"
}


##  If there's no EDITOR environment variable set, check for vi or nano.
##  Failing those, the user's just going to have to set an EDITOR variable or
##  go home.
function find_editor
{
    if [ "$EDITOR" = "" ]; then
        which vi &> /dev/null
        if [ $? -eq 0 ]; then
            export EDITOR=vi
        else
            which nano &> /dev/null
            if [ $? -eq 0 ]; then
                export EDITOR=nano
            else
                echo "ERROR:  No EDTIOR variable and neither vi nor nano found"
                exit 1
            fi
        fi
    fi
}


##  Create a new note
function create_note
{
    touch "$NOTEFILE" &> /dev/null
    
    if [ $? -ne 0 ]; then
        echo "ERROR:  Could not create file $NOTEFILE"
        exit 1
    fi
}


##  Edit the existing note
function edit_note
{
    find_editor

    if [ ! -f "$NOTEFILE" ]; then
        create_note
    elif [ ! -w "$NOTEFILE" ]; then
        echo "ERROR:  Cannot write to $NOTEFILE"
        exit 1
    fi

    "$EDITOR" "$NOTEFILE"
}


##  Read the note (by default, with either 'cat' or 'less')
function read_note
{
    if [ -r "$NOTEFILE" ]; then
        if [ $PAGING -eq 1 ]; then
            "$PAGEPROGRAM" "$NOTEFILE"
        else
            echo "" && "$CATPROGRAM" "$NOTEFILE" && echo ""
        fi
    else
        echo "ERROR:  Cannot read $NOTEFILE"
        exit 1
    fi
}


##  Display when the note was last modified, if the user hasn't check in a while
function display_last_modified
{
    if [ ! -r "$NOTEFILE" ]; then
        echo "ERROR:  Cannot read file $NOTEFILE"
        exit 1
    fi

    modString=$(stat "$NOTEFILE" | grep -i modif)
    dateString=$(echo "$modString" | cut -f2 -d':' | sed -e 's/^[ \t]*//' |\
        cut -f1 -d' ')
    hourString=$(echo "$modString" | cut -f3 -d' ' | cut -f1 -d':')
    minString=$(echo "$modString" | cut -f3 -d':')
    secString=$(echo "$modString" | cut -f4 -d':' | cut -f1 -d'.')

    echo -e "$dateString  $hourString:$minString:$secString"
}


##  If there are too many args, slap down the user's hopes and dreams
if [ $# -gt 1 ]; then
    echo "ERROR:  Too many arguments"
    print_help
    exit 1
fi


##  If there's one arg, we check to make sure it's valid.  Otherwise, we tell
##  the user that he or she is a big fat dummy
if [ $# -eq 1 ]; then
    if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "h" ]; then
        print_help
        exit 0
    elif [ "$1" = "--version" ]; then
        print_version
        exit 0
    elif [ "$1" = "-e" ] || [ "$1" = "--edit" ] || [ "$1" = "e" ]; then
        edit_note
        exit 0
    elif [ "$1" = "-t" ] || [ "$1" = "t" ]; then
        display_last_modified
        exit 0
    elif [ "$1" = "-p" ] || [ "$1" = "p" ]; then
        let PAGING=1
    else
        echo "ERROR:  Invalid argument:  $1"
        exit 1
    fi
fi


##  No args:  Read the note, or if necessary, create one and edit it
if [ -r "$NOTEFILE" ]; then
    read_note
else
    create_note
    edit_note
fi
