#!/usr/bin/bash -x

# Check that SWAYSOCK is in the env before doing anything
[ -z "$SWAYSOCK" ] && echo '$SWAYSOCK Not set in env. Exiting.'; exit 1

# Path to genbg.gp
genbg="/home/thurstylark/.config/sway/$(uname -n)/genbg.gp"

# Scale mode and fallback color (See: man sway-output)
scalemode="center"
color='#0000FF'

# Retrieve dimensions of currently connected display
outw=$(swaymsg -r -t get_outputs | jq -r '.[0].current_mode.width')
outh=$(swaymsg -r -t get_outputs | jq -r '.[0].current_mode.height')

# Set up a tempfile for the background location
bgstash="$(mktemp tl-sway-bg.XXXX)"

# Clean up any old files 
rm ${tempfile%.*}.*

# Generate the file in question
gnuplot -c "$genbg" "$outw" "$outh" > "$bgstash"

# Set the background
swaymsg "output * bg $bgstash $scalemode $color"
