#!/usr/bin/env bash

DIR="$(dirname $(realpath "$0"))"
cd "$DIR"

if [[ ! -f /etc/os-release ]]; then
    echo "Could not determine distribution; exiting..."
    exit 1
fi

. /etc/os-release
distro=${NAME,,}

if [[ $distro == "arch linux" ]]; then
    pacman=(
        # services
        "networkmanager"
        "bluez" "bluez-libs"
        "pipewire" "pipewire-alsa" "pipewire-pulse" "pipewire-audio" "pipewire-jack" "wireplumber" "alsa-utils"

        # Hyprland core
        "hyprland" "hypridle" "hyprpaper" "hyprlock" 
        "hyprpolkitagent" "hyprutils" "hyprland-qtutils"
        "xdg-desktop-portal-hyprland" "xdg-desktop-portal-gtk"
        # Hyprland extra
        "qt6ct" "gnome-keyring"
        "waybar" "mako" "wofi" "wl-clipboard" "copyq" "grim" "slurp"
        "adwaita-icon-theme" "adwaita-cursors" "dolphin"
        "network-manager-applet" "blueman"
        "playerctl" "pavucontrol"
        "upower"

        # terminal
        "kitty" "fish" "ranger" "htop" "tmux" "psmisc"
        
        # misc
        "wget" "curl" "git" "entr" "zip" "base-devel"
        "ninja" "make" "cmake"
        "neofetch" "fastfetch" "fortune-mod" "cowsay"
        
        # languages
        "python" "php" "go" "clang"

        # editors
        "nano" "vim" "helix"

        # network drives
        "rclone" "cifs-utils"

        # container
        "podman" "podman-compose" "podman-docker" "distrobox"

        # VMs
        "virtualbox" "virtualbox-host-modules-arch" "linux-headers"

        # fonts
        "adwaita-fonts"
        "ttf-jetbrains-mono" "ttf-jetbrains-mono-nerd"
        "noto-fonts" "noto-fonts-emoji" "noto-fonts-cjk"

        # flatpak
        "flatpak" "libportal"

        # apps
        "keepassxc" "remmina"
    )

    yay=(
        "light"
        "snapd"
        "brave-bin"
    )

    systemd=(
        "snapd.socket"
        "snapd.apparmor.service"
        "NetworkManager.service"
        "bluetooth.service"
    )

    snap_classic=(
        "code"
        "flutter"
        "android-studio"
    )

    flatpak=(
        "org.signal.Signal"
        "com.spotify.Client"
        "net.ankiweb.Anki"
        "org.gimp.GIMP"
        "org.inkscape.Inkscape"
        "org.onlyoffice.desktopeditors"
        "com.github.xournalpp.xournalpp"
        "com.github.flxzt.rnote"
        "net.nokyan.Resources"
        "org.gnome.Loupe"
        "org.gnome.Evince"
        "org.gnome.Totem"
    )

    sudo pacman -Syu --noconfirm
    sudo pacman --noconfirm --needed -S "${pacman[@]}"

    sudo ln -s "$(which helix)" /usr/local/bin/hx

    if ! command -v yay &> /dev/null; then
        git clone https://aur.archlinux.org/yay.git /tmp/yay
        cd /tmp/yay && makepkg -si --noconfirm
        yay -Sy --noconfirm
        cd "$DIR"
    fi

    yay --noconfirm --needed -S "${yay[@]}"

    if [[ ! -e /snap ]]; then
        sudo ln -s /var/lib/snapd/snap /snap
    fi

    for item in ${systemd[@]}; do
        sudo systemctl enable --now $item
    done

    while ! sudo snap refresh; do sleep 1; done

    for prog in ${snap_classic[@]}; do
        sudo snap install $prog --classic
    done

    flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
    flatpak install -y "${flatpak[@]}"

    sudo chsh -s /usr/bin/fish $USER

    sudo usermod -aG video $USER
    sudo usermod -aG kvm $USER

    xdg-mime default org.gnome.Loupe.desktop image/png
    xdg-mime default org.gnome.Loupe.desktop image/jpeg
    xdg-mime default org.gnome.Loupe.desktop image/gif
    xdg-mime default org.gnome.Loupe.desktop image/webp
    xdg-mime default org.gnome.Totem.desktop video/mp4
    xdg-mime default org.gnome.Totem.desktop video/x-matroska
    xdg-mime default org.gnome.Totem.desktop video/x-msvideo
    xdg-mime default org.gnome.Totem.desktop video/quicktime
    xdg-mime default org.gnome.Totem.desktop video/webm
    xdg-mime default org.gnome.Totem.desktop video/mpeg
else
    echo "Unknown distribution '$distro'; exiting..."
    exit 1
fi

echo "-"
"$DIR/install"

echo "-"
echo "Done!"
