#!/bin/bash

# omarchy:summary=Set the current background image or video
# omarchy:args=<path-to-media>
# omarchy:examples=omarchy theme bg set ~/Pictures/background.png

if [[ -z $1 ]]; then
  echo "Usage: omarchy-theme-bg-set <path-to-media>" >&2
  exit 1
fi

BACKGROUND="$(realpath "$1")"
CURRENT_BACKGROUND_LINK="$HOME/.local/state/omarchy/current/background"

if [[ ! -f $BACKGROUND ]]; then
  echo "File does not exist: $BACKGROUND" >&2
  exit 1
fi

# A theme switch repoints the background too, so the two take turns.
exec 9>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-theme-set.lock"
flock 9

INTRO=$(omarchy-theme-bg-boot-intro --resolve-enabled "$BACKGROUND")

# The shell sequences the change, with the intro or the ordinary transition,
# and repoints the link itself once nothing on screen can flash.
if [[ $(omarchy-shell shell switchBackground "$BACKGROUND" "$INTRO" 2>/dev/null) != "ok" ]]; then
  ln -nsf "$BACKGROUND" "$CURRENT_BACKGROUND_LINK"
  omarchy-shell -q background set "$BACKGROUND"
fi
