#!/bin/bash

## This script takes care of the i3 configuration

if [ -z "$BASE_DIR" ]; then
  echo "BASE_DIR wasn't set."
  exit 1
fi
if [ -z "$GITHUB_DOTFILES" ]; then
  echo "GITHUB_DOTFILES wasn't set."
  exit 1
fi

# Install required packages {{{
######
# OK #
######

if [ "$OS" = 'arch' ]; then
  pacman -Qe i3-gaps || sudo pacman -S --needed i3
  sudo pacman -S --needed dunst suckless-tools
  sudo pacman -S --needed picom hsetroot rofi xsettingsd lxappearance scrot viewnior
  sudo pacman -S --needed noto-fonts
elif [ "$OS" = 'debian' ]; then
  sudo apt install i3
  sudo apt install dunst suckless-tools
  sudo apt install compton hsetroot rofi xsettingsd lxappearance scrot viewnior
  sudo apt install fonts-noto
else
  echo "${RED}The OS wasn't defined. Quitting...${NC}"
  exit 1
fi

# }}}

# Add clones from GitHub {{{
######
# OK #
######

mkdir -p "$GITHUB_DOTFILES"

if [ -e "$GITHUB_DOTFILES"/i3-starterpack ]; then
  if [ "$UPDATE" = 'true' ]; then
    cd "$GITHUB_DOTFILES"/i3-starterpack && git pull
  fi
else
  cd "$GITHUB_DOTFILES" && git clone https://github.com/addy-dclxvi/i3-starterpack.git
fi

if [ -e "$GITHUB_DOTFILES"/i3blocks-contrib ]; then
  if [ "$UPDATE" = 'true' ]; then
    cd "$GITHUB_DOTFILES"/i3blocks-contrib && git pull
  fi
else
  cd "$GITHUB_DOTFILES" && git clone https://github.com/vivien/i3blocks-contrib.git
fi

if [ -e "$GITHUB_DOTFILES"/i3blocks-cryptocurrency ]; then
  if [ "$UPDATE" = 'true' ]; then
    cd "$GITHUB_DOTFILES"/i3blocks-cryptocurrency && git pull
  fi
else
  cd "$GITHUB_DOTFILES" && git clone https://github.com/ryanwalder/i3blocks-cryptocurrency.git
fi

# }}}

# Add config links {{{
######
# OK #
######

echo "Adding .Xresources and .xsettingsd"
make_symlink ~/.Xresources "$GITHUB_DOTFILES"/i3-starterpack/.Xresources
make_symlink ~/.xsettingsd "$GITHUB_DOTFILES"/i3-starterpack/.xsettingsd

echo "Adding picom.config"
mkdir -p ~/.config
make_symlink ~/.config/picom.conf "$BASE_DIR"/dotfiles/picom.conf
make_symlink ~/.config/compton.conf "$BASE_DIR"/dotfiles/picom.conf

echo "Adding feather.ttf"
mkdir -p ~/.fonts/icomoon
make_symlink ~/.fonts/icomoon/feather.ttf "$GITHUB_DOTFILES"/i3-starterpack/.fonts/icomoon/feather.ttf

# }}}

# Add i3 main config {{{
######
# OK #
######

echo "Adding i3/config"
mkdir -p ~/.config/i3
make_symlink ~/.config/i3/config "$BASE_DIR"/dotfiles/i3/'config'

# }}}

# Add i3status config {{{
######
# OK #
######

echo "Adding i3status/config"
mkdir -p ~/.config/i3status
make_symlink ~/.config/i3status/config "$BASE_DIR"/dotfiles/i3/'status'

# }}}

# Add i3blocks config {{{
######
# OK #
######

echo "Adding i3blocks/config"
mkdir -p ~/.config/i3blocks
make_symlink ~/.config/i3blocks/config "$BASE_DIR"/dotfiles/i3/'blocks'

echo "Adding i3blocks scripts"
mkdir -p ~/.config/i3blocks/scripts
make_symlink ~/.config/i3blocks/scripts/arch-update "$GITHUB_DOTFILES"/i3blocks-contrib/arch-update/arch-update
make_symlink ~/.config/i3blocks/scripts/ccurrency "$GITHUB_DOTFILES"/i3blocks-cryptocurrency/ccurrency
make_symlink ~/.config/i3blocks/scripts/rofi-calendar "$GITHUB_DOTFILES"/i3blocks-contrib/rofi-calendar/rofi-calendar

# }}}
