#!/bin/sh

printf '\033[2m
 _______  _______ ____      _    ____  _____ _     
| ____\ \/ /_   _|  _ \    / \  / ___|| ____| |    
|  _|  \  /  | | | |_) |  / _ \ \___ \|  _| | |    
| |___ /  \  | | |  _ <  / ___ \ ___) | |___| |___ 
|_____/_/\_\ |_| |_| \_\/_/   \_\____/|_____|_____|
\033[0m
'

show_dialog() {
    TITLE="$1" CONTENT="$2"
    printf "\033[1m%s\033[0m | %s" "$TITLE" "$CONTENT"
}

show_dialog_newline() {
    TITLE="$1" CONTENT="$2"
    printf "\033[1m%s\033[0m | %s\n" "$TITLE" "$CONTENT"
}

show_notification() {
    CONTENT="$1"
    printf "\n\033[1m\033[34mEXTRASEL:\033[0m \033[1m%s\033[0m" "$CONTENT"
    for i in 1 2 3; do sleep 0.8 ; printf "." ; done
}

warning() {
    CONTENT="$1"
    printf "\033[31mWarning:\033[0m %s\n" "$CONTENT"
}

[ "$(whoami)" != "root" ] && warning "You should be superuser." && exit 1

show_notification "Performing update"
apt update && apt upgrade -y

func_debloat() {
    show_notification "Debloating"
    apt purge gnome-games gnome-music synaptic transmission-gtk evolution shotwell -y
    apt autoremove -y
}

func_nonfree () {
    show_notification "Adding non-free & contrib repositories"
    apt-add-repository contrib non-free -y
}

func_locale_choose () {
    CHOSEN_LOCALE="$(cat /usr/share/i18n/SUPPORTED | fzf --margin 10% --prompt='Install locale: ')"

    grep -q "^$CHOSEN_LOCALE" /etc/locale.gen &&
        ! [ -z "$CHOSEN_LOCALE" ] &&
        CHOSEN_LOCALE="None" &&
        warning "Locale already installed"

    [ -z "$CHOSEN_LOCALE" ] &&
        CHOSEN_LOCALE="None" &&
        warning "Not chosen"
}

func_locale_install () {
    [ "$CHOSEN_LOCALE" = "None" ] && return 0
    [ "$CHOSEN_LOCALE" = "" ] && return 0
    show_notification "Installing locales..."
    echo "$CHOSEN_LOCALE" | tee -a '/etc/locale.gen'
    locale-gen
}

func_codecs_msfonts () {
    show_notification "Installing multimedia codecs & microsoft fonts"
    apt install vlc libavcodec-extra ttf-mscorefonts-installer
}

func_steam() {
    [ "$nonfree" != "y" ] && warning "Steam requires non-free repositories, Steam not installed." && return 1
    show_notification "Installing Steam"
    dpkg --add-architecture i386
    apt update
    apt install mesa-vulkan-drivers libglx-mesa0:i386 mesa-vulkan-drivers:i386 libgl1-mesa-dri:i386
    apt install steam-installer
}

func_flatpak() {

    if [ "$XDG_CURRENT_DESKTOP" = "" ]
    then
      desktop=$(echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/')
    else
      desktop=$XDG_CURRENT_DESKTOP
    fi
    desktop="$(echo "$desktop" | tr '[:upper:]' '[:lower:]')"

    [ "$desktop" = "gnome" ] && store_plugin="gnome-software-plugin-flatpak"
    [ "$desktop" = "kde" ] && store_plugin="plasma-discover-backend-flatpak"

    show_notification "Installing flatpak"
    apt install flatpak "$store_plugin"
    flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
}

printf '\033[2m
  ____ ___  _   _ _____ ___ ____ 
 / ___/ _ \| \ | |  ___|_ _/ ___|
| |  | | | |  \| | |_   | | |  _ 
| |__| |_| | |\  |  _|  | | |_| |
 \____\___/|_| \_|_|   |___\____|
\033[0m
'

show_dialog "DE-BLOAT" "Remove unnecessary packages (e.g. gnome-games) [y/n]:" && read -r debloat
show_dialog "NON-FREE" "Add non-free & contrib repositories (e.g. for Valve's Steam) [y/n]: " && read -r nonfree
func_locale_choose
show_dialog_newline "LOCALE" "Add locale: $CHOSEN_LOCALE"
show_dialog "CODECS & FONTS" "Add multimedia codecs & microsoft fonts (e.g. Arial) [y/n]: " && read -r codecs
show_dialog "STEAM" "Install the 'steam-installer' to launch steam installation [y/n]: " && read -r steaminstall
show_dialog "FLATPAK" "Install flatpak repositories (e.g. for Discord, Microsoft Teams...) [y/n]: " && read -r flatinstall

[ "$debloat" = "y" ] && func_debloat
[ "$nonfree" = "y" ] && func_nonfree
func_locale_install
[ "$codecs" = "y" ] && func_codecs_msfonts
[ "$steaminstall" = "y" ] && func_steam
[ "$flatinstall" = "y" ] && func_flatpak