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

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

if (( "${#bsng_vimrc[@]}" == 0 )); then
	dbg_msg "$(eval_gettext "BashStyle-NG Loading:")" VimCFG
	export RC_FILE
	RC_FILE="${HOME}/.vimrc.bs-ng.${BSNG_SESSION_TIME}_${BASH_SESSION_PID}"

	if bt "$(ini_get syntax_hilight)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Syntax "$(eval_gettext "On")"
		rc_add "syntax on"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Syntax "$(eval_gettext "Off")"
		rc_add "syntax off"
	fi

	if bt "$(ini_get dark_background)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Background dark
		rc_add "set background=dark"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Background light
		rc_add "set background=light"
	fi

	if bt "$(ini_get jump_back)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Jump-Back "$(eval_gettext "On")"
		rc_add "if has(\"autocmd\")"
		rc_add "    au BufReadPost * if line(\"'\\\"\") > 0 && line(\"'\\\"\") <= line(\"$\")"
		rc_add "    \| exe \"normal! g'\\\"\" | endif"
		rc_add "endif"
	else	dbg_msg "$(eval_gettext "VimCFG Setting:")" Jump-Back "$(eval_gettext "Off")"
	fi

	if bt "$(ini_get filetype_indent)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Filetype-Indention "$(eval_gettext "On")"
		rc_add "if has(\"autocmd\")"
		rc_add "    filetype plugin indent on"
		rc_add "endif"
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Auto-Indent "$(eval_gettext "On")"
		rc_add "set autoindent"
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Smart-Indent "$(eval_gettext "On")"
		rc_add "set smartindent"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Filetype-Indention "$(eval_gettext "Off")"
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Auto-Indent "$(eval_gettext "Off")"
		rc_add "set noautoindent"
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Smart-Indent "$(eval_gettext "Off")"
		rc_add "set nosmartindent"
	fi

	if bt "$(ini_get show_command)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Command "$(eval_gettext "On")"
		rc_add "set showcmd"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Command "$(eval_gettext "Off")"
		rc_add "set noshowcmd"
	fi

	if bt "$(ini_get highlight_matches)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Matches "$(eval_gettext "On")"
		rc_add "set showmatch"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Matches "$(eval_gettext "Off")"
		rc_add "set noshowmatch"
	fi

	if bt "$(ini_get ignore_case)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Ignore-Case "$(eval_gettext "On")"
		rc_add "set ignorecase"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Ignore-Case "$(eval_gettext "Off")"
		rc_add "set noignorecase"
	fi

	if bt "$(ini_get smart_case)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Smart-Case "$(eval_gettext "On")"
		rc_add "set smartcase"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Smart-Case "$(eval_gettext "Off")"
		rc_add "set nosmartcase"
	fi

	if bt "$(ini_get incremental_search)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Incremental-Search "$(eval_gettext "On")"
		rc_add "set incsearch"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Incremental-Search "$(eval_gettext "Off")"
		rc_add "set noincsearch"
	fi

	if bt "$(ini_get highlight_brackets)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Highlight "$(eval_gettext "On")"
		rc_add "set hls"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Highlight "$(eval_gettext "Off")"
		rc_add "set nohls"
	fi

	if bt "$(ini_get show_lineno)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Number "$(eval_gettext "On")"
		rc_add "set number"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Number "$(eval_gettext "Off")"
		rc_add "set nonumber"
	fi

	if bt "$(ini_get autosave)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Auto-Save "$(eval_gettext "On")"
		rc_add "set autowriteall"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Auto-Save "$(eval_gettext "Off")"
		rc_add "set noautowriteall"
	fi

	if bt "$(ini_get highlight_line)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Highlight-Current-Line "$(eval_gettext "On")"
		rc_add "set cursorline"
		rc_add "hi CursorLine term=none cterm=underline ctermbg=none"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Highlight-Current-Line "$(eval_gettext "Off")"
		rc_add "set nocursorline"
	fi

	if bt "$(ini_get highlight_column)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Highlight-Current-Column "$(eval_gettext "On")"
		rc_add "set cursorcolumn"
		rc_add "hi CursorColumn term=none cterm=underline ctermbg=none"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Highlight-Current-Column "$(eval_gettext "Off")"
		rc_add "set nocursorcolumn"
	fi

	if bt "$(ini_get ruler)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Ruler "$(eval_gettext "On")"
		rc_add "set ruler"
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Ruler-Format "$(ini_get rulerformat)"
		rc_add "set rulerformat=$(ini_get rulerformat)"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Show-Ruler "$(eval_gettext "Off")"
		rc_add "set noruler"
	fi

	if bt "$(ini_get vim_backup)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Backup "$(eval_gettext "On")"
		rc_add "set backup"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Backup "$(eval_gettext "Off")"
		rc_add "set nobackup"
	fi

	if bt "$(ini_get start_line)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Start-Of-Line "$(eval_gettext "On")"
		rc_add "set startofline"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Start-Of-Line "$(eval_gettext "Off")"
		rc_add "set nostartofline"
	fi

	dbg_msg "$(eval_gettext "VimCFG Setting:")" Linespace 0
	rc_add "set linespace=0"

	if bt "$(ini_get wrap_line)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Line-Wrapping "$(eval_gettext "On")"
		rc_add "set wrap"
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Autowrap-length "$(ini_get wrap_length)"
		rc_add "set tw=$(ini_get wrap_length)"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Line-Wrapping "$(eval_gettext "Off")"
		rc_add "set nowrap"
		rc_add "set tw=0"
	fi

	dbg_msg "$(eval_gettext "VimCFG Setting:")" Tab-width "$(ini_get tab_length)"
	rc_add "set tabstop=$(ini_get tab_length)"
	rc_add "set softtabstop=$(ini_get tab_length)"

	if bt "$(ini_get expandtab)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Expand Tab "$(eval_gettext "On")"
		rc_add "set expandtab"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Expand Tab "$(eval_gettext "Off")"
		rc_add "set noexpandtab"
	fi

	if bt "$(ini_get chdir)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Auto chdir "$(eval_gettext "On")"
		rc_add "set autochdir"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Auto chdir "$(eval_gettext "Off")"
		rc_add "set noautochdir"
	fi

	if bt "$(ini_get foldenable)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Fold Enable "$(eval_gettext "On")"
		rc_add "set foldenable"

		dbg_msg "$(eval_gettext "VimCFG Setting:")" Fold Level "$(ini_get foldlevelstart)"
		rc_add "set foldlevelstart=$(ini_get foldlevelstart)"

		dbg_msg "$(eval_gettext "VimCFG Setting:")" Fold Maximum Nest "$(ini_get foldnestmax)"
		rc_add "set foldnestmax=$(ini_get foldnestmax)"

		dbg_msg "$(eval_gettext "VimCFG Setting:")" Fold Method "$(ini_get foldmethod)"
		rc_add "set foldmethod=$(ini_get foldmethod)"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Fold Enable "$(eval_gettext "Off")"
		rc_add "set nofoldenable"
	fi

	if bt "$(ini_get wildmenu)"; then
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Wildmenu "$(eval_gettext "On")"
		rc_add "set wildmenu"
	else
		dbg_msg "$(eval_gettext "VimCFG Setting:")" Wildmenu "$(eval_gettext "Off")"
		rc_add "set nowildmenu"
	fi

	rc_add "if filereadable(\"~/.vimrc.custom\")"
	rc_add "	source ~/.vimrc.custom"
	rc_add "endif"

	mv "${RC_FILE}" "${HOME}/.vimrc"
	unset RC_FILE
fi

unset bsng_vimrc
