#!/bin/sh
#
#  wisdom.sh, a bash script by duus made on Fri Jul 20 15:51:46 EDT 2007 
#  lives in: ~/Documents/Software/unixscripts//wisdom/wisdom.sh 
#  run binedit wisdom to edit.  see binedit help for more information
#
_SCRIPT_NAME=wisdom             
_SCRIPT_LOCATION=~/bin/             
#             
if [ "$1" = "version" ]                
	then                                 
	echo " wisdom by duus"          
	echo " ~/Documents/Software/unixscripts//wisdom/wisdom.sh "
	else                                 
# --- normal script here ---           
_mode=display
_wisdir=~/Documents/PPL/duus/wisdom/
_wisproj=~/Documents/PPL/duus/PJduus.tmproj
_filename=""
#
#
#h wisdom...
arg=$1 # grab the first word
shift #move stuff over.

	if [ "$arg" = "d" -o "$arg" = "display" ]
		then
#h <(d)isplay> puts a word of wisdom to stdout (default)
		_mode=display
	fi

	if [ "$arg" = "a" -o "$arg" = "add" ]
		then
#h <(a)dd> FILE adds file to the wisdom database
		_mode=add
	fi

	if [ "$arg" = "n" -o "$arg" = "new" ]
		then
#h <(n)ew> (FILENAME) makes a new word of wisdom and opens it in textmate
		_mode=new
	fi
	
	if [ "$arg" = "h" -o "$arg" = "help" ]
		then
#h <(h)elp> shows this file
		grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
		exit 1;
	fi

###########################################################

function fechanger {
	# file exists name changer\
	__wisdir=$1
	__filename=$2
	while [ -e "${__wisdir}/${__filename}" ]
		do
		echo "file ${__filename} exists in database."
		echo "options: (o)verwrite, (e)nter newname, (a)utomatic newname, (d)isplay original file, (q)uit"
		echo -n " >> "
		read _action
		case "$_action" in
			'o')
				echo "overwrite file ${__filename}"
				rm "${__wisdir}/${__filename}"
				;;
			'e')
				_stop=1
				echo -n " enter new name: "
				read __filename
				;;
			'a')
				_stop=1
				__filename=${__filename}`date +_%h_%d_%y`
				echo "new name is ${__filename}"
				;;
			'd')
				cat ${__wisdir}/${__filename}
				;;
			'q')
				_stop=1
				echo "exiting..."
				exit 1;
		esac
	done
	echo -n ${_filename} > ${__wisdir}/.filename
}

function lineadder {
	_outfile=$1 # outputfile is $1...
	echo "" >> ${_outfile}
	echo "" >> ${_outfile}
	echo " (added by duus on `date +"%h %d, %Y"`)" >> ${_outfile}
	#cleanup extra returns:
	perl -i -pe 's/\n\n\n/\n\n/g' ${_outfile}
	flip -u ${_outfile}
}

##############################################################################



case "${_mode}" in
     'display')
		# display a word of wisdom to stdoutput...
		cd $_wisdir
		_list=( `ls` )
		#echo ${_list[*]}
		_randnum=`jot -r 1 0 ${#_list[*]}`
		_filename=${_list[$_randnum]}
		cat "${_filename}"
		;;
	 'add')
		echo "wisdom: add files $*"
		for _filename in $*
			do
			echo "adding ${_filename}"
			if [ ! -e "${_filename}" ]
				then 
				echo "${_filename} does not exist in directory `pwd`"
				else
				fechanger ${_wisdir} ${_filename}
				_outfile=`cat ${_wisdir}/.filename`
				#echo $_outfile
				cp ${_filename} ${_wisdir}/${_outfile}
				lineadder "${_wisdir}/${_outfile}"
			fi
		done
		;;
	'new')
		if [ "$1" != "" ]
			then 
			_filename=$1 #get the nextword as the filename
			else
			_filename=wisdom`date +_%h_%d_%y`.txt
		fi
		fechanger ${_wisdir} ${_filename}
		_outfile=`cat ${_wisdir}/.filename`
		lineadder "${_wisdir}/${_filename}"
		open -a TextMate ${_wisproj}
		open ${_wisdir}/${_filename}
		;;
esac

# 
#



 
# -------------------------- 
# 
fi # end 'version' 
# 
# end wisdom, ~/Documents/Software/unixscripts//wisdom/wisdom.sh
