#!/usr/bin/env bash

set -e

CONFIG="install.conf.yaml"
DOTBOT_DIR="dotbot"

DOTBOT_BIN="bin/dotbot"
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

cd "${BASEDIR}"
git -C "${DOTBOT_DIR}" submodule sync --quiet --recursive
git submodule update --init --recursive

"${BASEDIR}/${DOTBOT_DIR}/${DOTBOT_BIN}" -d "${BASEDIR}" -c "${CONFIG}" "${@}"

# Own Stuff
platform="$(uname -s)"

function uninstalled() {
	if which $1 >/dev/null; then
		return 1
	else
		return 0
	fi
}

# PNPM
if uninstalled pnpm; then
	curl -fsSL https://get.pnpm.io/install.sh | sh -
fi

# macOS
if [[ $platform == "Darwin" ]]; then
	# Brew
	if uninstalled brew; then
		/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
	fi
	brew bundle --no-lock --file ./files/Brewfile

	# iTerm2
	defaults write com.googlecode.iterm2.plist PrefsCustomFolder -string "~/.dotfiles/files/iterm2"
	defaults write com.googlecode.iterm2.plist LoadPrefsFromCustomFolder -bool true
fi

# NeoVim & Vim Plugins https://github.com/junegunn/vim-plug
for dir in "$HOME/.vim/autoload/plug.vim" "${XDG_DATA_HOME:-$HOME/.local/share}/nvim/site/autoload/plug.vim"
do
	if [[ ! -f $dir ]]
	then
		echo "Installing Plug to: $dir"
		curl -fLo "$dir" --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
	else
		echo "Plug already installed at: $dir"
	fi
done