#!/bin/bash

# Title......: qref
# Description: Quick reference system
# Author.....: Mitchell Johnston - uid 0
# Contact....: http://www.bdragon.net
# Date.......: Mon Sep 07 2009
#----------------------------------

#  This tool manages your notes in text format located in a directory
# defined by $QREFDIR. If you pass it a command line option it will
# check to see if it is a file and if so it will display it. Else it will
# search for it in the dir. If no option is given then it will run
# in for screen mode.


# variables
#----------------------------------
BOLD=$(tput smso)     # turn bold on
UBOLD=$(tput rmso)    # turn bold off
LINE=$(tput smul)     # turn underline on
ULINE=$(tput rmul)    # turn underline off      
YELLOW=$(tput setaf 3)
GREEN=$(tput setaf 2)
NORM=$(tput sgr0)
OS=$(uname -s)        # os type: SunOS Linux
QREFDIR=~/etc/qrefdir
LOG=~/etc/log.txt     # my log file
NAME=$(basename $0) 

# aliases
#----------------------------------
alias vi='vim -S ~/bin/vim.cmd'
# functions
#----------------------------------

pause(){ # simple pause rutine
echo -n "Please hit ENTER: "
read x
}
 
bl(){ # write a blank line
echo " "
}

end(){ ## ending function
# trap "end" 2 # runs a function

## Display run time
if [ $SECONDS -gt 60 ]
then
        ((MINUTES = SECONDS / 60))
        echo "$0 ended run after $MINUTES minute(s)"
else
        echo "$0 ended run after $SECONDS seconds"
fi

## Remove temp files
for TMP in $(ls /tmp/*.$$ 2>/dev/null)
do
        \rm $TMP 
done

## end application
exit

}  

xtitle(){ # set window title
    printf "\033]0;$*\007"        # Sets the window title
}

log(){ ## log entry tool
        if [ $# == 0 ] # display todays entry's
        then
                grep $(date '+%D') $LOG
        else
                # make an entry
                echo "$(date '+%D %T') $HOSTNAME [$$] $*">>$LOG
                echo "${GREENF}$(date '+%D %T')${NORM} $HOSTNAME ${YELLOWF}[$$]${NORM} $*"
        fi
}
 
# main
#----------------------------------
xtitle Qref
if [ $# -eq 1 ]
then
    if [ -f $QREFDIR/$1 ]  # check to see if option is a file
    then
        xtitle $1
        view -S ~/bin/vim.cmd $QREFDIR/$1 # display it if it is
        xtitle $PWD
        end
    else
        cd $QREFDIR
        grep -in --color $1 *             # or search for it
        cd $OLDPWD
        end
    fi
fi

## get the name of the last file
LAST=$(ls $QREFDIR| tail -1)
while :
do
    CNT=0
    clear
    for FILE in $(ls $QREFDIR)
    do
        if [ $CNT -le 20 ]
        then
            printf '%-15s' $FILE|tr ' ' '\056'
            echo ":${LINE}$(head -1 $QREFDIR/$FILE)${ULINE}"
            let "CNT = CNT + 1"
            if [ $CNT -eq 20 -o "$FILE" = "$LAST" ]
            then
                echo "${GREEN}=======================================${NORM}"
                echo "${YELLOW}${BOLD}N${UBOLD}ext ${BOLD}E${UBOLD}dit ${BOLD}V${UBOLD}iew ${BOLD}K${UBOLD}ey word ${BOLD}D${UBOLD}elete ${BOLD}R${UBOLD}ename ${BOLD}Q${UBOLD}uit${NORM}"
                echo -n "Enter menu choice: "
                read CHOICE
                case $CHOICE in
                    [nN]) clear
                          CNT=0;;
                    [eE]) echo -n "Document: "
                          read DOC
                          xtitle "qref: $DOC"
                          log "qref: edit $QREFDIR/$DOC"
                          vi $QREFDIR/$DOC;break;;
                    [vV]) echo -n "Document: "
                          read DOC  
                          xtitle "qref: $DOC"
                          view -S ~/bin/vim.cmd $QREFDIR/$DOC;break;;
                    [dD]) clear
                          echo -n "Remove: "
                          read REMOVE
                          \rm $QREFDIR/$REMOVE;;
                    [kK]) clear
                          echo -n "Match: "
                          read MATCH
                          cd $QREFDIR
                          grep -in --color $MATCH *
                          cd $OLDPWD
                          pause;break;;        
                    [rR])  clear
                          echo -n "File....: "
                          read FILE1
                          echo -n "New name: "
                          read FILE2
                          mv $QREFDIR/$FILE1 $QREFDIR/$FILE2
                          ;;
                    [qQ]) printf "\033]0;$LOCATION\007"
                          clear
                          exit;;
                       *) clear
                          echo ^G
                          echo "Bad choice"
                          sleep 2;break;;
                esac
            fi
        fi
   done
done

# changes
#----------------------------------
# 10/16/08 added option to search via cli