#!/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.
##################################################################################
##
## This is the theme engine starter for bashish
##
########################################################

## set a sync file
: >"$HOME/.bashish/tmp/${TTY##*/}/sync"

## load all modules specified in $THEMECAP
typeset LOAD
for LOAD in $THEMECAP
do
	_bashish_module engine/$LOAD
done

## load the theme
_bashish_module sys/loadtheme

## load application themes
_bashish_module sys/btapp

## this is a helper function commonly used to locate a file in
## the three btdirs
function _bashish_whichbtdir
{   
	typeset WHICHBTDIR
	for WHICHBTDIR in "$HOME/.bashish/bt/override" "$BTDIR" "$HOME/.bashish/bt/fallback"
	do
		test -f "$WHICHBTDIR/$1" && { printf "$WHICHBTDIR" ; return 0; }
	done
}

## _bashish_brefresh is the main themeing function called when theme
## settings is to be applied to a terminal
function _bashish_brefresh
{
	if test "${BASHISH_ARGV[0]}"; then
		## set BTDIR to APPDIR
		typeset BTDIR=$HOME/.bashish/bt/app/${BASHISH_ARGV[0]##*/}
	else
		## $BTDIR points to the theme dir
		typeset BTDIR=$HOME/.bashish/bt/prompt/
	fi
	## load default theme options
	_bashish_theme_defaults

	## load prompt theme
	_bashish_theme_prompt
	
	test "x${BASHISH_ARGV[0]}" != x && {
		## load application theme
		_bashish_btapp_${BASHISH_ARGV[0]##*/}
	}
		
	## override theme options
	_bashish_theme_overrides

	##################################################################
	
	## do not make FUNCTION global
	typeset FUNCTION
	
	## load theme functions
	for FUNCTION in $THEMECAP; do
		#test "$BASHISH_DEBUG"&&echo "debug:$FUNCTION"
		$FUNCTION
	done
}

