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

dbg_log export HISTTIMEFORMAT="$(ini_get history_timeformat)"
dbg_msg $"BashStyle-NG Setting:" HISTTIMEFORMAT "${HISTTIMEFORMAT}"

dbg_log export HISTCONTROL="$(ini_get history_control)"
dbg_msg $"BashStyle-NG Setting:" HISTCONTROL "${HISTCONTROL}"

dbg_log export HISTIGNORE="$(ini_get history_ignore)"
dbg_msg $"BashStyle-NG Setting:" HISTIGNORE "${HISTIGNORE}"

dbg_log export FIGNORE="$(ini_get completion_ignore)"
dbg_msg $"BashStyle-NG Setting:" FIGNORE "${FIGNORE}"

dbg_log export GLOBIGNORE="$(ini_get globignore)"
dbg_msg $"BashStyle-NG Setting:" GLOBIGNORE "${GLOBIGNORE}"

if [[ ${BSNG_BASHVER} -ge 44 ]]; then
	dbg_log export EXECIGNORE="$(ini_get exec_ignore)"
	dbg_msg $"BashStyle-NG Setting:" EXECIGNORE "${EXECIGNORE}"
fi

dbg_log export CDPATH="$(ini_get cdpath)"
dbg_msg $"BashStyle-NG Setting:" CDPATH "${CDPATH}"

dbg_log export FCEDIT="$(ini_get fcedit)"
dbg_msg $"BashStyle-NG Setting:" FCEDIT "${FCEDIT}"

if [[ $(grep -w TMOUT /etc/profile.d/*msec.sh 2>/dev/null) != "" ]]; then
	echo -e "\n * /etc/profile.d/msec.sh prevents setting TMOUT!"
else
	dbg_msg $"BashStyle-NG Setting:" TMOUT "${TMOUT}"
	export TMOUT
	TMOUT="$(ini_get timeout)"
fi

dbg_log export HISTSIZE="$(ini_get history_size)"
dbg_msg $"BashStyle-NG Setting:" HISTSIZE "${HISTSIZE}"

dbg_log export HISTFILESIZE="$(ini_get history_size)"
dbg_msg $"BashStyle-NG Setting:" HISTFILESIZE "${HISTFILESIZE}"

if [[ $(ini_get restore_directory) == True ]]; then
	dbg_log trap 'printf %s "${PWD}" > ${HOME}/.lastpwd' EXIT
	if [[ -d "$(cat "${HOME}"/.lastpwd 2>/dev/null)" ]]; then
		dbg_log cd "$(cat "${HOME}"/.lastpwd)"
	fi
fi

if [[ $(ini_get colored_grep) == True && $(ini_get enable_colors) == True ]]; then
	dbg_msg $"BashStyle-NG Setting:" Colored grep $"On"
	alias grep="grep --color=auto"
	GREP_COLOR="$(ini_get grep_color);"

	# make black (01;38;5;0) white on dark terminal
	# make white (01;38;5;97) black on bright terminal
	[[ $(ini_get dark_terminal) == True && ${GREP_COLOR} == "01;38;5;0" ]] && GREP_COLOR="01;38;5;97"
	[[ $(ini_get dark_terminal) == False && ${GREP_COLOR} == "01;38;5;97" ]] && GREP_COLOR="01;38;5;0"

else	dbg_msg $"BashStyle-NG Setting:" Colored grep $"Off"
	alias grep="grep --color=no"
fi

dbg_msg $"BashStyle-NG Setting:" dd $"Options"
DD_OPTS=""

if [[ $(ini_get dd_noerror) == True ]]; then
	dbg_msg $"BashStyle-NG Setting:" dd $"don't abort at errors"
	dbg_log export DD_OPTS="${DD_OPTS} conv=noerror"
fi

# status=progress is new in dd/coreutils 8.24
DD_VERSION="$(gawk '/^dd/{gsub("\\.","") ; print $3}'  <(dd --version))"

if [[ $(ini_get dd_progress) == True && ${DD_VERSION} -ge 824 ]]; then
	dbg_msg $"BashStyle-NG Setting:" dd $"display progressbar"
	dbg_log export DD_OPTS="${DD_OPTS} status=progress"
elif [[ $(ini_get dd_progress) == True ]]; then
	dbg_msg $"BashStyle-NG Setting:" dd $"display progressbar" \
		$"was requested by user, but coreutils is not 8.24 or newer"
fi

__dd () {
	dd ${DD_OPTS} "${@}"
}

dbg_log alias dd=__dd

dbg_msg $"BashStyle-NG Setting:" $"Various Options"
dbg_log export pwdmaxlen="$(ini_get pwdlength)"
dbg_log export trunc_symbol="$(ini_get pwdcut)"
dbg_log export trunc_length="$((${#trunc_symbol}-1))"
dbg_log export dirchar="$(ini_get directory_indicator)"
dbg_log export user_char="$(ini_get user_char)"
