#!/bin/sh
version="Beta 25.04.04-2"

echo -e "Atlas Desktop Installer - ${version}\n"

# Definitions

## Vars
script_path=$PWD
pkgs_path="${script_path}/packages"
config_path="${script_path}/config"
CPU_vendor=$(lscpu | grep Vendor | awk '{print $NF}')
dotfiles_path="~/.dotfiles"


## Functions
function install() {
    echo -e "\nDownloading $1 packages...\n"
    sudo pacman -Syu --noconfirm --color auto $(cat $pkgs_path/$1.pkgs)
    echo -e "\n\nDone\n"
}
function install_aur() {
    echo -e "\nDownloading $1 packages...\n"
    paru -Syu --noconfirm --color auto $(cat $pkgs_path/$1.pkgs)
    echo -e "\nDone\n"
}


# Checks

## Sudo installed
pacman -Q sudo > /dev/null
if [ $? != 0 ]; then
    echo "Please install 'sudo' first"
    exit 1
fi

## Scripts presence
if [ ! -d "$pkgs_path" ]; then
    echo "Couldn't find 'packages' folder inside ${script_path}"
    exit 1
fi

if [ ! -d "$config_path" ]; then
    echo "Couldn't find 'config' folder inside ${script_path}"
    exit 1
fi


# Installation

echo "--- Installation ---"

## Main packages
install core
install cli-dev-tools
install fonts
install desktop
install theming

## Vendor specific packages 
if [ $CPU_vendor == "Genuine Intel" ]; then 
    install intel
fi

## Paru
git clone https://aur.archlinux.org/paru.git /tmp/paru &&
echo "Installing paru" &&
cd /tmp/paru &&
makepkg -si
cd $script_path

## AUR packages
install_aur aur


# Configuration

echo -e "\n--- Configuration ---\n"

## Dotfiles
echo "Copying configuration files"
cp -r $config_path $dotfiles_path &&
cd $dotfiles_path &&
stow .
cd $script_path

## Install launcher theme (albert)
sudo ln -s .config/albert/schemes/Seventy\ Eight.qss /usr/share/albert/widgetsboxmodel/themes/

## Swap (Zram)
is_zram_active=$(sudo systemctl is-active systemd-zram-setup@zram0.service)
if [ is_zram_active != "active" ]; then 
    echo -e "\n========================================"
    echo "WARNING: you don't have Zram enabled"
    echo "Since this script is not yet capable of activating zram, you'll have to do it yourself"
    echo "Please refer to https://wiki.archlinux.org/title/Zram#Using_zram-generator"
    echo -e "========================================\n"
fi

## Mandatory Access Control (Apparmor)
is_aa_active=$(sudo systemctl is-active apparmor.service)
if [ is_aa_active != "active" ]; then 
    echo -e "\n========================================"
    echo "WARNING: you don't have AppArmor enabled"
    echo "Since this script is not yet capable of managing kernel parameters, you'll have to do it yourself"
    echo "Please refer to https://wiki.archlinux.org/title/AppArmor"
    echo -e "========================================\n"
fi

## Display manager (Greetd)
sudo systemctl enable greetd

## Firewall (UFW)
sudo systemctl enable ufw
sudo ufw enable

# Finish

fastfetch
echo
echo "Installation finished"
echo "Please check the previous outputs for warnings"
echo "You can make final adjustements before rebooting"
echo "- In particular, if you are using a qwerty keyboard, changing input-fr to input-en in ~/.config/hypr/hyprland.conf"