#!/bin/bash

# Setup font colors
RED='\033[0;31m'
PURPLE='\033[0;35m'
GREEN='\033[0;32m'
NC='\033[0m'

# Set configuration file URL
CONFIG_URL="https://dl.dinenon.com/mint/configs"

################################################################################
####  Welcome
################################################################################
printf "${PURPLE} ████████▄   ▄█  ███▄▄▄▄      ▄████████ ███▄▄▄▄    ▄██████▄  ███▄▄▄▄  ${NC}\n"
printf "${PURPLE}███   ▀███ ███  ███▀▀▀██▄   ███    ███ ███▀▀▀██▄ ███    ███ ███▀▀▀██▄ ${NC}\n"
printf "${PURPLE}███    ███ ███▌ ███   ███   ███    █▀  ███   ███ ███    ███ ███   ███ ${NC}\n"
printf "${PURPLE}███    ███ ███▌ ███   ███  ▄███▄▄▄     ███   ███ ███    ███ ███   ███ ${NC}\n"
printf "${PURPLE}███    ███ ███▌ ███   ███ ▀▀███▀▀▀     ███   ███ ███    ███ ███   ███ ${NC}\n"
printf "${PURPLE}███    ███ ███  ███   ███   ███    █▄  ███   ███ ███    ███ ███   ███ ${NC}\n"
printf "${PURPLE}███   ▄███ ███  ███   ███   ███    ███ ███   ███ ███    ███ ███   ███ ${NC}\n"
printf "${PURPLE}████████▀  █▀    ▀█   █▀    ██████████  ▀█   █▀   ▀██████▀   ▀█   █▀  ${NC}\n"
printf "\n"
printf "${PURPLE}Linux Mint Configuration Script${NC}\n"
printf "\n\n"
sleep 5

################################################################################
####  Get User Options
################################################################################
printf "${GREEN}Gathering user options...${NC}\n"
printf "\n\n"

while true; do
    read -p "Install game support? " yn
    case $yn in
        [Yy]* ) $game_apps = true; break;;
        [Nn]* ) $game_apps = false; break;;
        * ) echo "Please answer yes or no.";;
    esac
done
$yn = ""
printf "\n\n"

while true; do
    read -p "Install creative tools? " yn
    case $yn in
        [Yy]* ) $creative_apps = true; break;;
        [Nn]* ) $creative_apps = false; break;;
        * ) echo "Please answer yes or no.";;
    esac
done
$yn = ""
printf "\n\n"

while true; do
    read -p "Install IT tools? " yn
    case $yn in
        [Yy]* ) $it_apps = true; break;;
        [Nn]* ) $it_apps = false; break;;
        * ) echo "Please answer yes or no.";;
    esac
done
$yn = ""
printf "\n\n"

printf "System will be setup with basic configuration"
if [[ $game_apps -eq true ]]; then
    printf ", game apps"
fi
if [[ $creative_apps -eq true ]]; then
    printf ", creative apps"
fi
if [[ $it_apps -eq true ]]; then
    printf ", it apps"
fi
printf ".\n\n"
sleep 5

################################################################################
####  Install Nala
################################################################################
printf "${GREEN}Installing Nala package manager...${NC}\n"
sleep 2
sudo -s << 'END_OF_SUDO'
    apt update
    apt install nala -y
    nala update
END_OF_SUDO
printf "\n\n"

################################################################################
####  Purge Unused Programs
################################################################################
printf "${GREEN}Purging unused default programs...${NC}\n"
sleep 2
sudo -s << 'END_OF_SUDO'
    nala purge -y libreoffice* \
        thingy \
        simple-scan \
        drawing \
        pix* \
        firefox* \
        hexch* \
        thunderbird* \ 
        mozilla* \ 
        transmission* \
        webapp-manager \
        celluloid* \
        hypnot* \
        mpv \
        rhythm*

    nala autoremove -y
END_OF_SUDO
printf "\n\n"

################################################################################
#### Remove Unused Flatpaks
################################################################################
printf "${GREEN}Removing unused flatpaks...${NC}\n"
sleep 2

