#!/bin/bash

# Vars
DOTFILES_DIR="$HOME/.dotfiles"
CONFIG_DIR="$HOME/.config/dotfiles"
REPO="https://gitea.keuledrive.de/Keule2/Dotfiles.git"
DIST=$(source /etc/os-release && echo $ID)

# Colors
COLOR_RESET='\033[0m'
RED='\033[0;31m'
LGREEN='\033[01;32m'

# Helper Functions
print_header() {
  echo -e "${LGREEN}$1${COLOR_RESET}"
}

cmd() {
  local DOTFILES_LOG="./error.log"

  if eval "$1" 1> /dev/null 2> $DOTFILES_LOG; then
    rm "$DOTFILES_LOG"
    return 0
  fi

  echo -e "${RED}"
  cat $DOTFILES_LOG
  echo -e "${COLOR_RESET}"
  rm "$DOTFILES_LOG"
  exit 1
}

paru_install() {
  if [ -x "$(command -v paru)" ]; then
    return
  fi

  # Install Base-Devel
  if [ -z "$(pacman -Q | grep base-devel)" ]; then
    print_header "Installing base-devel"
    cmd "sudo pacman -S base-devel --noconfirm"
  fi
  # Install RustUp
  if ! [ -x "$(command -v rustup)" ]; then
    print_header "Installing rustup"
    cmd "sudo pacman -S rustup --noconfirm"
    cmd "rustup default stable"
  fi
  # Instal Paru
  FOLDER=$(mktemp -d)
  print_header "Installing paru"
  cmd "git clone https://aur.archlinux.org/paru.git $FOLDER"
  cd $FOLDER
  cmd "makepkg -si --noconfirm"
}

arch_install() {
  # Install Ansible
  if ! [ -x "$(command -v ansible)" ]; then
    print_header "Installing ansible"
    cmd "sudo pacman -S ansible --noconfirm"
  fi
  # Install Git 
  if ! [ -x "$(command -v git)" ]; then
    print_header "Installing git"
    cmd "sudo pacman -S git --noconfirm"
  fi
  # Installl Paru
  paru_install
  # Install PIP
  if ! [ -x "$(command -v pip)" ]; then
    print_header "Installing pip"
    cmd "sudo pacman -S python-pip --noconfirm"
  fi
  # Install Watchdog
  if [ -z "$(pip list | grep watchdog)" ]; then
    print_header "Installing watchdog"
    cmd "pip install --break-system-packages watchdog"
  fi
}

ubuntu_install() {
  # Install Ansible
  if ! dpkg -s ansible >/dev/null 2>&1; then
    print_header "Installing ansible"
    cmd "sudo apt-get update"
    cmd "sudo apt-get install -y software-properties-common"
    cmd "sudo apt-add-repository -y ppa:ansible/ansible"
    cmd "sudo apt-get update"
    cmd "sudo apt-get install -y ansible"
    cmd "sudo apt-get install python3-argcomplete"
    cmd "sudo activate-global-python-argcomplete3"
  fi
  # Install Git
  if ! dpkg -s git >/dev/null 2>&1; then
    print_header "Installing git"
    cmd "sudo apt-get install -y git"
  fi
  # TODO pip, watchdog
}

update_galaxy() {
  local OS=$1

  print_header "Installing/Updating ansible-galaxy"
  cmd "ansible-galaxy install -r $DOTFILES_DIR/requirements/default.yml"
  if [ -f "$DOTFILES_DIR/requirements/$OS.yml" ]; then
    cmd "ansible-galaxy install -r $DOTFILES_DIR/requirements/$OS.yml"
  fi
}

# Banner
cat << "EOF"
________          __           .___                 __         .__  .__                
\______ \   _____/  |_  ______ |   | ____   _______/  |______  |  | |  |   ___________ 
 |    |  \ /  _ \   __\/  ___/ |   |/    \ /  ___/\   __\__  \ |  | |  | _/ __ \_  __ \
 |    `   (  <_> )  |  \___ \  |   |   |  \\___ \  |  |  / __ \|  |_|  |_\  ___/|  | \/
/_______  /\____/|__| /____  > |___|___|  /____  > |__| (____  /____/____/\___  >__|   
        \/                 \/           \/     \/            \/               \/       
EOF

# Setup
case $DIST in
  arch)
      arch_install
      ;;
  ubuntu)
      ubuntu_install
      ;;
  *)
      echo "Unsupported OS"
      exit 1
      ;;
esac

# Clone repository
if ! [[ -d "$DOTFILES_DIR" ]]; then
  print_header "Cloning repo"
  cmd "git clone $REPO $DOTFILES_DIR"
else
  print_header "Updating repo"
  cmd "git -C $DOTFILES_DIR pull"
fi

# Change Working Dir
cd "$DOTFILES_DIR"

# Update Galaxy
update_galaxy $DIST

# Run playbook
if [[ -f "$CONFIG_DIR/vault-password.txt" ]]; then
  if [[ -f "$CONFIG_DIR/values.yml" ]]; then
    ansible-playbook --diff -v --ask-become-pass --extra-vars "@$CONFIG_DIR/values.yml" --vault-password-file "$CONFIG_DIR/vault-password.txt" "$DOTFILES_DIR/main.yml" "$@"
  else
    ansible-playbook --diff -v --ask-become-pass --vault-password-file "$CONFIG_DIR/vault-password.txt" "$DOTFILES_DIR/main.yml" "$@"
  fi
elif [[ -f "$CONFIG_DIR/values.yml" ]]; then
  ansible-playbook --diff -v --ask-become-pass --extra-vars "@$CONFIG_DIR/values.yml" "$DOTFILES_DIR/main.yml" "$@"
else
  ansible-playbook --diff -v --ask-become-pass "$DOTFILES_DIR/main.yml" "$@"
fi
