#!/usr/bin/env bb

(require '[babashka.process :refer [shell process exec]])

;; setup screens
;; todo: check if file exists, get home dir
(shell "/home/schrofi/.screenlayout/default.sh")

;; bspwm configuration
(defn- bspc [& commands]
  (apply shell (cons "bspc" commands)))

(defn- config [key value]
    (apply bspc (vector "config" key value)))

;; setup desktops
;; todo: handle no external display gracefully

;; read workspace configuration from config
;; format should be like: eDP-1 1 2 3 4 .., HDMI-A-0 X
;; where each monitor is defined on a separate line (no commas)
(def workspace-config
  (->> (slurp "/home/schrofi/.screenlayout/default-workspaces")
       (str/split-lines)
       (map #(str/split % #" "))))

(doseq [config workspace-config]
  (let [[monitor & workspaces] config]
    (->> workspaces
         (map str)
         (concat ["monitor" monitor "-d"])
         (apply bspc))))

(config "border_width" "2")
(config "window_gap" "12")

(config "split_ratio" "0.52")
(config "borderless_monocle" "true")

(config "focus_follows_pointer" "true")

;; todo
(bspc "rule" "-a" "alacritty:scratchpad" "-o" "state=floating" "sticky=on" "layer=above")

;; autostart
(defn- if-not-running [service run-command]
  (if (empty? (:out (shell {:out :string :continue true} "pgrep" "-x" service)))
    (run-command)))

(shell "setxkbmap" "de")
;; this is needed for the gnome keyring to function properly
(shell "dbus-launch" "--exit-with-session")
(shell "dbus-update-activation-environment" "--all")
(shell "gnome-keyring-daemon" "--start" "--components=secrets")

(if-not-running "sxhkd" #(process "sxhkd"))
(if-not-running "nitrogen" #(process "nitrogen" "--restore"))
(if-not-running "picom" #(process "picom" "--config" "/home/schrofi/.config/picom/picom.conf"))
(if-not-running "polybar" #(process "polybar"))
(if-not-running "flameshot" #(process "flameshot"))


;; start pipewire
;;(process "pipewire")
