#!/bin/sh
# Lock the sway session with gtklock, showing the lock-screen wallpaper
# (regolith.lockscreen.wallpaper.file), as the X11 lock screen does.
#
# This script is for gtklock only (--background is gtklock's option). It is
# the default of wm.program.lock_sway, set in 42_gtklock, and of the lid
# close script's lock, and it ships with that partial in the
# regolith-sway-gtklock package. To use another lock application, set
# wm.program.lock_sway to its command; support for another locker belongs in
# its own script and package, next to this one.
#
# The path goes to gtklock as a single argument, so it may contain spaces,
# and a leading ~ is expanded to the home directory. When the resource is
# unset or the file doesn't exist, gtklock still starts, just without the
# image: a wallpaper setting must never keep the session from locking.
#
# A look can style the lock screen with a gtklock stylesheet, named by the
# gtklock.stylesheet resource (as ilia.stylesheet styles the launcher), for
# example to keep the clock readable over its wallpaper. It is not used when
# the user has a gtklock stylesheet of their own (gtklock's style.css, or a
# style set in its config.ini).
#
# Arguments are passed on to gtklock (for example -d, to return once the
# session is locked).

wallpaper=$(trawlcat regolith.lockscreen.wallpaper.file "" 2> /dev/null)
# shellcheck disable=SC2088 # the resource holds a literal ~
case $wallpaper in
  "~/"*) wallpaper=$HOME/${wallpaper#"~/"} ;;
esac

gtklock_config=${XDG_CONFIG_HOME:-$HOME/.config}/gtklock
stylesheet=$(trawlcat gtklock.stylesheet "" 2> /dev/null)
if [ -n "$stylesheet" ]; then
  if [ -f "$gtklock_config/style.css" ] ||
     grep -qs '^[[:space:]]*style[[:space:]]*=' "$gtklock_config/config.ini"; then
    stylesheet=
  elif [ ! -f "$stylesheet" ]; then
    echo "regolith-sway-gtklock: gtklock.stylesheet: no such file: '$stylesheet'" >&2
    stylesheet=
  fi
fi
if [ -n "$stylesheet" ]; then
  set -- "$@" --style "$stylesheet"
fi

if [ -n "$wallpaper" ] && [ -f "$wallpaper" ]; then
  exec gtklock "$@" --background "$wallpaper"
fi

if [ -n "$wallpaper" ]; then
  echo "regolith-sway-gtklock: regolith.lockscreen.wallpaper.file: no such file: '$wallpaper'" >&2
fi
exec gtklock "$@"
