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

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

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

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

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

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

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

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

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

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

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

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

if bt "$(ini_get restore_directory)"; then
	if [ -f "${HOME}/.lastpwd" ]; then
		lastpwd=$(cat "${HOME}/.lastpwd")
		[ -d "${lastpwd}" ] && dbg_log cd "${lastpwd}"
	fi
fi

if bt "$(ini_get colored_grep)" && bt "$(ini_get enable_colors)"; then
	dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" Colored grep "$(eval_gettext "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
	[[ $(bt "$(ini_get dark_terminal)") && ${GREP_COLOR} == "01;38;5;0" ]] && GREP_COLOR="01;38;5;97"
	[[ $(bf "$(ini_get dark_terminal)") && ${GREP_COLOR} == "01;38;5;97" ]] && GREP_COLOR="01;38;5;0"

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

dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" dd "$(eval_gettext "Options")"
dd_opts=""

if bt "$(ini_get dd_noerror)"; then
	dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" dd "$(eval_gettext "don't abort at errors")"
	dbg_log export dd_opts+="conv=noerror "
fi

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

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

dbg_log alias dd="dd ${dd_opts}"

if bt "$(ini_get use_bat)"; then
	if command -v batcat >/dev/null; then
		batcmd=batcat
	elif command -v bat >/dev/null; then
		batcmd=bat
	else	batcmd=""
	fi

	if [ -n "${batcmd}" ]; then
		dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" bat "$(eval_gettext "as replacement for")" cat

		if [ -n "$(ini_get bat_theme)" ]; then
			dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" bat "$(eval_gettext "Theme")" "$(ini_get bat_theme)"
			dbg_log export BAT_THEME="$(ini_get bat_theme)"
		fi

		if [ "$(ini_get bat_tabwidth)" -gt 0 ]; then
			dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" bat "$(eval_gettext "Tab Width")" "$(ini_get bat_tabwidth)"
			dbg_log alias cat="${batcmd} --tabs $(ini_get bat_tabwidth)"
		else
			dbg_log alias cat="${batcmd}"
		fi
	fi
fi

if bt "$(ini_get curl_useragent)"; then
	if [ -n "$(ini_get curl_useragent_string)" ]; then
		grep -q user-agent "${HOME}/.curlrc" 2>/dev/null && \
			sed -e '/user-agent/d' -i "${HOME}/.curlrc"

		echo "user-agent = \"$(ini_get curl_useragent_string)\"" >> "${HOME}/.curlrc"
	fi
fi

if bt "$(ini_get wget_useragent)"; then
	if [ -n "$(ini_get wget_useragent_string)" ]; then
		grep -q user_agent "${HOME}/.wgetrc" 2>/dev/null && \
			sed -e '/user_agent/d' -i "${HOME}/.wgetrc"

		echo "user_agent = \"$(ini_get wget_useragent_string)\"" >> "${HOME}/.wgetrc"
	fi
fi

dbg_msg "$(eval_gettext "BashStyle-NG Setting:")" "$(eval_gettext "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)"

unset lastpwd dd_opts dd_version batcmd
