APPS=(
  barrier
  cmake
  curl
  flatpak
  g++
  htop
  neofetch
  ninja-build
  plasma-discover-backend-flatpak
  ripgrep
  rtorrent
  scdaemon
  steam
  stow
  tmux
  tmuxp
  virt-manager
  xclip
)

FLATPAK_APPS=(
  ch.protonmail.protonmail-bridge
  com.logseq.Logseq
  com.obsproject.Studio
  com.protonvpn.www
  com.ultimaker.cura
  im.riot.Riot
  org.blender.Blender
  org.darktable.Darktable
  org.freecadweb.FreeCAD
  org.gimp.GIMP
  org.inkscape.Inkscape
  org.libreoffice.LibreOffice
  org.mozilla.Thunderbird
  org.mozilla.firefox
)

DOTFILES=(
  bash
  konsole
  #menus
  nvim
  rtorrent
  tmux
  tmuxp
)

# Check if Snap is installed
if [ -x "$(command -v snap)" ]; then
  echo "Remove Snap packages first before running this script!"
  exit 1
fi

# Check for Debian installation. If yes, remove some default programs
while true; do
  read -p "Is this a Debian installation? y/N: " osChoice
  osChoice=$(echo "$osChoice" | tr '[:upper:]' '[:lower:]')

  if [ "$osChoice" = "y" ]; then
    sudo apt purge libreoffice-core libreoffice-base-core libreoffice-common libreoffice-style-breeze libreoffice-style-colibre firefox-esr gimp -y
    sudo apt autoremove -y
    break
  elif [ "$osChoice" = "n" ]; then
    break
  else
    echo "Invalid input. Please enter y or n."
  fi
done

# Sym Link Home Folders To External Storage
while true; do
  read -p "Do you want to link home folders with external storage? y/N: " storageChoice
  storageChoice=$(echo "$storageChoice" | tr '[:upper:]' '[:lower:]')

  sudo rm -rf ~/Public ~/Templates

  if [ "$storageChoice" = "y" ]; then
    read -p "Specify the path to the external storage device (full path must be used): " storagePath

    sudo rm -rf ~/Documents ~/Pictures ~/Downloads

    ln -s "$storagePath/Pictures/" ~/
    ln -s "$storagePath/Repos/" ~/
    ln -s "$storagePath/Documents/" ~/
    ln -s "$storagePath/Downloads/" ~/

    break
  elif [ "$storageChoice" = "n" ]; then
    echo "Skipping sym linking..."
    cp -r ./Repos ~
    break
  else
    echo "Invalid input. Please enter y or n."
  fi
done

# Install Apt Apps
for i in "${APPS[@]}"
do
  if [ -x "$(command -v $i)" ]; then
    echo "$i already installed!"
  else
    sudo apt install $i -y
  fi
done

# Add Flathub Repo
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

# Install Flatpak Apps
for i in "${FLATPAK_APPS[@]}"
do
  if [ -x "$(command -v $i)" ]; then
    echo "$i already installed!"
  else
    flatpak install $i -y
  fi
done

# NVM Setup
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

source ~/.bashrc
. ~/.nvm/nvm.sh
nvm install --lts

# Add Yubikey Support to bashrc
if grep -q '# Yubikey SSH Support' ~/.bashrc; then
  echo "Yubikey support already added."
else

echo "
# Yubikey SSH Support
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
" >> ~/.bashrc

fi

# Remove default menu config files
sudo rm -rf ~/.config/menus ~/.config/plasma-org.kde.plasma.desktop-appletsrc

# Link Config Files
for folder in ${DOTFILES[@]}
do
  echo $folder
  stow $folder --adopt
done

git restore .

# Font Install
sudo cp ./fonts/Fira_Code_Regular_Nerd_Font_Complete.ttf /usr/local/share/fonts
sudo cp ./fonts/FiraCodeNerdFont-Retina.ttf /usr/local/share/fonts

# Check if this is my profile
while true; do
  read -p "Is this profile for Kendall? y/N: " profileChoice
  profileChoice=$(echo "$profileChoice" | tr '[:upper:]' '[:lower:]')

  if [ "$profileChoice" = "y" ]; then
    # Add Profile Image
    sudo rm ~/.face
    cp ./profile/.face ~

    # Add User To Group For Virtual Manager
    sudo adduser kendall libvirt-qemu
    break
  elif [ "$profileChoice" = "n" ]; then
    break
  else
    echo "Invalid input. Please enter y or n."
  fi
done

# Add touchscreen scroll support to Firefox
while true; do
  read -p "Does this device have a touchscreen? y/N: " touchscreenChoice
  touchscreenChoice=$(echo "$touchscreenChoice" | tr '[:upper:]' '[:lower:]')

  if [ "$touchscreenChoice" = "y" ]; then
    sudo flatpak override --env="MOZ_USE_XINPUT2=1" org.mozilla.firefox
    break
  elif [ "$touchscreenChoice" = "n" ]; then
    echo "Skipping touchscreen setup..."
    break
  else
    echo "Invalid input. Please enter y or n."
  fi
done

# Add filesystem override to Firefox
sudo flatpak override org.mozilla.firefox --filesystem=$HOME

# Install latest Neovim
if ! [ -x "$(command -v nvim)" ]; then
  sudo apt remove gettext-base -y
  sudo apt install gettext -y
  source ~/.bashrc
  git clone https://github.com/neovim/neovim
  cd neovim
  make CMAKE_BUILD_TYPE=RelWithDebInfo && cd build && cpack -G DEB && sudo dpkg -i nvim-linux64.deb
  cd ~/.dotfiles/ && sudo rm -rf neovim
fi

# Install TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
