#!/bin/bash
##################################################################################
## Bashish, a console theme engine
## Copyright (C) 2010 Thomas Eriksson
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##################################################################################
##
## bashish provides mechanisms for changing themes.
## it is loaded by the bashish command
##
###############################################################################

#TTY=$(tty)

test "x${TTY}" = x && TTY=$(tty 2>/dev/null)

## command name, bashish?
BASHISHNAME="${0##*/}"

## print out fault message, help and return with a return status
function errorhandler
{
	if test "x$2" != x
	then
		printf "${BASHISHNAME}: $2\n"
		exit $1
	else 
	local ERROR=$1
	case "$1" in
	99) printf "$BASHISHNAME: not implemented";;
	12) printf "$BASHISHNAME: interactive mode unavaliable since dialog could not be found";;
	20) printf "$BASHISHNAME: file was saved to $BTFILE";;
	21) printf "$BASHISHNAME: error saving, aborting on users request";;
	22) printf "$BASHISHNAME: error saving, could not parse theme-file";;
	220) printf "$BASHISHNAME: error saving, header in prompt.[zsh|ksh|bash|sh] or theme does not start with #"'!'"/bin/sh";;
	221) printf "$BASHISHNAME: error saving, could not parse prompt.sh-file";;
	222) printf "$BASHISHNAME: error saving, could not parse prompt.ksh-file";;
	223) printf "$BASHISHNAME: error saving, could not parse prompt.bash-file";;
	224) printf "$BASHISHNAME: error saving, could not parse prompt.zsh-file";;
	23) printf "$BASHISHNAME: error saving, this program must be executed from a valid themedirectory.
$BASHISHNAME: error saving, the prompt theme directory is located in \${HOME}/.bashish/prompt";;
	24) printf "$BASHISHNAME: error saving, theme was not saved";;
	25) printf "$BASHISHNAME: error saving, \$BASHISH_THEME not set";;
	251) printf "$BASHISHNAME: error saving, _bashish_theme_\$BASHISH_THEME() function not existing or incorrectly named";;
	261) printf "$BASHISHNAME: error saving, _bashish_prompt() nonexistant or named incorrectly in prompt.sh";;
	262) printf "$BASHISHNAME: error saving, _bashish_prompt() nonexistant or named incorrectly in prompt.ksh";;
	263) printf "$BASHISHNAME: error saving, _bashish_prompt() nonexistant or named incorrectly in prompt.bash";;
	24) printf "$BASHISHNAME: an error occured";;
	27) printf "$BASHISHNAME: an error occured while saving the theme";;
	30) printf "$BASHISHNAME: theme not found";;
	80) 	helphandler
		printf "
$BASHISHNAME: you need to install the \"dialog\" utility in order to use the interactive mode\n\n";;
	0) exit 0;;
	1) exit 1;;
	esac
	printf "\n"
	#helphandler
	exit $ERROR
	fi
}

## help
function helphandler
{
	BASHISHNAME_MODE=help
	printf "
Bashish  Copyright (c) 2010 Thomas Eriksson
Bashish comes with absolutely no warranty
This is free software, and you are welcome
to redistribute it under certain conditions
See the GNU General Public License v2 for details

Advanced Usage:

--uninstall        uninstall bashish
--enable-terminal  enable terminal theming
--disable-terminal disable terminal themeing (but keep prompt changing enabled)

Theme handling:

To change to a theme by the name THEMENAME, enter:
$BASHISHNAME THEMENAME [<color1>] [<color2>] [<...>] [<colorn>]

Colors are optional (and not all themes support them) and may be any of the following words:
cyan magenta blue red yellow white black green

To save a theme in the current directory, enter:
$BASHISHNAME save

To list available themes, enter:
$BASHISHNAME list

"
errorhandler 0
}

function _bashish_module
{
	. "$BASHISHDIR"/main/bashish/$1
}

## returncodes
## 1 if prompt theme was not changed
## >1 if an error occured
## 0 if prompt theme was successfully changed
RETURN=1

## by default bashish is used in batch mode
BASHISHNAME_MODE=batch

## provide the list function
_bashish_module sys/list

## provide the find function
#_bashish_module sys/find
function _bashish_find
{
	local n=0
	while test "x${THEME_LIST[$n]}" != x
	do
		if test 	x"${THEME_LIST[$n]##*/}" = "x${1}" \
	 			-a -f "${THEME_LIST[$n]}"/theme
		then
			THEME=${1};\
			THEME=${THEME##*/};\
			THEMEFILE=${THEME_LIST[$n]};\
			return 0; \
		fi
		let n++
	done
	return 1
}

case "$BASHISH_ARGC" in
0)
	## denote that bashish is in interactive mode
	BASHISHNAME_MODE=interactive

	## provide the unpack
	_bashish_module sys/unpack

	## provide bashish appdb
	_bashish_module sys/appdb

	## interactive mode
	_bashish_module sys/fe/dialog
	_bashish_module sys/update
	errorhandler 0
	;;
*)
	case "${BASHISH_ARGV[0]}" in
	"") 
if test ! -d ~/.bashish
then
printf "\
This will enable Bashish prompt and terminal themeing

