#!/bin/zsh
#
# YADM Bootstrap
# @CharlesDanesi

case $(uname -s) in
   Darwin)

      # Install Homebrew
      if ! command -v brew >/dev/null 2>&1; then
         echo "Installing Homebrew"
         /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      fi

      if [ -f "$HOME/.Brewfile" ]; then
         echo "Installing/Updating homebrew bundle"
         brew bundle --global
      fi

      # espanso
      espanso service register
      espanso service start
      ;;

   Linux)

      # First, we need to distinguish between Arch and Debian/Ubuntu
      # I'm not sure the best way to do this yet, but as I primarily use Arch and
      # Debian based distros, I'm going to separate them by package manager..

      if command -v pacman >/dev/null 2>&1; then # Arch-based

         # Add BlackArch repositories
         if [ ! -f /etc/pacman.d/blackarch-mirrorlist ]; then
            echo "Installing BlackArch repositories"
            curl -O https://blackarch.org/strap.sh && sudo sh ./strap.sh
         fi

      elif [ ! command -v apt >/dev/null 2>&1 ]; then # Debian-based

         echo "nothing to do yet"

      else

         echo "nothing to do yet"

      fi
      ;;

   *) ;;
esac

echo "Setting up ZSH"
if ! [ -d "$HOME/.zsh" ]; then
   mkdir "$HOME/.zsh"
fi

if ! [ -d "$HOME/.zsh/zsh-autosuggestions" ]; then
   echo "Installing zsh-autosuggestions"
   git clone https://github.com/zsh-users/zsh-autosuggestions $HOME/.zsh/zsh-autosuggestions
fi

if ! [ -d "$HOME/.zsh/zsh-syntax-highlighting" ]; then
   echo "Installing zsh-syntax-highlighting"
   git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $HOME/.zsh/zsh-syntax-highlighting
fi

if ! [ -d "$HOME/.zsh/zsh-completions" ]; then
   echo "Installing zsh-completions"
   git clone https://github.com/zsh-users/zsh-completions.git $HOME/.zsh/zsh-completions
   [[ ! -f $HOME/.zcompdump ]] || rm $HOME/.zcompdump
   echo "Setting up zsh completions"
   compinit
fi

if ! [ -d "$HOME/.zsh/zsh-vi-mode" ]; then
   echo "Installing zsh-vi-mode"
   git clone https://github.com/jeffreytse/zsh-vi-mode.git $HOME/.zsh/zsh-vi-mode
fi

if ! [ -d "$HOME/.zsh/powerlevel10k" ]; then
   echo "Installing powerlevel10k"
   git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $HOME/.zsh/powerlevel10k
fi

if ! [ -d "$HOME/.oh-my-zsh/" ]; then
   echo "Installing oh-my-zsh"
   sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
   rm "$HOME/.zshrc"
   mv "$HOME/.zshrc.pre-oh-my-zsh" "$HOME/.zshrc"
   source "$HOME/.zshrc"
fi

read -qs "ans?Change yadm origin remote to SSH url? (y/N) "
case $ans in
   y)
      echo "\n\nSetting remote origin to use SSH"
      yadm remote set-url origin "git@github.com:cdanesi/dotfiles.git"
      ;;
   *) ;;
esac

if ! [ -d "$HOME/.config/ranger/plugins/ranger_devicons" ]; then
   echo "Installing ranger_devicons"
   git clone https://github.com/alexanderjeurissen/ranger_devicons $HOME/.config/ranger/plugins/ranger_devicons
fi

if ! [ -d "$HOME/.config/tmux/plugins/tpm/" ]; then
   echo "Installing Tmux Plugin Manager (TPM)"
   mkdir -p "$HOME/.config/tmux/plugins/tpm"
   git clone https://github.com/tmux-plugins/tpm $HOME/.config/tmux/plugins/tpm
fi