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

if [[ -e $HOME/.vimrc && ( ! -w $HOME/.vimrc || ! -r $HOME/.vimrc ) ]]; then
	ERR_MSG+="\n * $HOME/.vimrc is not accessible! -- adjust permissions"
else

if [[ ! $BSNG_RC_GEN ]]; then
	export RC_FILE=$HOME/.vimrc.$(date +%I-%M-%S)
fi

dbg_msg $"VimCFG Setting:" $"runtimepath"
rc_add "set runtimepath+=$BSNG_PREFIX/share/bashstyle-ng/vim/"

if [[ $(get_key vim/vim_colorscheme) != "default" ]]; then
	colorscheme=$(get_key vim/vim_colorscheme)
	dbg_msg $"VimCFG Setting:" $"Colorscheme" $colorscheme
	rc_add "colorscheme $colorscheme"
fi

if [[ $(get_key vim/syntax_hilight) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Syntax" $"On"
	rc_add "syntax on"
else
	dbg_msg $"VimCFG Setting:" $"Syntax" $"Off"
	rc_add "syntax off"
fi

if [[ $(get_key vim/dark_background) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Background dark"
	rc_add "set background=dark"
else
	dbg_msg $"VimCFG Setting:" $"Background light"
	rc_add "set background=light"
fi

if [[ $(get_key vim/jump_back) == "true" ]]; then

	if [[ -e $HOME/.viminfo && ( ! -w $HOME/.viminfo || ! -r $HOME/.viminfo ) ]]; then
		ERR_MSG+="\n * $HOME/.viminfo is not accessible! -- adjust permissions."
	else
		dbg_msg $"VimCFG Setting:" $"Jump-Back" $"On"
		rc_add "if has(\"autocmd\")"
		rc_add "    au BufReadPost * if line(\"'\\\"\") > 0 && line(\"'\\\"\") <= line(\"$\")"
		rc_add "    \| exe \"normal! g'\\\"\" | endif"
		rc_add "endif"
	fi

else	dbg_msg $"VimCFG Setting:" $"Jump-Back" $"Off"
fi

if [[ $(get_key vim/filetype_indent) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Filetype-Indention" $"On"
	rc_add "if has(\"autocmd\")"
	rc_add "    filetype plugin indent on"
	rc_add "endif"
	dbg_msg $"VimCFG Setting:" $"Auto-Indent" $"On"
	rc_add "set autoindent"
	dbg_msg $"VimCFG Setting:" $"Smart-Indent" $"On"
	rc_add "set smartindent"
else
dbg_msg $"VimCFG Setting:" $"Filetype-Indention" $"Off"
	dbg_msg $"VimCFG Setting:" $"Auto-Indent" $"Off"
	rc_add "set noautoindent"
	dbg_msg $"VimCFG Setting:" $"Smart-Indent" $"Off"
	rc_add "set nosmartindent"
fi

if [[ $(get_key vim/show_cmd) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Show-Command" $"On"
	rc_add "set showcmd"
else
	dbg_msg $"VimCFG Setting:" $"Show-Command" $"Off"
	rc_add "set noshowcmd"
fi

if [[ $(get_key vim/show_match) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Show-Matches" $"On"
	rc_add "set showmatch"
else
	dbg_msg $"VimCFG Setting:" $"Show-Matches" $"Off"
	rc_add "set noshowmatch"
fi

if [[ $(get_key vim/ignore_case) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Ignore-Case" $"On"
	rc_add "set ignorecase"
else
	dbg_msg $"VimCFG Setting:" $"Ignore-Case" $"Off"
	rc_add "set noignorecase"
fi

if [[ $(get_key vim/smart_case) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Smart-Case" $"On"
	rc_add "set smartcase"
else
	dbg_msg $"VimCFG Setting:" $"Smart-Case" $"Off"
	rc_add "set nosmartcase"
fi

if [[ $(get_key vim/inc_search) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Incremental-Search" $"On"
	rc_add "set incsearch"
else
	dbg_msg $"VimCFG Setting:" $"Incremental-Search" $"Off"
	rc_add "set noincsearch"
fi

if [[ $(get_key vim/highlight) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Highlight" $"On"
	rc_add "set hls"
else
	dbg_msg $"VimCFG Setting:" $"Highlight" $"Off"
	rc_add "set nohls"
fi

if [[ $(get_key vim/number) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Show-Number" $"On"
	rc_add "set number"
else
	dbg_msg $"VimCFG Setting:" $"Show-Number" $"Off"
	rc_add "set nonumber"
fi

if [[ $(get_key vim/autosave) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Auto-Save" $"On"
	rc_add "set autowriteall"
else
	dbg_msg $"VimCFG Setting:" $"Auto-Save" $"Off"
	rc_add "set noautowriteall"
fi

if [[ $(get_key vim/hl_cursor) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Highlight-Current-Line" $"On"
	rc_add "set cursorline"
	rc_add "hi CursorLine term=none cterm=underline ctermbg=none"
else
	dbg_msg $"VimCFG Setting:" $"Highlight-Current-Line" $"Off"
	rc_add "set nocursorline"
fi

if [[ $(get_key vim/hl_column) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Highlight-Current-Column" $"On"
	rc_add "set cursorcolumn"
	rc_add "hi CursorColumn term=none cterm=underline ctermbg=none"
else
	dbg_msg $"VimCFG Setting:" $"Highlight-Current-Column" $"Off"
	rc_add "set nocursorcolumn"
fi

if [[ $(get_key vim/ruler) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Show-Ruler" $"On"
	rc_add "set ruler"
	dbg_msg $"VimCFG Setting:" $"Ruler-Format" $(get_key vim/rulerformat)
	rc_add "set rulerformat=$(get_key vim/rulerformat)"
else
	dbg_msg $"VimCFG Setting:" $"Show-Ruler" $"Off"
	rc_add "set noruler"
fi

if [[ $(get_key vim/backup) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Backup" $"On"
	rc_add "set backup"
else
	dbg_msg $"VimCFG Setting:" $"Backup" $"Off"
	rc_add "set nobackup"
fi

if [[ $(get_key vim/startofline) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Start-Of-Line" $"On"
	rc_add "set startofline"
else
	dbg_msg $"VimCFG Setting:" $"Start-Of-Line" $"Off"
	rc_add "set nostartofline"
fi

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

if [[ $(get_key vim/wrap) == true ]]; then
	dbg_msg $"VimCFG Setting:" $"Linewrapping" $"On"
	rc_add "set wrap"
	dbg_msg $"VimCFG Setting:" $"Autowrap-length" $(get_key vim/wrap_length)
	rc_add "set tw=$(get_key vim/wrap_length)"
else
	dbg_msg $"VimCFG Setting:" $"Linewrapping" $"Off"
	rc_add "set nowrap"
fi

dbg_msg $"VimCFG Setting:" $"Tab-width" $(get_key vim/tabstop)
rc_add "set tabstop=$(get_key vim/tabstop)"
rc_add "set shiftwidth=$(get_key vim/tabstop)"

if [[ $(get_key vim/chdir) == "true" ]]; then
	dbg_msg $"VimCFG Setting:" $"Auto chdir" $"On"
	rc_add "set autochdir"
else
	dbg_msg $"VimCFG Setting:" $"Auto chdir" $"Off"
	rc_add "set noautochdir"
fi

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

if [[ ! $BSNG_RC_GEN ]]; then
	mv $RC_FILE $HOME/.vimrc
	unset RC_FILE
fi

dbg_msg $"VimCFG Loading:" $"BashStyle-NG"

fi
