#!/bin/bash
##################################################################################
## Bashish, a console theme engine
## Copyright (C) 2005 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.
##################################################################################
##
## bashishtheme provides mechanisms for changing themes.
## it is loaded by the bashishtheme command
##
###############################################################################

TTY=$(tty)

## print out fault message, help and return with a return status
function errorhandler
{
	typeset SELF=${0##*/}
	typeset ERROR=$1
	case "$1" in
	99) printf "not implemented";;
	10) exit 10;;
	12) printf "$SELF: interactive mode unavaliable since dialog could not be found";;
	20) printf "$SELF: file was saved to $BTFILE";;
	21) printf "$SELF: aborting on users request";;
	22) printf "$SELF: could not parse theme";;
	23) printf "$SELF: this program must be executed from a valid themedirectory";;
	24) printf "$SELF: theme was not saved";;
	25) printf "$SELF: \$BASHISH_THEME not set";;
	24) printf "$SELF: an error occured";;
	26) printf "$SELF: theme was sucessfully saved";ERROR=10;;
	27) printf "$SELF: an error occured while saving the theme";;
	30) printf "$SELF: theme not found";;
	80) printf "$SELF: you need to install the \"dialog\" utility in order to use the interactive mode";;
	0) exit 0;;
	1) exit 1;;
	esac
	printf "\n"
	#helphandler
	exit $ERROR
}

## help
function helphandler
{
	BASHISHTHEME=help
	printf "
USAGE:

${0##*/} [save]

${0##*/} is the theme changer for Bashish.
If ${0##*/} is started without options an interactive dialog will be presented to the user.
With the interactive dialog, the user can choose a theme for the prompt or for individual applications.

If ${0##*/} is supplied with the \"save\" option, the theme in the current working directory will be saved.
"

	exit 1
}



function _bashish_module
{
	. $BASHISHDIR/main/bashishtheme/$1
}

## prompt the user if the $HOME/.bashish directory is missing
test -d $HOME/.bashish || { bashish;errorhandler 10;}

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

## by default bashishtheme is used in batch mode
BASHISHTHEME=batch

## create a list over avaliable themes
_bashish_module sys/list

## provide the find function
_bashish_module sys/find


case "$BASHISH_ARGC" in
0)
	## denote that bashishtheme is in interactive mode
	BASHISHTHEME=interactive

	case ${BASHISH_ARGV[0]} in
	-*) helphandler;;
	esac
	## provide the unpack
	_bashish_module sys/unpack

	## provide bashish save
	_bashish_module sys/themedb

	## provide bashish appdb
	_bashish_module sys/appdb

	## interactive mode
	_bashish_module sys/fe/dialog
	_bashish_fe
	_bashish_module sys/update
	_bashish_update
	errorhandler 0
	;;
*)
	case "${BASHISH_ARGV[0]}" in
	save)
		_bashish_module sys/save
		_bashish_save
		;;
	import)
		## arg2 so konsole one day can be supported too
		_bashish_module sys/import/gnome-terminal
		_bashish_import
	;;
	-*)	helphandler;;
	*)
		## provide the unpack
		_bashish_module sys/unpack

		## provide bashish save
		_bashish_module sys/themedb
		## provide bashish appdb
		_bashish_module sys/appdb

		case ${BASHISH_ARGV[1]} in
		-*|"") 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_update
		_bashish_module sys/setopts
		_bashish_setopts
		errorhandler 0
	esac
;;
esac

errorhandler 99
