#!/usr/bin/env -S bash -e

rm -rf "$HOME/.vim" "$HOME/.vimrc"
src_dir="$(cd "$(dirname "$0")" && pwd)"
ln -rsv "$src_dir/.vimrc" "$HOME"
dest_start_dir=$HOME/.vim/pack/$USER/start
mkdir -p "$dest_start_dir"
src_start_dir=$src_dir/start
for src in "$src_start_dir/"*
do
    src_basename="$(basename "$src")"
    if [[ $src_basename = you-complete-me ]]
    then
        unset skip
        while ! [[ -v skip ]]
        do
            read -p "Install YouCompleteMe [y/N]?  " answer
            case $answer in
            Y|y)
                python3 "$src/install.py" --clang-completer
                skip=0
                ;;
            ''|N|n)
                skip=1
            esac
        done
        if (( skip ))
        then
            continue
        fi
    fi
    ln -rsv "$src" "$dest_start_dir"
done
echo "Installed to $HOME"
if [[ $USER != root ]]
then
    root_home_dir=~root
    while true
    do
        read -p "Install to $root_home_dir [y/N]?  " answer
        case $answer in
        Y|y)
            sudo rm -rf "$root_home_dir/.vim" "$root_home_dir/.vimrc"
            sudo ln -sv "$src_dir/.vimrc" "$root_home_dir"
            dest_start_dir=$root_home_dir/.vim/pack/$USER/start
            sudo mkdir -p "$dest_start_dir"
            for src in "$src_start_dir/"*
            do
                src_basename="$(basename "$src")"
                if [[ $src_basename != you-complete-me ]]
                then
                    sudo ln -sv "$src" "$dest_start_dir"
                fi
            done
            break
            ;;
        ''|N|n)
            break
        esac
    done
fi
