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

echo "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 "Downloading $1 packages"
    # sudo pacman -Syu --noconfirm --color auto $(cat $pkgs_path/$1.pkgs)
    echo "Done"
}
function install-aur() {
    echo "Downloading $1 packages"
    # paru -Syu --noconfirm $(cat $pkgs_path/$1.pkgs)
    echo "Done"
}


# 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
# Install packages

echo "--- Downloading packages ---"

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

## Install vendor specific software 
if [ $CPU_vendor == "Genuine Intel" ]; then 
    install intel
fi

## Install paru
git clone https://aur.archlinux.org/paru.git /tmp/paru &&
cd /tmp/paru &&
makepkg -si
cd $script_path

install-aur aur

# Configuration

## Dotfiles
cp $config_path $dotfiles_path &&
cd $dotfiles_path &&
stow .
cd $script_path

## Swap (Zram)
is_zram_active=$(sudo systemctl is-active systemd-zram-setup@zram0.service)
if [ is_zram_active != "active" ]; then 
    ...
fi

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