flatpak uninstall --unused
printf "\n\n"

################################################################################
####  Install New System Packages
################################################################################
# Ensure basic utilities
printf "${GREEN}Installing basic system utilities...${NC}\n"
sleep 2
sudo -s << 'END_OF_SUDO'
    nala update
    nala install -y wget \
        curl \
        unzip \
        ubuntu-drivers \
        xdotool btop \
        apt-transport-https \
        ubuntu-drivers
END_OF_SUDO
printf "\n\n"

################################################################################
####  Install Nerd Fonts
################################################################################
# Download and install DejaVuSansM Nerd Font
printf "${GREEN}Installing fonts...${NC}\n"
sleep 2
wget https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/DejaVuSansMono.zip
unzip DejaVuSansMono.zip
if test -d /home/$USER/.local/share/fonts; then
    echo "Fonts folder already exists"
else
    echo "Fonts folder not found, creating..."
    mkdir /home/$USER/.local/share/fonts
fi
for font_file in *.ttf; do
    mv -f "$font_file" /home/$USER/.local/share/fonts/
done
fc-cache -f -v

################################################################################
####  Install Flatpaks
################################################################################
printf "${GREEN}Installing new flatpaks...${NC}\n"
flatpak install --assumeyes --noninteractive flathub org.wezfurlong.wezterm
flatpak install --assumeyes --noninteractive flathub com.vscodium.codium
flatpak install --assumeyes --noninteractive flathub org.videolan.VLC
flatpak install --assumeyes --noninteractive flathub com.nextcloud.desktopclient.nextcloud
flatpak install --assumeyes --noninteractive flathub com.bitwarden.desktop
flatpak install --assumeyes --noninteractive flathub io.missioncenter.MissionCenter
flatpak install --assumeyes --noninteractive flathub com.moonlight_stream.Moonlight
flatpak install --assumeyes --noninteractive flathub io.github.pwr_solaar.solaar
flatpak install --assumeyes --noninteractive flathub io.github.ilya_zlobintsev.LACT

if [[ $game_apps -eq true ]]; then
    printf "${GREEN}Installing game apps...${NC}\n"
    flatpak install --assumeyes --noninteractive com.discordapp.Discord
    flatpak install --assumeyes --noninteractive flathub com.valvesoftware.Steam
    flatpak install --assumeyes --noninteractive flathub com.heroicgameslauncher.hgl
    flatpak install --assumeyes --noninteractive flathub org.prismlauncher.PrismLauncher
    flatpak install --assumeyes --noninteractive flathub net.lutris.Lutris
    flatpak install --assumeyes --noninteractive flathub com.vysp3r.ProtonPlus
    flatpak install --assumeyes --noninteractive flathub io.github.benjamimgois.goverlay
    flatpak install --assumeyes --noninteractive flathub io.github.antimicrox.antimicrox
    flatpak install --assumeyes --noninteractive flathub io.github.berarma.Oversteer
fi

if [[ $creative_apps -eq true ]]; then
    printf "${GREEN}Installing creative apps...${NC}\n"
    flatpak install --assumeyes --noninteractive flathub org.kde.kdenlive
    flatpak install --assumeyes --noninteractive flathub org.freecad.FreeCAD
    flatpak install --assumeyes --noninteractive flathub org.blender.Blender
    flatpak install --assumeyes --noninteractive flathub com.orcaslicer.OrcaSlicer
fi

if [[ $it_apps -eq true ]]; then
    printf "${GREEN}Installing IT apps...${NC}\n"
    flatpak install --assumeyes --noninteractive flathub org.wireshark.Wireshark
    flatpak install --assumeyes --noninteractive flathub io.github.totoshko88.RustConn
    flatpak install --assumeyes --noninteractive flathub org.nmap.Zenmap
    flatpak install --assumeyes --noninteractive flathub com.devolutions.remotedesktopmanager
fi

