#!/usr/bin/fish

# configured for wayland
set LOCKPROG swaylock
set OPTIONAL_CMDS rg sed grim lockscreen-blur
set CONFIG "$HOME/.config/sway/swaylock.config"
set DEFAULT_ARGS --daemonize --ignore-empty-password --show-failed-attempts --color 302f3b --config $CONFIG
set BLUR_FACTOR 40

# check if lock program is running
if ps -e | grep " $LOCKPROG\$"
    echo "$LOCKPROG is already running"
    exit 1
end

# check if we have programs required to take a screenshot and do cool stuff with it
for cmd in $OPTIONAL_CMDS
    if not command -v $cmd &>/dev/null
        echo "$cmd could not be found, falling back to basic mode."
        $LOCKPROG $DEFAULT_ARGS
        exit 0
    end
end

# get a list of outputs/monitors
switch $XDG_CURRENT_DESKTOP
    case Hyprland
        set outputs (hyprctl monitors | rg Monitor | sed 's/Monitor //' | sed 's/ (ID.*$//')
    case sway
        set outputs (swaymsg -t get_outputs -r | rg "name" | sed 's/^.*"name": "//' | sed 's/",$//')
    case niri
        set outputs (niri msg outputs | rg --pcre2 -o '[^" ]+(?=")')
    case '*'
        echo "unsupported compositor, falling back to basic mode."
        $LOCKPROG $DEFAULT_ARGS
        exit 0
end

# take one screenshot of each output
for output in $outputs
    set img "/tmp/lock-$output.png"
    grim -o $output -t ppm - | lockscreen-blur -o $img -b $BLUR_FACTOR --stamp ~/.config/wallpapers/ferris-stamp.png &
    set -a images --image
    set -a images "$output:$img"
end

# wait for all screenshot jobs to complete
wait

$LOCKPROG $DEFAULT_ARGS $images
