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

white=${white}
orange=${orange}
iceblue=${iceblue}
green=${green}

BASH_KEYS="alias1 alias2 alias3 alias4 alias5 alias6 alias7 cdpath char_ps234 char_separator colored_ls colored_man \
color_style date_color fcedit fignore font_color grep_color hist_control hist_ignore hist_size host_color \
man_style path prompt_style ps234_color pwdchar pwdmaxlen random sepa_color show_files_amount show_size_of_files \
show_tty show_uptime time_color tmout uptime_color use_bashstyle use_grep_color user_color wdir_color \
welcome_message show_load show_proc show_mem show_battery_load tabrotate"

READLINE_KEYS="ambiguous bellstyle comp_h editing_mode enable_completion hidden ignore_case mark_dirs \
mark_sym_dirs scroll_h use_rlconfig vstats"

SHOPT_KEYS="cdable_vars cdspell checkhash cmdhist force_fignore histappend histreedit no_empty_cmd_completion"

VIM_KEYS="autosave 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"

NANO_KEYS="use_nanocfg auto_indent backup bold_text case_sensitive const more_space nohelp use_colors"

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

case $1 in

	*export )

		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="\"$(gconftool-2 --get /apps/bashstyle/$key)\" >> $PROFILE; \
		done

		for key in $READLINE_KEYS; do \
			echo "$key="\"$(gconftool-2 --get /apps/bashstyle/readline/$key)\" >> $PROFILE; \
		done

		for key in $SHOPT_KEYS; do \
			echo "$key="\"$(gconftool-2 --get /apps/bashstyle/shopt/$key)\" >> $PROFILE; \
		done

		for key in $VIM_KEYS; do \
			echo "$key="\"$(gconftool-2 --get /apps/bashstyle/vim/$key)\" >> $PROFILE; \
		done

		for key in $NANO_KEYS; do \
			echo "$key="\"$(gconftool-2 --get /apps/bashstyle/nano/$key)\" >> $PROFILE; \
		done

		echo "Profile created in: $PROFILE"

	;;

	*import )

		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 \
			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 $READLINE_KEYS; do \
			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 \
			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 \
			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 \
			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"

	;;

	* )

		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

	;;

esac
