#!/usr/bin/env bash

wait_for_window() {
  local pid=$1
  local addr=""
  while :; do
    addr=$(hyprctl clients -j | jq -r ".[] | select(.pid==$pid) | .address")
    [[ -n "$addr" ]] && break
    sleep 0.1
  done
  echo "$addr"
}

hyprctl dispatch workspace 1
hyprctl dispatch -- exec firefox

sleep 0.5

hyprctl keyword windowrulev2 "workspace 2 silent, class:^(nvim-term)$"

hyprctl dispatch workspace 2

kitty --class nvim-term -e nvim &
pid_nvim=$!
addr_nvim=$(wait_for_window $pid_nvim)

hyprctl keyword windowrulev2 "unset, class:^(nvim-term)$"

hyprctl dispatch focuswindow address:$addr_nvim
hyprctl dispatch split r

hyprctl keyword windowrulev2 "workspace 2 silent, class:^(Thunar)$"
thunar &
pid_thunar=$!
wait_for_window $pid_thunar
hyprctl keyword windowrulev2 "unset, class:^(Thunar)$"

hyprctl dispatch workspace 3
hyprctl dispatch -- exec "flatpak run dev.vencord.Vesktop"

sleep 0.5

hyprctl dispatch workspace 4
kitty --class btop-term -e btop &
wait_for_window $(pidof kitty)

hyprctl dispatch workspace 5
kitty --class ranger-term -e ranger &
wait_for_window $(pidof kitty)

hyprctl dispatch workspace 1
