#!/usr/bin/env sh

set -ex

_fail() {
    echo "$@" >&2
    exit 1
}
 
# Provision seatd + sway
pacman -S --noconfirm sway xorg-xwayland xorg-xlsclients qt5-wayland glfw-wayland xdg-desktop-portal-wlr seatd greetd greetd-wlgreet || _fail "Could not provision desktop packages"

systemctl enable greetd.service || _fail "Could not enable the greeter"
mkdir -p /etc/greetd
cat <<EOF > /etc/greetd/config.toml || _fail "Could not configure the greeter"
[terminal]
vt = 1

[default_session]
command = "sway --config /etc/greetd/sway-config"
user = "greeter"
EOF

cat <<'EOF' >  /etc/greetd/sway-config || _fail "Could not configure sway for the greeter"
set $mod Mod4
output * bg /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill
exec "wlgreet --command sway; swaymsg exit"
set $mode_shutdown " (<b>h</b>) hibernate (<b>l</b>) lock (<b>e</b>) logout (<b>r</b>) reboot (<b>u</b>) suspend (<b>s</b>) shutdown"
mode --pango_markup $mode_shutdown {
    bindsym e exec loginctl terminate-user $USER
    bindsym u mode "default", exec systemctl suspend
    bindsym h mode "default", exec systemctl hibernate
    bindsym s exec systemctl poweroff
    bindsym r exec systemctl reboot
    bindsym Escape mode "default"
}
bindsym $mod+Shift+e mode $mode_shutdown
EOF

## Provision tools
pacman -S --noconfirm git bind which tree ripgrep base-devel pulseaudio pavucontrol evince nemo chromium 1password 1password-cli
# Replace emacs-nox with normal emacs
pacman -R --noconfirm --unneeded emacs-nox
pacman -S --noconfirm emacs

## Provision conf
# FIXME: Get this token outa here
su - arrdem <<EOF
git clone https://arrdem:59642f9163e0a05f0d3755d84e0002949b5ee8a4@git.arrdem.com/arrdem/dotfiles.git conf
cd conf
./cram apply --execute
EOF
