APPS=(
  cmake
  curl
  flatpak
  g++
  btop
  fastfetch
  ninja-build
  plasma-discover-backend-flatpak
  ripgrep
  rtorrent
  scdaemon
  steam
  stow
  tmux
  tmuxp
  virt-manager
  wl-clipboard
)


FLATPAK_APPS=(
  com.protonvpn.www
  im.riot.Riot
  org.darktable.Darktable
  org.freecad.FreeCAD
  org.gimp.GIMP
  org.inkscape.Inkscape
  org.libreoffice.LibreOffice
  com.brave.Browser
)


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


# 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 ~/Videos ~/Music

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

    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.40.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/FiraCodeNerdFontMono-Medium.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 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


# 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-linux-x86_64.deb
  cd ~/.dotfiles/ && sudo rm -rf neovim
fi