################################################################################
####  Install Native Programs
################################################################################
# Setup terminal utilities
printf "${GREEN}Installing terminal utilities...${NC}\n"
echo "Installing Starship, fzf & zoxide"
sudo nala install -y starship fzf zoxide
echo 'eval "$(starship init bash)"' >> /home/$USER/.bashrc
echo 'eval "$(fzf --bash)"' >> /home/$USER/.bashrc
echo 'eval "$(zoxide init bash)"' >> /home/$USER/.bashrc
echo "Installing Atuin"
curl --proto '=https' --tlsv1.2 -LsSf https://setup.atuin.sh | sh s -- --non-interactive
printf "\n\n"

# Brave Origin
printf "${GREEN}Installing Brave Origin...${NC}\n"
curl -fsS https://dl.brave.com/install.sh | FLAVOR=origin CHANNEL=nightly sh
printf "\n\n"

# Joplin Client
printf "${GREEN}Installing Joplin...${NC}\n"
wget -O - https://raw.githubusercontent.com/laurent22/joplin/dev/Joplin_install_and_update.sh | bash
printf "\n\n"

# Remotely Client
printf "${GREEN}Installing Remotely...${NC}\n"
sudo rm -f /tmp/Install-Remotely.sh
sudo wget -q -O /tmp/Install-Remotely.sh https://remote.dinenon.com/api/ClientDownloads/UbuntuInstaller-x64/504d81e9-47bd-4453-88be-43ebfc883875
sudo chmod +x /tmp/Install-Remotely.sh
sudo /tmp/Install-Remotely.sh
printf "\n\n"

################################################################################
####  Import Configurations
################################################################################
# Cinnamon config
# Export config ``dconf dump /org/cinnamon/ > cinnamon.dconf
# Import config ``dconf load /org/cinnamon/ < cinnamon.dconf
#wget "${CONFIG_URL}/cinnamon.dconf"
#dconf load /org/cinnamon/ < cinnamon.dconf

# Cinnamon Hotkeys
#    May not be needed if the above includes hotkeys
# Utility to allow text hotkeys in Cinamon
# Example to paste date: ```bash -c "sleep .1 && xdotool type \"$(date +'%Y-%m-%d')\""```
# Export hotkeys ``dconf dump /org/cinnamon/desktop/keybindings/ > dconf-settings.conf``
# Import hotkeys ``dconf load /org/cinnamon/desktop/keybindings/ < dconf-settings.conf``
#wget "${CONFIG_URL}/dconf-settings.conf"
#mv -f dconf-settings.conf /org/cinnamon/desktop/keybindings/

cd ~
# Bash Aliases
wget "${CONFIG_URL}/bash_aliases.txt"
mv -f ~/bash_aliases.txt ~/.bash_aliases

cd ~/.config/
# Starship
#wget "${CONFIG_URL}/starship.toml"

# Atuin
#wget "${CONFIG_URL}/atuin.toml"

# fzf
#wget "${CONFIG_URL}/fzf.toml"

# Zoxide
#wget "${CONFIG_URL}/zoxide.toml"

# Source bashrc to get all the new stuff
source /home/$USER/.bashrc
printf "\n\n"

################################################################################
####  Drivers
################################################################################
# Check for nVidia GPU, autoinstall recommended driver
if lspci | grep -i 'nvidia' > /dev/null; then
    printf "${GREEN}nVidia GPU detected, installing recommended drivers...${NC}\n"
    sudo ubuntu-drivers install
else
    printf "${GREEN}No nVidia GPU detected, skipping drivers...${NC}\n"
fi
printf "\n\n"

################################################################################
####  Tailscale
################################################################################
# Do this last to allow clicking activation link after install
printf "${GREEN}Installing Tailscale...${NC}\n"
curl -fsSL https://tailscale.com/install.sh | sh
printf "\n\n"

################################################################################
####  Reboot
################################################################################
printf "${PURPLE}System initialization is complete.${NC}\n"
printf "\n\n"
printf "${RED}Click the Tailscale activation link BEFORE pressing Enter${NC}\n"
printf "\n\n"
read -p "Press Enter to update and reboot..."
nala update
nala upgrad -y
reboot
