#!/usr/bin/env bash
set -euo pipefail

if [[ $# -eq 0 ]]; then
  echo "Usage: hyprshot-wrapper.sh MODE [MODE ...]" >&2
  exit 1
fi

modes=()
for mode in "$@"; do
  modes+=("-m" "$mode")
done

screens_dir="${XDG_PICTURES_DIR:-$HOME/Pictures}/Screenshots"
mkdir -p "$screens_dir"

stamp=$(date +'%Y-%m-%d_%H-%M-%S')
filename="Screenshot_${stamp}.png"
filepath="$screens_dir/$filename"

if ! hyprshot --freeze "${modes[@]}" --output-folder "$screens_dir" --filename "$filename"; then
  exit $?
fi

if [[ -f "$filepath" ]]; then
  if command -v wl-copy >/dev/null 2>&1; then
    wl-copy < "$filepath" || true
  fi

  satty_bin="${SATTY_BIN:-$HOME/data/git/clone/Satty/target/debug/satty}"
  if [[ -x "$satty_bin" ]]; then
    if command -v uwsm >/dev/null 2>&1; then
      uwsm app -- "$satty_bin" -f "$filepath" &
    else
      "$satty_bin" -f "$filepath" &
    fi
  fi
fi
