#!/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.
##################################################################################

function _bashish_save
{
	## run in a subshell so the themeloading won't pollute the enviroment
	local THEMEFILE FILE SHELLNAME THEMEDIR
	## do basic checks
	( test -f $PWD/theme || exit 23 ) || errorhandler 23
	## theme parsing fail?
	( . ./theme||exit 22 ) || errorhandler 22

	local HEADERFILE=""
	## header check
	for HEADERFILE in ./prompt.sh ./prompt.ksh ./prompt.zsh ./prompt.bash ./theme
	do
		test -f "${HEADERFILE}" && case $(head -n1 "$HEADERFILE") in
		"#!/bin/sh"):;;
		*) errorhandler 220 ;;
		esac
	done
	unset -f _bashish_prompt 2>/dev/null
	
	## theme parsing fail?
	if test -f ./prompt.sh
	then
		sh ./prompt.sh || errorhandler 221
		( . ./prompt.sh ; declare -f _bashish_prompt 2>/dev/null 1>/dev/null || exit 261)|| errorhandler 261

	fi

	if test -f ./prompt.ksh
	then
		bash ./prompt.ksh || errorhandler 222
		( . ./prompt.ksh ; declare -f _bashish_prompt 2>/dev/null 1>/dev/null || exit 262)|| errorhandler 262

	fi

	if test -f ./prompt.ksh -a "x$(type -p ksh)" != x
	then
		ksh ./prompt.ksh || errorhandler 222

	fi

	if test -f ./prompt.bash
	then
		bash ./prompt.bash || errorhandler 223
		( . ./prompt.bash ; declare -f _bashish_prompt 2>/dev/null 1>/dev/null || exit 263)|| errorhandler 263

	fi

	if test -f ./prompt.zsh -a "x$(type -p zsh)" != x
	then
		zsh ./prompt.zsh || errorhandler 222
	fi
	## syntax parsing end
	
	## warn if using unwanted constructs
	for FILE in ./prompt.sh ./prompt.bash ./prompt.ksh ./prompt.zsh
	do
		test -f "${FILE}" || continue
		grep '\\033' "$FILE" && echo "$BASHISHNAME: warning: use \${ESC} instead of \\033 since it may not work on some shells"
		grep '\\007' "$FILE" && echo "$BASHISHNAME: warning: use \${BEL} instead of \\007 since it may not work on some shells"
		grep '\\e' "$FILE" && echo "$BASHISHNAME: warning: use \${ESC} instead of \\e since it may not work on some shells"
		grep '\\d' "$FILE" && echo "$BASHISHNAME: warning: use \${DATE} instead of \\d since it may not work on some shells"
		grep '\\t' "$FILE" && echo "$BASHISHNAME: warning: use \${TIME} instead of \\t since it may not work on some shells"
		grep '%{' "$FILE" && echo "$BASHISHNAME: warning: use \${EMBED} and \${UNEMBED} instead of %{ and %} since it may not work on some shells"
		grep '\\\[' "$FILE" && echo "$BASHISHNAME: warning: use \${EMBED} and \${UNEMBED} instead of \\[ and \\] since it may not work on some shells"
		grep '${HOSTNAME}' "${FILE}" && echo "$BASHISHNAME: warning: use \${HOSTNAME%%.*} instead of \${HOSTNAME} since the hostname may include several subdomains"
		grep '$HOSTNAME' "${FILE}" && echo "$BASHISHNAME: warning: use \${HOSTNAME%%.*} instead of \$HOSTNAME since the hostname may include several subdomains"
	done
	
	 . ./theme ; test "x$BASHISH_THEME" != x|| errorhandler 25
	declare -f "_bashish_theme_${BASHISH_THEME}" 1>/dev/null 2>/dev/null || errorhandler 251
	 
	unset -f "_bashish_theme_$BASHISH_THEME"
	## the "-z" flag to tar is a GNU extension
	## hence a more compatible tar command must be issued
	_bashish_find "$BASHISH_THEME"
	THEMEFILE="${THEMEFILE#$BASHISHDIR}"
	THEMEFILE="${THEMEFILE#$HOME/.bashish}"
	if test "x$THEMEFILE" != x
	then
		printf "${0##*/}: update ${THEMEFILE#/themes/}? [yes]"
		read input
		case $input in
		""|y*|Y*):;;
		*)errorhandler 24;;
		esac
		mkdir -p "$HOME/.bashish/${THEMEFILE%/*}"
	else
		while test "x$THEMEDIR" = x
		do
			printf "${0##*/}: specify themedirectory:\n$HOME/.bashish/themes/"
			read THEMEDIR
		done
		THEMEFILE="themes/$THEMEDIR/${BASHISH_THEME}"
	fi
	THEMEFILE="$HOME"/.bashish/${THEMEFILE}
	mkdir -p "${THEMEFILE}"
	rm -f "${THEMEFILE}"/*
	## only copy the needed files
	for FILE in *
	do
		case "${FILE}" in
		## do nothing if temp-file or argv andor conf file
		*~|argv*|conf*) : do nothing ;;
		*)
		## save if file is nonzero 
		test -s "${FILE}" && cp "${FILE}" "${THEMEFILE}" || errorhandler 27;;esac;done
	(
		. ./theme
		## test if this is the prompt theme
		OLD_BASHISH_THEME="$BASHISH_THEME"
		. "$HOME"/.bashish/prompt/theme
	)
}
_bashish_save
