#!/usr/bin/env bash
# dar.vin + Flameshot uploader
# Install: download this file, `chmod +x darvin-flameshot.sh`, then run it with
# DARVIN_API_KEY=... ./darvin-flameshot.sh. Bind that command in your desktop
# environment if Wayland does not provide global app shortcuts.

set -euo pipefail

short_url="$({ flameshot gui --raw | curl -sf -F "file=@-;filename=shot.png" \
  -H "Authorization: Bearer ${DARVIN_API_KEY:?set DARVIN_API_KEY}" \
  -H "x-darvin-source: flameshot" \
  https://dar.vin/api/v3/screenshots; } | jq -er .shortUrl)"

if command -v wl-copy >/dev/null 2>&1; then
  printf '%s' "$short_url" | wl-copy
elif command -v xclip >/dev/null 2>&1; then
  printf '%s' "$short_url" | xclip -selection clipboard
elif command -v xsel >/dev/null 2>&1; then
  printf '%s' "$short_url" | xsel --clipboard --input
fi

if command -v notify-send >/dev/null 2>&1; then
  notify-send 'dar.vin screenshot uploaded' "$short_url"
fi

printf '%s\n' "$short_url"