the following files will be modified:
~/.profile
~/.bashrc
~/.bash_profile (if already exists)
~/.zprofile (if already exists)
~/.zshrc


press ENTER to enable Bashish theming
or hit CTRL+C to quit
"
read -n1
	_bashish_module sys/createdirs
printf "\
Bashish is now installed.

Restart your terminal or logoff and logon again to start theming your shell.

"
	errorhandler 1
else
helphandler
fi
errorhandler 1
	;;
	--bashishdir|-b) printf "$BASHISHDIR\n";errorhandler 1;;
	--enable-terminal)
## gnome stuff
IFS="[]"
for LIST in $(gconftool-2 --get /apps/gnome-terminal/global/profile_list 2>/dev/null);do :;done
case "$LIST" in
""|*,Bashish|Bashish,*|*,Bashish,*)
	:
;;
*)
	## load schema
	gconftool-2 --load "$BASHISHDIR/main/terminal/templates/gnome-terminal/schema"
	gconftool-2 --type string --set /apps/gnome-terminal/global/default_profile "Bashish"
	gconftool-2 --type list --list-type=string --set /apps/gnome-terminal/global/profile_list "[${LIST},Bashish]"
esac
unset IFS

	test -f "${HOME}"/.bashish/terminal-theming-disabled && rm "${HOME}"/.bashish/terminal-theming-disabled ; gconftool-2 --type string --set /apps/gnome-terminal/global/default_profile "Bashish" 2>/dev/null; errorhandler 1 "terminal theming enabled";;
	--disable-terminal) 
if type -p gconftool-2 1>/dev/null 2>/dev/null
then
gconftool-2 --recursive-unset /apps/gnome-terminal/profiles/Bashish
gconftool-2 --unset /apps/gnome-terminal/profiles/Bashish
gconftool-2 --type string --set /apps/gnome-terminal/global/default_profile "Default"
NEW_LIST="$(gconftool-2 --get /apps/gnome-terminal/global/profile_list|sed -e 's/Bashish//g' -e 's/,,/,/g' -e 's/\[,/\[/g' -e 's/,\]/\]/g')"
gconftool-2 --type list --list-type=string --set /apps/gnome-terminal/global/profile_list "${NEW_LIST}"
fi
	touch "${HOME}"/.bashish/terminal-theming-disabled ; gconftool-2 --type string --set /apps/gnome-terminal/global/default_profile "Default" 2>/dev/null; errorhandler 1 "terminal theming disabled" ;;
	uninstall|--u*|-u*) _bashish_module sys/uninstall 
printf "${BASHISHNAME}: bashish is now uninstalled
"
errorhandler 1
;;
	--bashishdir) echo "$BASHISHDIR";;
	list|show|-l*|--l*) for THEME_ITEM in ${THEME_LIST[*]##*/}; do echo "$THEME_ITEM";done | sort|uniq; errorhandler 0;;
	exec|-e*|--e*)
	echo "exec is not available yet"
	exit 42
	#$BASHISHDIR/lib/_bashish_appdb

	## single application mode
	## eg. when launching a bashish themed application from a GUI
	## first launch bashish
	if _bashish_module sys/termloader
	then
	
		termhandler
		## magic globbing code
		TEMP=$(declare -p BASHISH_ARGV)
		PATH=$BASHISH_OLDPATH
		printf "BASHISH_BASHISH_ARGV=${TEMP#*=};BTAPP='${0##*/}'" 1>"$HOME"/.bashish/tmp/"${TTY##*/}"/subproc
		#read input <$HOME/.bashish/tmp/${TTY##*/}/singitback
		n=1
		eval $(printf \"${0##*/}\"; while test "$n" -lt ${BASHISH_ARGC};do
			printf " "\""${BASHISH_ARGV[$n]}"\";
			let n++;
		done)
	else
		errorhandler 6
	fi
	;;
	save|-s|--s*)
		_bashish_module sys/save
		printf "\
$BASHISHNAME: theme was sucessfully saved
$BASHISHNAME: press \033[1mreturn\033[0m to load the saved theme
"
read -n1 INPUT

		_bashish_module sys/update
		_bashish_module sys/termcleanup
errorhandler 0
		;;
	-*)	helphandler;;
	*)
		## provide the unpack fn
		_bashish_module sys/unpack

		## provide bashish appdb
		_bashish_module sys/appdb
		case ${BASHISH_ARGV[1]} in
		-*|""|bright*|dim*|underscore*|blink*|reverse*|hidden*|black|red|green|yellow|blue|magenta|cyan|white)
			BTAPP="";_bashish_unpack "${BASHISH_ARGV[0]}";;
		*)
			BTAPP="${BASHISH_ARGV[0]}"; _bashish_unpack "${BASHISH_ARGV[1]}"
		
			## application theme changer
			BTAPP=${BASHISH_ARGV[0]}
		esac

		_bashish_module sys/update
		_bashish_module sys/termcleanup
		errorhandler 0
	esac
;;
esac
## we shouldn't have gotten here
echo ${BASHISH_ARGV[0]}
errorhandler 99
