#!/bin/bash

set -eu
set -o pipefail

######################################################################
# We install pathogen, jedi-vim, supertab, nerdtree, syntastic, MRU and a .vimrc .
# pathogen makes it easier to install/manage vim plugins.
# jedi-vim does identifier completion.  It's auto-enabled for .py?
# supertab enables tab completion for a bunch of things (?)
# nerdtree is a textual file browser which you get upon "editing" a directory.
# syntastic is like pyflakes, but it does many languages, and allows using pylint. It's engaged on open and save.
# MRU gives an LRU cache of recently-edited filenames.
#
# Tested on Debian 7.8, Linux Mint 17 and OS/X 10.10.2 .
# Didn't work at all on AMI Linux, even though it has vim.  I'm not sure why it didn't work.
#
# On OS/X, building vim with +conceal and +python was necessary: ./configure --with-features=big --enable-pythoninterp
# On the Linuxes, no compilation was required.
######################################################################

if type gtar > /dev/null 2>&1
then
    tar=gtar
else
    tar=tar
fi

find='find'
for candidate_find in /usr/bin/find /bin/find /usr/bin/find.exe /bin/find.exe
do
    if [ -f "$candidate_find" ]
    then
        find="$candidate_find"
        break
    fi
done

function opsys
{
	uname -s | tr '[:upper:]' '[:lower:]'
}

case "$(opsys)" in
	msys*)
		# shellcheck disable=SC2016
		hohm=$(powershell 'echo $HOME')
		vimrc=_vimrc
		;;
	*)
		hohm=$HOME
		vimrc=.vimrc
		;;
esac

# Otherwise python-mode might get kind of surreal
rm -rf "$hohm"/.vim

# BTW, if you have a vim setting that's not what you want (EG Cylance tabstops being 8 instead of 4), you can:
# :verbose set tabstop?
# ...to see where it's getting set.

# Copy files
case "$(hostname)" in
	xDS-RE-C02R23TQG8WM.tld)
		syntastic="-o -name syntastic"
		;;
	DS-RE-C02R23TQG8WM.tld)
		# Another thing macOS doesn't like
		syntastic=""
		;;
	dstromberg-linux)
		# syntastic="-o -name syntastic"
		syntastic=""
		;;
	*)
		syntastic=""
		;;
esac

do_ruby=True
case "$(ruby --version)" in
	*2.0.0p648*)
		do_ruby=False
		;;
esac

(
	set -eu
	cd hierarchy
	# shellcheck disable=SC2086
	$find . \
		\( \
			-name .svn \
			$syntastic \
		\) -prune -o \( -type f -o -type l \) -print | \
		$tar --create --no-recursion --file - --files-from - | \
		(cd "$hohm" && "$tar" xvfp -) || true
)

# Remove something I didn't like.  auto-pairs is a nice idea, but it messes up n.n.n.
rm -rf "$hohm"/.vim/bundle/auto-pairs

max_column=133

# DS-RE-C02R23TQG8WM.tld is my Cylance Mac.  It's likely DHCP.
case "$(hostname)" in
	dstromberg-linux|DS-RE-C02R23TQG8WM.tld)
		# Cylance uses hard tabs
		echo Doing cylance fixing 1>&2
		sed -i.bak 's/\<expandtab\>/noexpandtab/g' "$hohm"/"$vimrc"
		# Cylance uses a maximum column of 120
        max_column=120
        for candidate_path in \
			/usr/local/Cellar/vim/7.4.*/share/vim/vim74/ftplugin/python.vim \
            /usr/share/vim/vim74/ftplugin/python.vim \
            /usr/share/vim/vim73/ftplugin/python.vim
        do
            if [ -f "$candidate_path" ]
            then
				# setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
				sudo sed \
					-i.bak \
					-e 's/ tabstop=[0-9][0-9]*/ tabstop=4/g' \
					-e 's/ softtabstop=[0-9][0-9]*/ softtabstop=4/g' \
					-e 's/ expandtab/ noexpandtab/g' \
					"$candidate_path"
            fi
        done
        # This has an effect on macOS, and doesn't hurt anyway.
        # macOS sed doesn't appear to understand \< and \> !
		# brew provides gsed though - in the future we might use that.
        sed -i.bak -e 's/^\(set  *\)tabstop=[0-9]$/\1tabstop=4/g' "$hohm"/"$vimrc"
        sed -i.bak -e 's/^\(set  *\)expandtab$/\1noexpandtab/g' "$hohm"/"$vimrc"
        # set   expandtab
		;;
esac

# This was uname -o, but that was given an error on macOS.  I'm not sure the Msys below is still going to work.
case "$(opsys)" in
    linux|darwin)
        # Awesome. Should "just work".
        ;;
    msys*)
        # Windows. Sigh.
        # Assumes a native vim.
        # Based on https://n3wjack.net/2014/08/25/setting-up-vim-on-windows/
        mv "$hohm"/.vimrc "$hohm"/_vimrc
        rm -rf "$hohm"/vimfiles
        mv "$hohm"/.vim "$hohm"/vimfiles
        # Might also have to remove (from vimrc) the OCaML support - or install OCaML.
        # Likely need to manually pip install (and choco?) some packages.
        ;;
    *)
        echo "$0: Unrecognized OS: $(uname -o).  Treating it like GNU/Linux" 1>&2
        ;;
esac

# Set our own maximum line length.
sed -i.bak 's/^autocmd FileType python set colorcolumn=.*$/autocmd FileType python set colorcolumn='"$max_column"/ "$hohm"/"$vimrc"

# We need ruby for sqlint.
./install-package \
    --deb-packages "python3-pip python3-setuptools ruby ruby-dev build-essential" \
    --rpm-packages "python3-pip lua-devel python3-setuptools ruby ruby-devel epel-release"
if ! ./install-package --deb-packages "vim-gtk3" --rpm-packages "vim-X11 vim-enhanced epel-release" --brew-packages "vim"
then
    # Debian 8.10 does not have a vim-gtk3, so we fall back to vim-gtk
    ./install-package --deb-packages "vim-gtk" --rpm-packages "vim-X11 vim-enhanced epel-release"
fi

case "$do_ruby" in
	True)
		./install-via-gem \
			--gem-path gem \
			--ruby-path ruby \
			--module-to-import sqlint \
			--gem-package sqlint
		;;
	False)
		;;
	*)
		echo "$0: internal error: \$do_ruby has a strange value: $do_ruby" 1>&2
		exit 1
		;;
esac

./os-packages

echo "Note: If you want ecmascript support, run ./os-packages --expensive" 1>&2