#!/bin/bash

### Go to the script path
set -e
cd -- "$(dirname -- "$0")"
cd ..
[[ -f "./scripts/functions" ]] || { printf "\e[1;31m==> Error : \e[0mFunctions not found"; exit 1; }
source ./scripts/functions

### Global variables
_DIRNAME="$(pwd)"
_PACKAGES=(arc-gtk-theme chromium code curl discord dmenu dunst egl-wayland feh flameshot flatpak firefox htop hyprland i3lock i3-wm imagemagick lxappearance mpv nwg-look otf-comicshanns-nerd pavucontrol pipewire pipewire-jack pipewire-pulse polybar neofetch numlockx papirus-icon-theme picom plocate polybar remmina ttf-dejavu-nerd wget wireguard-tools xdg-desktop-portal xorg xorg-xinit)

### Configure pacman and install needeed packages
_info "Configuring and updateing packages"
_msg "Configuring pacman"
sed -i "s|^#Color.*|Color|" /etc/pacman.conf
sed -i "s|^#ParallelDownloads.*|ParallelDownloads = 10|" /etc/pacman.conf
_msg "Updating packages"
pacman -Syu --noconfirm
_msg "Installing needeed packages"
pacman -S --noconfirm --needed "${_PACKAGES[@]}"
flatpak install com.github.IsmaelMartinez.teams_for_linux
flatpak -y update

### Template files
_info "templating config files"
_TEMPLATES=(X11/.Xresources dunst/dunstrc polybar/colors.ini rofi/colors.rasi)
mkdir -p links/X11 links/dunst links/polybar links/rofi
for template in "${_TEMPLATES[@]}"; do
  echo "$template";
  cp "./templates/$template" "./links/$template"
  _template "./links/$template"
done

### Linking config files
_info "Linking config files"
_msg "modprobe links"
ln -sf "${_DIRNAME}/links/modprobe/blacklist.conf" /etc/modprobe.d/blacklist.conf
_msg "tty links"
mkdir -p /etc/systemd/system/getty@tty1.service.d
ln -sf "$_DIRNAME/links/tty/override.conf" /etc/systemd/system/getty@tty1.service.d/override.conf
ln -sf "$_DIRNAME/links/tty/issue" /etc/issue
ln -sf "$_DIRNAME/links/tty/inputrc" /etc/inputrc
ln -sf "$_DIRNAME/links/tty/bash.bashrc" /etc/bash.bashrc
ln -sf "$_DIRNAME/links/tty/bash.bashaliases" /etc/bash.bashaliases
ln -sf "$_DIRNAME/links/tty/bash.bashexports" /etc/bash.bashexports
_msg "links pipewire"
mkdir -p ~/.config/systemd/user/pipewire-pulse.service.d ~/.config/systemd/user/pipewire-pulse.socket.d
ln -sf "$_DIRNAME/links/pipewire/override.conf" ~/.config/systemd/user/pipewire-pulse.service.d/override.conf
ln -sf "$_DIRNAME/links/pipewire/override.conf" ~/.config/systemd/user/pipewire-pulse.socket.d/override.conf
_msg "X11 links"
ln -sf "$_DIRNAME/links/X11/.Xresources" /etc/X11/xinit/.Xresources
ln -sf "$_DIRNAME/links/X11/xinitrc" /etc/X11/xinit/xinitrc
ln -sf "$_DIRNAME/links/X11/00-keyboard.conf" /etc/X11/xorg.conf.d/00-keyboard.conf
_msg "dunst links"
ln -sf "$_DIRNAME/links/dunst/dunstrc" /etc/dunst/dunstrc
_msg "i3 links"
ln -sf "$_DIRNAME/links/i3wm/config" /etc/i3/config
ln -sf "$_DIRNAME/links/i3wm/home" /etc/i3/home
ln -sf "$_DIRNAME/links/i3wm/work" /etc/i3/work
_msg "polybar links"
ln -sf "$_DIRNAME/links/polybar/config.ini" /etc/polybar/config.ini
ln -sf "$_DIRNAME/links/polybar/colors.ini" /etc/polybar/colors.ini
ln -sf "$_DIRNAME/links/polybar/home.ini" /etc/polybar/home.ini
ln -sf "$_DIRNAME/links/polybar/work.ini" /etc/polybar/work.ini
_msg "rofi links"
mkdir -p ~/.config/rofi ~/.local/share/rofi/themes
ln -sf "$_DIRNAME/links/rofi/config.rasi" ~/.config/rofi/config.rasi
ln -sf "$_DIRNAME/links/rofi/colors.rasi" ~/.local/share/rofi/themes/colors.rasi
ln -sf "$_DIRNAME/links/rofi/launcher.rasi" ~/.local/share/rofi/themes/launcher.rasi
ln -sf "$_DIRNAME/links/rofi/disconnect.rasi" ~/.local/share/rofi/themes/disconnect.rasi
_msg "picom links"
ln -sf "$_DIRNAME/links/picom/picom.conf" /etc/xdg/picom.conf
_msg "apps links"
mkdir -p ~/.local/share/applications
ln -sf "$_DIRNAME/links/applications/teams.desktop" ~/.local/share/applications/teams.desktop
sed -i "s|^Exec=.*|Exec=code-oss --no-sandbox|" /usr/share/applications/code-oss.desktop
sed -i "s|^Icon=.*|Icon=vscode|" /usr/share/applications/code-oss.desktop
_msg "links kitty"
mkdir -p ~/.config/kitty
ln -sf "$_DIRNAME/links/kitty/kitty.conf" ~/.config/kitty/kitty.conf
ln -sf "$_DIRNAME/links/kitty/current-theme.conf" ~/.config/kitty/current-theme.conf

### Qwerty fr
cp /usr/share/X11/xkb/symbols/us /usr/share/X11/xkb/symbols/us_qwerty-fr
curl https://raw.githubusercontent.com/qwerty-fr/qwerty-fr/master/linux/us_qwerty-fr >> /usr/share/X11/xkb/symbols/us_qwerty-fr

if [[ $_LOCATION == "work" ]]
then
  _info "Configuring for work"
  sed -i "s|^include /etc/i3.*|include /etc/i3/work|" ./links/i3wm/config
  sed -i "s|^include-file = /etc/polybar/home.ini|include-file = /etc/polybar/work.ini|" ./links/polybar/config.ini
fi
if [[ $_LOCATION == "home" ]]
then
  _info "Configuring for work"
  sed -i "s|^include /etc/i3.*|include /etc/i3/home|" ./links/i3wm/config
  sed -i "s|^include-file = /etc/polybar/work.ini|include-file = /etc/polybar/home.ini|" ./links/polybar/config.ini
fi

### nvidia vs amd

### Print kernel cmdline

### Wait for user
_info "Update sucessfull"
read -r
