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

keybindings_setter () {
	if [[ "${1}" != "" ]]; then
		MOD=${1/:*}
		KEY=${1/*:}

		case ${2} in
			complete-path)	ACT='"\eb`which \ef` \e\C-e"' ;;
			*)		ACT=${2} ;;
		esac

		if [[ ${MOD} == e ]]; then
			dbg_msg "$ReadlineCFG Setting:" $"${ACT}" $"e-${KEY}"
			bind "\\e${KEY}":self-insert
			echo -E \"\\e${KEY}\":${ACT} >> $RC_FILE
		elif [[ ${MOD} == C ]]; then
			dbg_msg "$ReadlineCFG Setting:" $"${ACT}" $"C-${KEY}"
			bind "\\C-${KEY}":self-insert
			echo -E \"\\C-${KEY}\":${ACT} >> $RC_FILE
		fi
	fi
}

if [[ ! -e $HOME/.keybindings.bs-ng.* ]]; then

	export RC_FILE=$HOME/.keybindings.bs-ng.$(date +%I-%M-%S)
	dbg_msg $"ReadlineCFG Loading:" $"Keybindings"

	for key in undo upcase_word capitalize_word downcase_word transpose_words \
		transpose_chars unix_word_rubout kill_word possible_filename_completions \
		possible_hostname_completions possible_username_completions \
		possible_variable_completions kill_line unix_line_discard \
		beginning_of_line end_of_line clear_screen history_search_forward \
		history_search_backward complete_path; do

			keybindings_setter $(ini_get $key) ${key//_/-}

	done

	mv ${RC_FILE} ${HOME}/.keybindings
	bind -f ${HOME}/.keybindings
	unset RC_FILE

fi

if [[ $(ini_get tab_rotation) == True ]]; then
	dbg_msg $"BashStyle-NG Setting:" $"Tab-Rotation" $"On"
	dbg_log bind 'TAB:menu-complete'
	dbg_log complete -d -X '.[a-zA-Z0-9]*' cd
else	dbg_msg $"BashStyle-NG Setting:" $"Tab-Rotation" $"Off"
fi
