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

source $BSNG_RC_DIR/internal/keys
source $BSNG_RC_DIR/internal/gconf

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

_export ()
{

	PROFILE=$2

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

	echo BashStyle-Profile > $PROFILE

	for key in ${BASH_KEYS[@]} ${ALIAS_KEYS[@]} ${COLOR_KEY[@]} \
		${DIRCOLOR_KEYS[@]} ${SEPARATOR_KEYS[@]} ${NANO_KEYS[@]} \
		${READLINE_KEYS[@]} ${SHOPT_KEYS[@]} ${VIMKEYS[@]}; do
		echo "$key="\"$(get_key $key)\" >> $PROFILE
	done

	echo -e "Profile created in:${eiceblue} $PROFILE"

}

_import ()
{

	PROFILE=$2

	if [[ $PROFILE == "" ]]; then \
		echo "No Profile given" && exit 1; \
	elif [[ ! -e $PROFILE ]]; then \
		echo "Profile does not exist" && exit 1; \
	elif [[ $(grep -w "BashStyle-Profile" $PROFILE) == "" ]]; then \
		echo "This is not a profile" && exit 1; \
	fi

	for key in ${BASH_KEYS[@]} ${ALIAS_KEYS[@]} ${COLOR_KEY[@]} \
		${DIRCOLOR_KEYS[@]} ${SEPARATOR_KEYS[@]} ${NANO_KEYS[@]} \
		${READLINE_KEYS[@]} ${SHOPT_KEYS[@]} ${VIMKEYS[@]}; do
		set_key $(gconftool-2 -T /apps/bashstyle/$key) $key \
			"$( grep -w $key $PROFILE | gawk -F= '{print $2}' | sed 's/\"//g' )"
	done

	echo "$PROFILE imported"

}

case $1 in

	*export )

		_export $1 $2

	;;

	*import )

		_import $1 $2

	;;

	* )

		echo -e "\n${ewhite}Usage:\n"
		echo -e "${eorange}bs-ng-profiler${ewhite} |${egreen} --export <profile>${eiceblue} [export a profile]\
		\n${eorange}bs-ng-profiler${ewhite} |${egreen} --import <profile>${eiceblue} [import a profile]" | column -t
		echo ""
		tput sgr0

	;;

esac
