#!/usr/bin/env bash

platform=`uname`

if [[ $platform == 'Darwin' ]]; then
    if ! command -v brew &>/dev/null
    then
        # We need to update bash, so we'll start with setting up homebrew.
        ./osx/pre_install
        exit
    fi
fi

source "script_helpers/printing.sh"
source "script_helpers/core.sh"
source "script_helpers/platform.sh"
source "script_helpers/file_ops.sh"

cwd=$PWD
confirm_link=0
os_is_windows is_windows
os_is_macos is_macos
os_is_linux is_linux

if [[ $is_windows -eq 1 ]]; then
    # Check for admin permissions.
    net session &>/dev/null
    admin_error=$?
    if [[ ! $admin_error -eq 0 ]]; then
        fatal "You need to run this in an admin shell!"
    fi
fi

set -e

####################################################################################################
# Helpers
####################################################################################################

setup_zsh() {
    printf "Setting up zsh...\n"

    if [[ $is_linux -eq 1 ]]; then
        sudo apt install zsh
    fi

    TEST_CURRENT_SHELL=$(expr "$SHELL" : '.*/\(.*\)')
    if [ "$TEST_CURRENT_SHELL" != "zsh" ]; then
        if hash chsh >/dev/null 2>&1; then
            printf "\n${BLUE}Changing the default shell to zsh${NORMAL}\n"
            chsh -s $(grep /zsh$ /etc/shells | tail -1)
        else
            printf "\n${RED}Unable to change the shell because this system does not have chsh.\n"
            printf "${BLUE}If this is Windows then you probably want to run the bash installer.${NORMAL}\n"
        fi
    fi

    setup_dir .dotfiles/zsh .zsh

    FILES=()
    FILES+=('zshrc')
    FILES+=('zshenv')
    FILES+=('zlogin')

    for file in "${FILES[@]}"
    do
        setup_file .dotfiles/$file .$file
    done
}

####################################################################################################
# Setup
####################################################################################################

pushd "$HOME" &>/dev/null

#########################
# Setup root dirs
#########################
setup_dir $cwd .dotfiles
setup_dir .dotfiles/vim .vim

#########################
# Setup root files
#########################
FILES=()
FILES+=('aliases')
FILES+=('gitconfig')
FILES+=('vimrc')
FILES+=('curlrc')

for file in "${FILES[@]}"
do
    setup_file .dotfiles/$file .$file
done

#########################
# Setup non-root files
#########################
if [[ $is_windows -eq 1 ]]; then
    setup_file .dotfiles/bash/bashrc .bashrc
    setup_file .dotfiles/bash/bash_profile .bash_profile
    setup_file .dotfiles/bash/inputrc .inputrc
    setup_file .dotfiles/windows/gitconfig.platform .gitconfig.platform
fi

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o .git-completion.bash

if [[ $is_windows -eq 0 ]]; then
    setup_zsh
fi

if [[ $is_macos -eq 1 ]]; then
    printf "\n${BOLD}Running the MacOS installer${NORMAL}\n"
    $cwd/osx/install
fi

popd "$HOME" &>/dev/null

printf "${BOLD}${GREEN}Done!${NORMAL}\n"