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

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="use_vimcfg dark_background syntax_hilight jump_back show_cmd filetype_indent show_match ignore_case \
smart_case"

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

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

PROFILE=$2

case $1 in
	*export)
		if [[ ! -a $HOME/bashstyle ]]; then \
			mkdir $HOME/bashstyle; \
		fi
		echo BashStyle-Profile > $HOME/bashstyle/profile-$DATE
		for key in $BASH_KEYS; \
			do echo "$key="\"$(gconftool-2 --get /apps/bashstyle/$key)\" \
				>> $HOME/bashstyle/profile-$DATE; \
		done
		for key in $READLINE_KEYS; \
			do echo "$key="\"$(gconftool-2 --get /apps/bashstyle/readline/$key)\" \
				>> $HOME/bashstyle/profile-$DATE; \
		done
		for key in $SHOPT_KEYS; \
			do echo "$key="\"$(gconftool-2 --get /apps/bashstyle/shopt/$key)\" \
				>> $HOME/bashstyle/profile-$DATE; \
		done
		for key in $VIM_KEYS; \
			do echo "$key="\"$(gconftool-2 --get /apps/bashstyle/vim/$key)\" \
				>> $HOME/bashstyle/profile-$DATE; \
		done
		for key in $NANO_KEYS; \
			do echo "$key="\"$(gconftool-2 --get /apps/bashstyle/nano/$key)\" \
				>> $HOME/bashstyle/profile-$DATE; \
		done
		echo "Profile created in: $HOME/bashstyle/profile-$DATE"
	;;

	*import)
		if [[ $PROFILE == "" ]]; then echo "No Profile given" && exit; fi
		if [[ ! -a $PROFILE ]]; then echo "Profile does not exist" && exit; fi
		if [[ $(grep -w BashStyle-Profile $PROFILE) == "" ]]; then echo "This is not a profile" && exit; 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 "Usage: bs-ng-profiler --export		 export a profile"
		echo -e "Usage: bs-ng-profiler --import <profile> import a profile\n"
	;;
esac
