#!/bin/bash

# ------------------------------------------------------
# --- SETTINGS
# ------------------------------------------------------
home_directory="$HOME"
config_directory="$HOME/.config"
fonts_directory="/usr/share/fonts"
scripts_directory="/usr/local/bin"

green='\033[0;32m'
magenta='\x1b[38;5;206m'
normal='\033[0m'
bold='\033[1m'
no_color='\033[0m'
date=$(date +%s)

# ------------------------------------------------------
# --- INSTALL DIALOG
# ------------------------------------------------------
sudo pacman --noconfirm --needed -Sy dialog

# ------------------------------------------------------
# --- 1 SYSTEM UPDATE
# ------------------------------------------------------
system_update() {
  echo -e "${green}${bold}[*] DOING A SYSTEM UPDATE, CAUSE STUFF MAY BREAK IF IT'S NOT THE LATEST VERSION...${normal}${no_color}"
  echo
  sleep 3

  sudo pacman -Sy --noconfirm archlinux-keyring
  sudo pacman --noconfirm -Syu
  sudo pacman -S --noconfirm --needed base-devel wget git curl

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- 2 INSTALL AUR HELPER
# ------------------------------------------------------
install_aur_helper(){
  echo -e "${green}${bold}[*] INSTALLING AUR HELPER...${normal}${no_color}"
  echo
  sleep 3

  cd $HOME
  git clone https://aur.archlinux.org/yay.git .yay
  cd $HOME/.yay
  makepkg -si
  cd $HOME

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- 3 INSTALL XORG
# ------------------------------------------------------
install_xorg(){
  echo -e "${green}${bold}[*] INSTALLING XORG...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'xorg'
    'xorg-server'
    'xorg-apps'
    'xorg-xinit'
    'xf86-video-intel'
    'mesa'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL PACMAN PACKAGES
# ------------------------------------------------------
install_pkgs(){
  echo -e "${green}${bold}[*] INSTALLING PACMAN PACKAGES...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'acpi'            # ACPI (Advanced Configuration and Power Interface)
    'pacman-contrib'  # Contributed scripts and tools for pacman systems
    # 'base-devel'
    # 'curl'
    # 'git'
    # 'wget'

    # TERMINAL EMULATOR ---------------------

    'alacritty'
    'kitty'

    # TERMINAL UTILITES ---------------------

    'htop'
    'neovim'
    'neofetch'
    'feh'
    'dunst'
    'scrot'
    'xclip'
    'fish'
    'file-roller'
    'sbxkb'
    'gufw'
    'hardinfo'
    'inxi'
    'jq'
    'jshon'
    'ntp'
    'numlockx'
    'openssh'
    'rsync'
    'tlp'
    'pass'
    'ripgrep'
    'picom'
    'yt-dlp'
    'tmux'
    'fzf'
    'man-db'
    'cmatrix'
    'ncdu'
    'calcurse'
    'unclutter'
    'bat'
    'dosfstools'
    'brightnessctl'
    'lazygit'
    'exa'
    # 'picom'

    # MEDIA ---------------------

    'mpd'
    'ncmpcpp'
    'mpv'
    'cmus'
    'ffmpeg'

    # PRODUCTIVITY ---------------------

    'galculator'
    'zathura'
    'zathura-pdf-mupdf'
    'obsidian'

    # FILEMANAGER ---------------------

    'nemo'
    'ranger'
    'mc'
    'thunar'
    'thunar-archive-plugin'
    'thunar-volman'

    # WEB TOOLS ---------------------

    'firefox'

    # ARCHIVE ---------------------

    'unrar'
    'unzip'
    'zip'
    'p7zip'

    # DISK UTILITIES ---------------------

    'autofs'
    'exfat-utils'
    'gparted'
    'gnome-disks'
    'ntfs-3g'
    'parted'
    'gvfs'
    'gvfs-mtp'
    'gvfs-afc'
    'gvfs-gphoto2'
    'gvfs-nfs'
    'gvfs-smb'
    'xdg-utils'
    'xdg-user-dirs-gtk'

    # GENERAL UTILITIES ---------------------

    'veracrypt' # Disc encryption utility
    'keepassxc' # Pass manager
    'catfish'   # Filesystem search

  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL DEVELOPMENT
# ------------------------------------------------------
install_development(){
  echo -e "${green}${bold}[*] INSTALLING DEVELOPMENT...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'nodejs'
    'yarn'
    'pyenv'
    'code'
    'filezilla'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL GRAPHICS AND DESIGN
# ------------------------------------------------------
install_graphics(){
  echo -e "${green}${bold}[*] INSTALLING GRAPHICS AND DESIGN...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    # 'gcolor2'
    'gcolor3'
    'gimp'
    'inkscape'
    'imagemagick'
    # 'nomacs'
    'pngcrush'
    # 'ristretto'
    'sxiv'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL FONTS
# ------------------------------------------------------
install_fonts(){
  echo -e "${green}${bold}[*] INSTALLING FONTS...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'terminus-font'
    'adobe-source-code-pro-fonts'
    'nerd-fonts'
    'noto-fonts'
    'noto-fonts-emoji'
    'noto-fonts-cjk'
    'ttf-jetbrains-mono'
    'ttf-joypixels'
    'ttf-font-awesome'
    'ttf-hack'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL AUDIO
# ------------------------------------------------------
install_audio(){
  echo -e "${green}${bold}[*] INSTALLING AUDIO...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'alsa-utils'
    'alsa-plugins'
    'pulseaudio'
    'pulseaudio-alsa'
    'pavucontrol'     # pulseaudio volume control
    'volumeicon'      # tray volume icon
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL PRINTERS & SACANNERS
# ------------------------------------------------------
install_printers_scanners(){
  echo -e "${green}${bold}[*] INSTALLING PRINTERS & SACANNERS...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'cups'
    'cups-pdf'
    'ghostscript'
    'gsfonts'
    'system-config-printer'
    'skanlite'
    'simple-scan'
    'sane'
    'sane-airscan'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  sudo systemctl enable org.cups.cupsd.service
  sudo systemctl start org.cups.cupsd.service
  sudo systemctl enable --now cups

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL BLUETOOTH
# ------------------------------------------------------
install_bluetooth(){
  echo -e "${green}${bold}[*] INSTALLING BLUETOOTH...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'bluez'
    'bluez-utils'
    'bluez-firmware'
    # 'blueberry'
    'pulseaudio-bluetooth'
    'blueman'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  sudo systemctl enable bluetooth
  sudo systemctl start bluetooth

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

# ------------------------------------------------------
# --- INSTALL NETWORK
# ------------------------------------------------------
install_network(){
  echo -e "${green}${bold}[*] INSTALLING NETWORK...${normal}${no_color}"
  echo
  sleep 3

  PKGS=(
    'wpa_supplicant'
    'dialog'
    'networkmanager'
    'openvpn'
    'networkmanager-openvpn'
    'networkmanager-vpnc'
    'network-manager-applet'
    'dhclient'
    'libsecret'
    'dnsutils'
  )

  for PKG in "${PKGS[@]}"; do
    sudo pacman -S "$PKG" --noconfirm --needed
  done

  sudo systemctl enable NetworkManager.service
  sudo systemctl start NetworkManager.service

  echo
  echo -e "${magenta}${bold}[+] DONE ---------${normal}${no_color}"
  echo
}

cmd=(dialog --clear --separate-output --checklist "Select (with space) what script should do.\\nChecked options are required for proper installation, do not uncheck them if you do not know what you are doing." 26 86 16)
options=(1 "System update" off
         0 "Install Xorg" off
         0 "Install Printers & Scanners" off
         0 "Install Bluetooth" off
         0 "Install Network" off
         0 "Install Audio" off
         0 "Install Packages" off
         0 "Install Graphics & Design" off
         9 "Install Fonts" off
       )
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

clear

for choice in $choices
do
    case $choice in
        1) system_update;;
        2) install_aur_helper;;
        0) install_xorg;;
        0) install_printers_scanners;;
        0) install_bluetooth;;
        0) install_network;;
        0) install_audio;;
        0) install_pkgs;;
        0) install_graphics;;
        0) install_fonts;;
    esac
done
