#!/bin/bash
# Title......: qref
# Description: Quick reference system
# Contact....: http://www.bdragon.net
# Author.....: Mitchell Johnston - uid 0
# Date.......: Sat Apr 10 2010
#----------------------------------
# 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} 2>/dev/null # use $QREFDIR if set otherwise use ~/etc/qrefdir
LOG=~/etc/log.txt # my log file
NAME=$(basename $0)
TMOUT=300 # Time before screen saver kicks in
SS='cmatrix -b -s -C cyan' # App to use as screen saver
PS4='$SECONDS $LINENO: ' # debug prompt
DEBUG=0 # turn on (1) debug mode
# aliases
#----------------------------------
alias vi='vim -S ~/bin/vim.cmd'
# functions
#----------------------------------
pause(){ # simple pause rutine
[ $DEBUG == 1 ] && set -x
echo -n "Please hit ENTER: "
read x
}
bl(){ # write a blank line
[ $DEBUG == 1 ] && set -x
echo " "
}
end(){ ## ending function
[ $DEBUG == 1 ] && set -x
# 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
[ $DEBUG == 1 ] && set -x
printf "\033]0;$*\007" # Sets the window title
}
log(){ ## log entry tool
[ $DEBUG == 1 ] && set -x
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
#----------------------------------
[ $DEBUG == 1 ] && set -x
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 -R -C 1 -in --color $1 * # or search for it
ES=$?
if [ $ES -eq 1 ]
then
echo "Didn't find shit."
exit 1
fi
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
if [ -d $QREFDIR/$FILE ]
then
printf '%-15s' $FILE/|tr ' ' '\056'
echo ":${YELLOWF}DIR - $(ls -l $QREFDIR/$FILE|wc -l) files${NORM} "
else
printf '%-15s' $FILE|tr ' ' '\056'
echo ":${LINE}$(head -1 $QREFDIR/$FILE)${ULINE}"
fi
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}C${UBOLD}hange DIR ${BOLD}Q${UBOLD}uit${NORM}"
echo -n "Enter menu choice: "
CHOICE="";read CHOICE
case $CHOICE in
!) clear
echo -n "cmd: "
read EXEC
eval $EXEC
pause;;
rs) exec $0;;
[cC]) clear
echo "${YELLOWF}Leave blank for default${NORM}"
echo "QREFDIR: $QREFDIR"
echo -n "NEW.....:"
read NEWDIR
[ -z $NEWDIR ] && QREFDIR=~/etc/qrefdir
if [ ! -d ${QREFDIR}/$NEWDIR ]
then
export QREFDIR=~/etc/qrefdir
else
export QREFDIR=${QREFDIR}/$NEWDIR
fi
exec $0;;
[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 -R -C 1 -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
if [ -z $CHOICE ]
then
$SS
else
echo ^G
echo "Bad choice"
sleep 2;break
fi;;
esac
fi
fi
done
done
exit 0