#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG  				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2010 Christopher Bratusek		#
#							#
#########################################################

source $BSNG_PREFIX/share/bashstyle-ng/rc/functions/bs-ng-echo

white=$(bs_ng_echo $white)
orange=$(bs_ng_echo $orange)
iceblue=$(bs_ng_echo $iceblue)
green=$(bs_ng_echo $green)

source $BSNG_PREFIX/share/bashstyle-ng/rc/functions/gconf

BASH_KEYS="cdpath colored_grep colored_ls colored_man custom_command custom_prompt \
debug debug_log debug_verbose dirchar fcedit fignore hist_control hist_ignore history_size \
man_style path prompt_style ps234 pwdchar pwdmaxlength random_style tabrotate timeout \
use_bashstyle use_custom_prompt welcome_message"

ALIAS_KEYS="one two three four five six seven eight nine"

COLOR_KEYS="style date font grep host ps234c separatorc time uptime enable user wdir"

DIRCOLOR_KEYS="custom deb dirs enable exec flac generic gif jpeg logs mp3 ogg png \
rar rpm tar tarbz2 targz zip"

SEPARATOR_KEYS="battery_load files_amount file_size load mem processes tty uptimes separator"

READLINE_KEYS="ambiguous bellstyle completion completionhz editing_mode hidden ignore_case \
mark_dirs mark_modlines mark_sym_dirs query_item scrollhz use_readlinecfg vstats"

SHOPT_KEYS="autocd cdable_vars cdspell checkhash checkjobs cmdhist dirspell force_fignore \
globstar histappend histreedit no_empty_cmd_completion"

VIM_KEYS="autosave backup dark_background filetype_indent highlight hl_cursor ignore_case \
inc_search jump_back number ruler rulerformat show_cmd show_match smart_case syntax_hilight \
use_vimcfg vim_colorscheme"

NANO_KEYS="auto_indent backup bold_text case_sensitive const more_space nohelp use_colors \
use_nanocfg history rebind_del rebind_kp"

DATE="$(date +%Y-%m-%d-%I-%M)"

_help ()
{

	echo -e "\n${white}Usage:\n"
	echo -e "${orange}bs-ng-profiler${white} |${green} --export <profile>${iceblue} [export a profile]\
	\n${orange}bs-ng-profiler${white} |${green} --import <profile>${iceblue} [import a profile]" | column -t
	echo ""
	tput sgr0

}

_bs_ng_export ()
{

	export BSNG_GCONF_READ=1
	PROFILE=$2

	if [[ $PROFILE == "" ]]; then
		PROFILE=$HOME/bashstyle/profile-$DATE
		if [[ ! -e $HOME/bashstyle ]]; then \
			mkdir $HOME/bashstyle; \
		fi; \
	fi

	echo BashStyle-Profile > $PROFILE

	for key in $BASH_KEYS; do \
		echo "$key="\"$(get_key $key)\" >> $PROFILE; \
	done

	for key in $ALIAS_KEYS; do \
		echo "$key="\"$(get_key alias/$key)\" >> $PROFILE; \
	done

	for key in $COLOR_KEYS; do \
		echo "$key="\"$(get_key color/$key)\" >> $PROFILE; \
	done

	for key in $DIRCOLOR_KEYS; do \
		echo "$key="\"$(get_key dircolors/$key)\" >> $PROFILE; \
	done

	for key in $SEPARATOR_KEYS; do \
		echo "$key="\"$(get_key separator/$key)\" >> $PROFILE; \
	done

	for key in $READLINE_KEYS; do \
		echo "$key="\"$(get_key readline/$key)\" >> $PROFILE; \
	done

	for key in $SHOPT_KEYS; do \
		echo "$key="\"$(get_key shopt/$key)\" >> $PROFILE; \
	done

	for key in $VIM_KEYS; do \
		echo "$key="\"$(get_key vim/$key)\" >> $PROFILE; \
	done

	for key in $NANO_KEYS; do \
		echo "$key="\"$(get_key nano/$key)\" >> $PROFILE; \
	done

	echo "Profile created in: $PROFILE"
	unset BSNG_GCONF_READ

}

_bs_ng_import ()
{
	export BSNG_GCONF_READ=1
	PROFILE=$2

	if [[ $PROFILE == "" ]]; then \
		echo "No Profile given" && exit 1; \
	fi

	if [[ ! -e $PROFILE ]]; then \
		echo "Profile does not exist" && exit 1; \
	      	fi

	if [[ $(grep -w "BashStyle-Profile" $PROFILE) == "" ]]; then \
		echo "This is not a profile" && exit 1; \
	fi

	for key in $BASH_KEYS; do \
		echo $key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/$key) \
				/apps/bashstyle/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
		fi; \
	done

	for key in $ALIAS_KEYS; do \
		echo alias/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/alias/$key) \
				/apps/bashstyle/alias/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
		fi; \
	done

	for key in $COLOR_KEYS; do \
		echo color/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/color/$key) \
				/apps/bashstyle/color/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
		fi; \
	done

	for key in $DIRCOLOR_KEYS; do \
		echo dircolors/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/dircolors/$key) \
				/apps/bashstyle/dircolors/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
		fi; \
	done

	for key in $SEPARATOR_KEYS; do \
		echo separator/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/separator/$key) \
				/apps/bashstyle/separator/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
			fi; \
	done

	for key in $READLINE_KEYS; do \
		echo readline/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/readline/$key) \
				/apps/bashstyle/readline/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
		fi; \
	done

	for key in $SHOPT_KEYS; do \
		echo shopt/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/shopt/$key) \
			/apps/bashstyle/shopt/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' )" ; \
		fi; \
	done

	for key in $VIM_KEYS; do \
		echo vim/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/vim/$key) \
				/apps/bashstyle/vim/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' ) "; \
		fi; \
	done

	for key in $NANO_KEYS; do \
		echo nano/$key
		if [[ $(grep -w $key $PROFILE) != "" ]]; then \
			gconftool-2 --set --type $(gconftool-2 -T /apps/bashstyle/nano/$key) \
			/apps/bashstyle/nano/$key "$(grep -w $key $PROFILE | sed -e "s/$key=//g" -e 's/\"//g' ) "; \
		fi; \
	done

	echo "$PROFILE imported"
	unset BSNG_GCONF_READ

}

case $1 in

	*export )

		_bs_ng_export $1 $2

	;;

	*import )

		_bs_ng_import $1 $2

	;;

	* )

		_help

	;;

esac
