 #!/bin/bash
#
#v binedit v 0.3 by duus
#h binedit v 0.3 by duus
#h
#h usage: binedit <options> scriptname foldername
#h    for example binedit edit binedit binedit
#h    if 'folder' is missing, binedit assumes the folder is scriptname
#h
#  Created by duus on 2007-05-15.
# 
## defaults
_maindir=`unixscriptsfolder`
# _maindir=~/Documents/Software/unixscripts/
# if [ "`whatmachine`" == "bingoffice" -o "`whatmachine`" == "matt48" ]
# 	then
# 	_maindir=`echo ${_maindir} | perl -i -pe 's/Documents/sDocs/g'`
# fi
#
## leave these defaults
#
_thisdir=binedit/
_thisscript=binedit
_pj=PJunixscripts.tmproj
_mode=edit
_mkdir=0
_SCRIPT_NAME=binedit
_SCRIPT_LOCATION=~/bin/
#
#
#h _options_
if [ "$1" = "edit" -o "$1" = "e" ]
	then
#h  (e)dit or nothing to edit in textmate (verb)
	_mode=edit
	shift
#
elif [ "$1" = "bin" -o "$1" = "b" -o "$1" = "binify" ]
	then
#h  (b)in binifies the edited script (verb)
	_mode=bin
	shift
#
elif [ "$1" = "f" -o "$1" = "folders" ]
	then
#h  (f)olders folders the script from ~/bin/ (verb)
	_mode=folders
	shift
#
elif [ "$1" = "c" -o "$1" = "catalog" ]
	then
#h  (c)atalog lists all folders in unixscripts (verb)
	_mode=catalog
	shift
#
elif [ "$1" = "r" -o "$1" = "run" ]
	then
#h  (r)un the script in folders (verb)
	_mode=run
	shift
#
elif [ "$1" = "archive" -o "$1" = "a" ]
	then
#h  (a)rchive the script in folders (verb)
	_mode=archive
	shift
#
elif [ "$1" = "info" -o "$1" = "i" ]
	then
#h  (i)nfo get fileinfo about the script, both locations. (verb)
	_mode=info
	shift
#
elif [ "$1" = "dir" -o "$1" = "d" ]
	then
#h  (d)ir copy the script dir into the buffer (getpwd)  (verb)
	_mode=dir
	shift
#
elif [ "$1" = "path" -o "$1" = "p"  ]
	then
#h  (p)ath copies the script file path into the buffer   (verb)
	_mode=path
	shift
#
elif [  "$1" = "remove" -o  "$1" = "rm"  ]
	then
#h  (rm) remove a script    (verb) [ not correctly implemented.  run binedit remove for the complaint...]
	echo "WARNING!  REMOVE IS NOT CORRECTLY IMPLEMENTED!"
	echo "  IT REMOVES THE WHOLE FOLDER, EVEN WHEN THERE ARE OTHER SCRIPTS IN THAT FOLDER!"
	echo "  therefore I quit until that can be fixed..."
	exit 1;
	_mode=remove
	shift
#
elif [ "$1" = "new" -o "$1" = "n"  ]
	then
#h  (n)ew makes a new script.      (verb)
	_mode=edit
	shift
#
elif [  "$1" = "post"  ]
	then
#h  (post) "phrase" makes a script webpage makes a new script, with phrase "phrase"      (verb)
	_thisphrase="$2"
	_mode=post
	shift
	shift
#h
#h
#h ---
#
elif [ "$1" = "force" ]
	then
#h  (force) forces creation of a folder if none exists (must be after verb) 
	_mkdir=1
	shift
#
elif [ "$1" = "h" -o "$1" = "help" -o "$1" = "v" -o "$1" = "version" ]
	then
#h    <(h)elp>,<(v)ersion> displays this help file 
	# this should refer to binedit here:
	if [ "$1" = "v" -o "$1" = "version" ]
		then 
		_search=v
		else
		_search=h
	fi
	grep "^#${_search}" ${_maindir}/binedit/binedit.sh | more
	_mode="none"
	# now end.
	exit 1
fi
#
#
#
#
if [ "$1" != "" ]
	then
	_thisscript="$1"
	if [ "$2" != "" ]
		then
		_thisdir="$2"
		else
		_thisdir="$1"
	fi
