#!/usr/bin/env bash
set -euo pipefail
(($EUID == 0)) && echo "Do not run as root" && exit 1
sudo -v

SETUP_DIR="$HOME/.arch-installer"

git clone --depth 1 https://github.com/yookibooki/arch-installer.git "$SETUP_DIR" || true

mapfile -t PACMAN_PKGS < <(grep -vE '^\s*$|^\s*#' "$SETUP_DIR/pacman.txt")
mapfile -t AUR_PKGS < <(grep -vE '^\s*$|^\s*#' "$SETUP_DIR/aur.txt")

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

command -v yay >/dev/null || {
	git clone --depth 1 https://aur.archlinux.org/yay-bin.git
	(cd yay-bin && makepkg -si --noconfirm)
	rm -rf yay-bin
}
yay -S --noconfirm --needed "${AUR_PKGS[@]}"

for script in "$SETUP_DIR/scripts/"*.sh; do
	[[ -f "$script" ]] && bash "$script"
done

sudo systemctl daemon-reload
echo "Done. Reboot."
