#!/bin/bash

set -e

CONFIG_DIR="$HOME/.config/dotfiles"
DOTFILES_DIR="$HOME/.dotfiles"
PATH="${PATH}:${HOME}/.local/bin"

clone_or_update_dotfiles() {
  if ! [[ -d "$DOTFILES_DIR" ]]; then
    git clone "https://github.com/opdavies/dotfiles.git" "$DOTFILES_DIR"
  else
    git -C "$DOTFILES_DIR" pull
  fi
}

create_config_dir() {
  mkdir -p "${CONFIG_DIR}"
}

install_dependencies() {
  ansible-galaxy install -r "${DOTFILES_DIR}/requirements.yml"
}

install_ansible() {
  sudo apt-get -yqq update 
  sudo apt-get -yqq install python3-pip
  pip install ansible --user
}

run_playbook() {
  ansible-playbook --diff --extra-vars "@${CONFIG_DIR}/values.yaml" --vault-password-file=${CONFIG_DIR}/vault-password.txt "${DOTFILES_DIR}/main.yaml" "${@}"
}

clone_or_update_dotfiles
install_ansible
install_dependencies
run_playbook "${@}"