fi
#
#
#	
# check if relevant folder exists
#
if [ ! -d ${_maindir}/${_thisdir} ]
	then
	if [ "${_mkdir}" = "0" ]
		then
		echo "folder ${_maindir}/${_thisdir} not found."
		echo -n "make folder  ${_maindir}/${_thisdir}? [Y]/n "
		read _yesno
		if [ "${_yesno}" = "n" -o "${_yesno}" = "no" ]
			then
			echo "okay, won't make folder  ${_maindir}/${_thisdir}, exiting"
			echo " if you reached this in error, perhaps you mispelled the script name?"
			echo " "
			_mode="none"  
			exit 1
			else
			_mkdir=1
		fi
	fi
	if [ "${_mkdir}" = "1" ]
		then
		mkdir -v ${_maindir}/${_thisdir}
	fi
fi
#
# check if relevant script exists. (kind of implied by above...)
#
if  ( ! ( test -e  "${_maindir}/${_thisdir}/${_thisscript}.sh" ) )
	then
	echo "${_thisdir}/${_thisscript}.sh does not exist."
	if [ "${_mode}" = "edit" ]
		then
		if [ -e ~/bin/${_thisscript} ]
			then
			echo "so binedit will copy up ~/bin/${_thisscript} by running \'binedit folders ${_thisscript} ${_thisdir}\'"
			binedit folders ${_thisscript} ${_thisdir}
			else
			echo "So binedit will make a new script ${_thisdir}/${_thisscript}.sh			"
			_nicedate=`nicedate`
			_mood=`getmood`
			#
			echo "type a short phrase to describe the script ${_thisscript}:"
			echo -n ">>> "
			read _thisphrase
			echo -n ${_thisphrase} > ${_maindir}/${_thisdir}/${_thisscript}phrase.txt
			cp ${_maindir}/binedit/binedit_newscript.sh ${_maindir}/${_thisdir}/${_thisscript}.tmp
			cat ${_maindir}/${_thisdir}/${_thisscript}.tmp | \
				perl -i -pe "s#THISSCRIPT#${_thisscript}#g; s#MAINDIR#${_maindir}#g; s#THISDIR#${_thisdir}#g ; s#DATE#${_nicedate}#g ; s#SCRIPTLOCATION#~/bin#g ; s#MOOD#${_mood}#g  ; s#PHRASE#${_thisphrase}#g" \
				> ${_maindir}/${_thisdir}/${_thisscript}.sh
			# moved to binedit_newscript.sh in this folder.
					# echo "#!/bin/bash	" > ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "#" >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "#  ${_thisscript}.sh, a bash script by duus made on `date` " >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "#  lives in: ${_thisdir}/${_thisscript}.sh " >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "#  run binedit ${_thisscript} to edit.  see binedit help for more information" >>  ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "#" >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "_SCRIPT_NAME=${_thisscript}             "  >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "_SCRIPT_LOCATION=${_SCRIPT_LOCATION}             "  >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "#             "  >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "if [ \"\$1\" = \"version\" ]                ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "	then                                 ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "	echo \" ${_thisscript} by duus\"          ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "	echo \" ${_maindir}/${_thisdir}/${_thisscript}.sh \"" >> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "	else                                 ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "# --- normal script here ---           ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo " ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo " ">> ${_maindir}/${_thisdir}/${_thisscript}.sh
					# echo "# -------------------------- ">> ${_maindir}/${_thisdir}/${_thisscript}.sh 
					# echo "# ">> ${_maindir}/${_thisdir}/${_thisscript}.sh 
					# echo "fi # end 'version' ">> ${_maindir}/${_thisdir}/${_thisscript}.sh 
					# echo "# ">> ${_maindir}/${_thisdir}/${_thisscript}.sh 
					# echo "# end ${_thisscript}, ${_maindir}/${_thisdir}/${_thisscript}.sh">> ${_maindir}/${_thisdir}/${_thisscript}.sh 
			chmod +x ${_maindir}/${_thisdir}/${_thisscript}.sh
			head ${_maindir}/${_thisdir}/${_thisscript}.sh
		fi
	else
		if [  "${_mode}" != "none"  -a  "${_mode}" != "folders" -a  "${_mode}" != "remove"  ]
			then
			echo "quitting... try binedit help for more information.  (failed ${_mode})"
			exit 1;
		fi
	fi
	## if it does exist, and the mode is edit, then archive the old version of this script...
