#!/bin/bash

# Function for create vimrc
# Syntax: create_config <user>
function create_config() {
  vimrc_user="${1}"
  vimrc_home="/root"
  if [ "${vimrc_user}" == "root" ] ; then
    vimrc_home="/root"
  else
    vimrc_home="/Users/${1}"
  fi
  echo '"set termguicolors' > ${vimrc_home}/.vimrc
  echo ':set formatoptions=tcqrn1' >> ${vimrc_home}/.vimrc
  echo ':set tabstop=2' >> ${vimrc_home}/.vimrc
  echo ':set shiftwidth=2' >> ${vimrc_home}/.vimrc
  echo ':set softtabstop=2' >> ${vimrc_home}/.vimrc
  echo ':set expandtab' >> ${vimrc_home}/.vimrc
  echo ':set noshiftround' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Color scheme' >> ${vimrc_home}/.vimrc
  echo '":hi clear' >> ${vimrc_home}/.vimrc
  echo ':colorscheme default' >> ${vimrc_home}/.vimrc
  echo '":set background=dark' >> ${vimrc_home}/.vimrc
  echo '":hi Normal ctermfg=145 ctermbg=234' >> ${vimrc_home}/.vimrc
  echo '":hi Normal guifg=#ABB2BF guibg=#282C34 gui=NONE cterm=NONE' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo "let &t_ut=''" >> ${vimrc_home}/.vimrc
  echo ':tab all' >> ${vimrc_home}/.vimrc
  echo '" Change fonts for Windows' >> ${vimrc_home}/.vimrc
  echo '":set guifont=Consolas:h11:cANSI' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo 'if has("gui_macvim")' >> ${vimrc_home}/.vimrc
  echo '    set guifont=Menlo\ Regular:h14' >> ${vimrc_home}/.vimrc
  echo 'endif' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Hide menu/toolbar of GVIM' >> ${vimrc_home}/.vimrc
  echo '":set guioptions -=T' >> ${vimrc_home}/.vimrc
  echo '":set guioptions -=r' >> ${vimrc_home}/.vimrc
  echo '":set guioptions -=L' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Helps force plug-ins to load correctly when it is turned back on below.' >> ${vimrc_home}/.vimrc
  echo 'filetype off' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Turn on syntax highlighting.' >> ${vimrc_home}/.vimrc
  echo 'syntax on' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" For plug-ins to load correctly.' >> ${vimrc_home}/.vimrc
  echo 'filetype plugin indent on' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Set status line display' >> ${vimrc_home}/.vimrc
  echo "set statusline=%F%m%r%h%w\ [%{&ff}]\ [filetype=%Y]\ [%l,%v][%p%%]\ [%{strftime('%F')}\ %{strftime('%T')}]" >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Encoding' >> ${vimrc_home}/.vimrc
  echo 'set encoding=utf-8' >> ${vimrc_home}/.vimrc
  echo 'set wildmenu' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Fixes common backspace problems' >> ${vimrc_home}/.vimrc
  echo 'set backspace=indent,eol,start' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Status bar' >> ${vimrc_home}/.vimrc
  echo 'set laststatus=2' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" Display options' >> ${vimrc_home}/.vimrc
  echo ':set showmode' >> ${vimrc_home}/.vimrc
  echo ':set showcmd' >> ${vimrc_home}/.vimrc
  echo '' >> ${vimrc_home}/.vimrc
  echo '" File explorer' >> ${vimrc_home}/.vimrc
  echo '"nnoremap <leader>n :Explore<CR>' >> ${vimrc_home}/.vimrc
  echo '"nnoremap <C-n> :Lexplore<CR>' >> ${vimrc_home}/.vimrc
  echo '"nnoremap <C-t> :Vexplore<CR>' >> ${vimrc_home}/.vimrc
  echo '"nnoremap <C-f> :Texplore<CR>' >> ${vimrc_home}/.vimrc
  rm -rf ${vimrc_home}/.vim
  chown ${vimrc_user} ${vimrc_home}/.vimrc 2> /dev/null
  echo "# Created ${vimrc_home}/.vimrc"
}

# Check root permissions
function rootMessage() {
  mkdir -p /etc/root &> /dev/null
  administrator=$?
  if [ ${administrator} -eq 0 ] ; then
    rm -rf /etc/root
  else
    echo ""
    echo "* Administrator permissions are required"
    echo ""
    exit
  fi
}

function check_dependencies() {
  wget --help > /dev/null
  error=$?
  if [ ${error} -ne 0 ] ; then
    echo ""    
    echo "* Command 'wget' not installed."
    echo ""
    exit
  fi
}

# Generate configuration file
rootMessage
check_dependencies
list_users=$(ls -1 /Users)
list_users="root ${list_users}"
for current_user in ${list_users} ; do
  if [ -d /Users/${current_user} ] ; then
    create_config "${current_user}"
  elif [ -d /${current_user} ] ; then
    create_config "${current_user}"
  fi
done
