#!/bin/bash
WHITE='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
# bash  <(curl -s https://files.ratchfordconsulting.com/software/software_install/linux/install_vim_plugins.sh)


echo -e "${WHITE}This script installs vim plugins and appends to your existing ~/.vimrc"
echo -e "${WHITE}You should examine the file and understand it's content before executing${RED}"
read -p "Do you really want to run some sketchy file from the internet? (yes/no) " yn

case $yn in 
	yes ) echo -e "${NC}ok... your funeral";;
	no ) echo -e "${NC}a wise choice...";
		exit;;
	* ) echo invalid response;
		exit 1;;
esac


# Install pathogen
mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

# Install vim plugins
mkdir -p ~/.vim/bundle
cd ~/.vim/bundle
git clone -q https://github.com/tpope/vim-fugitive 2>/dev/null
git clone -q https://github.com/msanders/snipmate.vim snipmate 2>/dev/null
git clone -q https://github.com/godlygeek/tabular 2>/dev/null
git clone -q https://github.com/scrooloose/syntastic 2>/dev/null
git clone -q https://github.com/rodjek/vim-puppet puppet 2>/dev/null
git clone -q https://github.com/vim-ruby/vim-ruby 2>/dev/null
git clone -q https://github.com/gabrielelana/vim-markdown 2>/dev/null
git clone -q https://github.com/fatih/vim-hclfmt 2>/dev/null
git clone -q https://github.com/hashivim/vim-hashicorp-tools 2>/dev/null

# Install everforest theme
mkdir -p ~/.vim/pack/colors/opt
git clone -q --depth 1 https://github.com/sainnhe/everforest.git ~/.vim/pack/colors/opt/everforest 2>/dev/null
vim -u NONE -c  "helptags ~/.vim/pack/colors/opt/everforest/doc/" -c q

# Install NERDtree
git clone -q https://github.com/preservim/nerdtree.git ~/.vim/pack/vendor/start/nerdtree 2>/dev/null
vim -u NONE -c "helptags ~/.vim/pack/vendor/start/nerdtree/doc" -c q

# .vimrc heredoc
cat << EOF > ~/.vimrc.new
" New hotkeys
" Indent block	        (In either text mode or Visual Mode) : tab
" Unindent block	    (In either text mode or Visual Mode) : ^tab
" Toggle Line Numbering (Must be in command mode)            : F2
" Toggle Paste Mode     (Must be in text mode)               : F5
" Toggle NerdTree	    (Must be in command mode)            : Ctrl + n


" use visual bell instead of beeping
set vb

" incremental search
set incsearch

" Allow hidden buffers
set hidden

" syntax highlighting
" set t_Co=256
call pathogen#infect()
" set cursorline

packadd! everforest
syntax on
colorscheme everforest
set background=dark

" autoindent
set autoindent|set cindent

" 2 space tabs
set tabstop=2|set shiftwidth=2|set expandtab|set softtabstop=2

" show matching brackets
set showmatch

" show line numbers
set nonumber
nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>

" make tab in v mode ident code
vmap <tab> >gv
vmap <s-tab> <gv

" make tab in normal mode ident code
nmap <tab> I<tab><esc>
nmap <s-tab> ^i<bs><esc>

" paste mode - this will avoid unexpected effects when you
" cut or copy some text from one window and paste it in Vim.
set pastetoggle=<F5>

" Turn plugin on
filetype plugin indent on

if executable("vi")
  set foldenable
  set foldmarker={,}
  set foldmethod=marker
  set foldlevel=100
endif
set listchars=tab:>-,trail:-
set statusline=%F%m%r%h%w\ %y\ %=[%l/%L,%04v](%p%%)
set laststatus=2

" Buffer navigation
map <C-k> :bp<CR>
map <C-j> :bn<CR>
map <C-h> :tabp<CR>
map <C-l> :tabn<CR>

" File edit shortcuts
let mapleader=','
cnoremap %% <C-R>=expand('%:h').'/'<cr>
map <leader>ew :e %%
map <leader>es :sp %%
map <leader>ev :vsp %%
map <leader>et :tabe %%

" Set wrapping
set wrap
set linebreak
if executable("par")
    set formatprg=par\ -w80rq
endif

" vim bundles
nnoremap <C-n> :NERDTreeToggle<CR>
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

" Syntax
au BufRead,BufNewFile *.pp              set filetype=puppet

" Mouse
"set mouse=a
map <ScrollWheelUp> <C-Y>
map <ScrollWheelDown> <C-E>
set clipboard=unnamed


" Shared 'buffer'
vmap <C-c> :w! ~/.vimbuffer<CR>
nmap <C-c> :.w! ~/vimbuffer<CR>
" paste from buffer
nmap <C-p> :r ~/.vimbuffer<CR>

" Trailing Whitespace
highlight default link EndOfLineSpace ErrorMsg
match EndOfLineSpace / \+$/
autocmd InsertEnter * hi link EndOfLineSpace Normal
autocmd InsertLeave * hi link EndOfLineSpace ErrorMsg
function! TrimWhiteSpace()
    %s/\s\+$//e
endfunction
autocmd FileWritePre    * :call TrimWhiteSpace()
autocmd FileAppendPre   * :call TrimWhiteSpace()
autocmd FilterWritePre  * :call TrimWhiteSpace()
autocmd BufWritePre     * :call TrimWhiteSpace()

let g:syntastic_python_checkers = ['flake8']
let g:syntastic_python_flake8_post_args='--ignore=W504,E501'
set number

EOF

if [[ -f ~/.vimrc ]]; then
    cp ~/.vimrc ~/.vimrc.old
else
    touch ~/.vimrc 
fi

cat ~/.vimrc.new >> ~/.vimrc

echo -e "${GREEN}Updated ${WHITE}.vimrc${NC} and installed plugins"
echo -e "Backup of your old ${WHITE}.vimrc${NC} can be found at ${WHITE}${HOME}/.vimrc.old${NC}"