elif [ "${_mode}" = "edit" ]
	then
	echo "archive ${_thisscript} before editing it."
	binedit archive ${_thisscript} ${_thisdir}
fi

#echo "mode = $_mode"
#
################################  case statement (switch)
case "${_mode}" in
     'edit')
		 echo "edit ${_thisdir}/${_thisscript}.sh in textmate"
         open -a TextMate ${_maindir}/${_pj}
		 cd ${_maindir}/${_thisdir}/
		 runlater s 2 mate -a -d -r -l 1 ./${_thisscript}.sh
         ;;
     'bin')
		 echo "binify script ${_thisdir}/${_thisscript}.sh -> ~/bin/${_thisscript}"
		 cd ~/bin/
		 if [ ${_maindir}/${_thisdir}/${_thisscript}.sh -ot ~/bin/${_thisscript} ]
			then
			echo "since bin version was edited more recently, it is archived"
			archthis ${_thisscript}
			else
			echo "NOTE: bin version not archived"
		 fi
         cp ${_maindir}/${_thisdir}/${_thisscript}.sh ./${_thisscript}
         ;;
	 'dir')
		 echo -n "${_maindir}/${_thisdir}/" | pbcopy
		 ;;
	 'file')
		 echo -n "${_maindir}/${_thisdir}/${_thisscript}.sh" | pbcopy
		 ;;
     'archive')
	     echo "archive script ${_thisscript}.sh"
		 cd ${_maindir}/${_thisdir}/
		 archthis ${_thisscript}.sh
         ;;
	 'folders')
	     echo "take bin to folders script ~/bin/${_thisscript} -> ${_thisdir}/${_thisscript}.sh"
	     cd ${_maindir}/${_thisdir}/
	     archthis ${_thisscript}.sh
	     cp ~/bin/${_thisscript} ./${_thisscript}.sh 
	     ;;
	 'info')
	     echo "fileinfo on script ${_thisscript}"
		 echo " > in ${_maindir}/${_thisdir}/"
	     cd ${_maindir}/${_thisdir}/
	     ls -Fl ${_thisscript}.sh
	     echo " > in ~/bin/"
	     cd  ~/bin/
	     ls -Fl ${_thisscript}
	     ;;
     'run')
		echo "run script ${_thisdir}/${_thisscript}.sh"
		${_maindir}/${_thisdir}/${_thisscript}.sh $2 $3 $4 $5 $6 $7 $8
		;;
	 'catalog')
		echo "catalog of scripts (in ${_maindir})"
		cd ${_maindir}
		_foldercount=1
		for _folder in `ls -p`
			do
			_slash=${_folder%%/}
			#echo "slash: " $_slash
			if [ ${#_slash} -gt 0 ]
				then
				#it's a folder
				_name=${_folder/\//}
				thisls=`ls ${_name}/*.sh 2> /dev/null`
				if [ "${thisls}" != "" ]
					then
					echo "${_foldercount}:   <${_name}>   ---------"
					ls ${_name}/*.sh 2> /dev/null
					_foldercount=`expr $_foldercount + 1`
				fi
			fi
		done
		;;
	 'remove')
		#echo "removing \'$_thisscript\' ..."
		cd ${_maindir}
		if [ -e ${_thisdir} -o -e ~/bin/${_thisscript} ]
			then
			echo "move ${_thisdir} into ${_maindir}/archive"
			echo "and rm ~/bin/${_thisscript}"
			echo -n "Confirm: [Y]/n : "
			read confirm
			if [ "$confirm" != "n" -a "$confirm" != "no" ]
				then
				rm -vf ~/bin/${_thisscript}
				mv -v  ${_thisdir} ${_maindir}/archive/
				else
				echo "removal of ${_thisscript} skipped."
			fi
		fi
		;;
	'post')
		binpost ${_thisscript} ${_thisdir} "${_thisphrase}"
		;;
esac
#
#
#
#