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

dbg_msg $"BashStyle-NG Loading:" KeybindingsCFG

function list-keybindings () {
	bind -P | sed -e '/is not/d;s/can be found on/:/' | column -s : -t
}

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

		case ${2} in
			complete-path)	ACT='"\eb`which \ef` \e\C-e"' ;;
			rerun-root)	ACT='"\ebsudo !!\e\C-e\C-j"' ;;
			list-keys)	ACT='"\eblist-keybindings\e\C-e\C-j"' ;;
			*)		ACT="${2}" ;;
		esac

		case ${MOD} in
			e)
				dbg_msg $"KeybindingsCFG Setting:" "${ACT}" e-"${KEY}"
				dbg_msg "\\e${KEY}":self-insert
				bind "\\e${KEY}":self-insert
				dbg_msg \"\\e"${KEY}"\":"${ACT}"
				echo -E \"\\e"${KEY}"\":"${ACT}" >> "${RC_FILE}"
			;;
			C)
				dbg_msg $"KeybindingsCFG Setting:" "${ACT}" C-"${KEY}"
				dbg_msg "\\C-${KEY}":self-insert
				bind "\\C-${KEY}":self-insert
				dbg_msg \"\\C-"${KEY}"\":"${ACT}"
				echo -E \"\\C-"${KEY}"\":"${ACT}" >> "${RC_FILE}"
			;;
			X)
				dbg_msg $"KeybindingsCFG Setting:" "${ACT}" "${KEY}"
				#bind "${KEY}":self-insert
				dbg_msg "${KEY}":"${ACT}"
				echo -E "${KEY}":"${ACT}" >> "${RC_FILE}"
			;;
		esac
	fi
}

shopt -s nullglob
bsng_keybindingsrc=( "${HOME}"/.keybindings.bs-ng.* )
shopt -u nullglob

if (( "${#bsng_keybindingsrc[@]}" == 0 )); then

	export RC_FILE
	RC_FILE="${HOME}/.keybindings.bs-ng.$(date +%I-%M-%S)"
	dbg_msg $"KeybindingsCFG 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 menu_complete alias_expand_line \
		backward_char backward_word backward_delete_char delete_char \
		copy_backward_word copy_forward_word forward_char forward_word \
		overwrite_mode menu_complete_backward rerun_root backward_kill_line \
		list_keys; do

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

	done

	if [[ $(ini_get prompt_style) == equinox ]]; then
		# make the prompt properly re-draw when pressing [Return]
		dbg_msg $"KeybindingsCFG Setting:" Equinox $"Return-Key compat"
		dbg_msg 'RETURN: "\C-l\C-j"'
		echo -E 'RETURN: "\C-l\C-j"' >> "${RC_FILE}"
	fi

	mv "${RC_FILE}" "${HOME}"/.keybindings
	unset RC_FILE

fi

rm -f "${HOME}"/.keybindings.bs-ng.*
bind -f "${HOME}"/.keybindings
