#!/bin/sh
# Screenshot helper for sway.
# Copyright 2017,2019,2021 Witalij Berdinskich.

if [ -z $WAYLAND_DISPLAY ]; then
	(>&2 echo Wayland is not running)
	exit 1
fi

## Put the path to your own screenshot folder
## to variable SWAYSHOT_SCREENSHOTS here: ~/.config/swayshot.sh
if [ -f ~/.config/swayshot.sh ]; then
	. ~/.config/swayshot.sh
fi

if [ -z $SWAYSHOT_SCREENSHOTS ]; then
	SWAYSHOT_SCREENSHOTS=$(xdg-user-dir PICTURES)
fi

SCREENSHOT_TIMESTAMP=$(date "+${SWAYSHOT_DATEFMT:-%F_%H-%M-%S_%N}")
SCREENSHOT_FULLNAME="$SWAYSHOT_SCREENSHOTS"/screenshot_${SCREENSHOT_TIMESTAMP}.png

readonly filter='
# returns the focused node by recursively traversing the node tree
def find_focused_node:
    if .focused then . else (if .nodes then (.nodes | .[] | find_focused_node) else empty end) end;
# returns a string in the format that grim expects
def format_rect:
    "\(.rect.x),\(.rect.y) \(.rect.width)x\(.rect.height)";
find_focused_node | format_rect
'

make_screenshot() {
	case "$1" in
		-h|--help)
			echo 'Usage: swayshot [display|window|region]'
			return 0
			;;
		region)
			grim -g "$(slurp -b '#AFAFAFAF' -c '#FF3F3FAF' -s '#00000000' -w 3 -d)" "$2"
			;;
		window)
			grim -g "$(swaymsg --type get_tree --raw | jq --raw-output "${filter}")" "$2"
			;;
		*)
			grim -o "$(swaymsg --type get_outputs --raw | jq --raw-output '.[] | select(.focused) | .name')" "$2"
			;;
	esac
}

copy_to_clipboard() {
	if type wl-copy >/dev/null  2>&1; then
		if [ -z $SWAYSHOT_WL_COPY_FILE ]; then
			printf "%s" "$1" | wl-copy
		else
			wl-copy < "$SCREENSHOT_FULLNAME"
		fi
	elif type xsel >/dev/null  2>&1; then
		printf "%s" "$1" | xsel --clipboard
		unset SWAYSHOT_WL_COPY_FILE
	elif type xclip &>/dev/null; then
		printf "%s" "$1" | xclip -selection clipboard
		unset SWAYSHOT_WL_COPY_FILE
	else
		echo "$1"
	fi
}

show_message() {
	if type notify-send >/dev/null  2>&1; then
		notify-send --expire-time=3000 --category=screenshot --icon="$2" "$3" "$1"
	fi
}

upload_screenshot() {
	if [ -f "$1" ]; then
		if type curl >/dev/null  2>&1; then
			curl -s -F "file=@\"$1\";filename=.png" 'https://x0.at'
		fi
	fi
}

make_screenshot "$1" "$SCREENSHOT_FULLNAME"

if [ ! -f "$SCREENSHOT_FULLNAME" ]; then
	return 1;
fi

case "$2" in
	upload)
		SCREENSHOT_LOCATOR=$(upload_screenshot "$SCREENSHOT_FULLNAME")
		if [ -z $SCREENSHOT_LOCATOR ]; then
			copy_to_clipboard "$SCREENSHOT_FULLNAME"
			show_message "$SCREENSHOT_FULLNAME" document-save "Screenshot path"
			if [ ! -z $SWAYSHOT_WL_COPY_FILE ]; then
				show_message "Screenshot was copied to clipboard.\\nFeel free to paste it." "$SCREENSHOT_FULLNAME" "Screenshot image"
			fi
		else
			copy_to_clipboard "$SCREENSHOT_LOCATOR"
			show_message "$SCREENSHOT_LOCATOR" document-send "Screenshot URL"
			if [ ! -z $SWAYSHOT_WL_COPY_FILE ]; then
				show_message "Screenshot was copied to clipboard.\\nFeel free to paste it." "$SCREENSHOT_FULLNAME" "Screenshot image"
			fi
		fi
		;;
	*)
		copy_to_clipboard "$SCREENSHOT_FULLNAME"
		show_message "$SCREENSHOT_FULLNAME" document-save "Screenshot path"
		if [ ! -z $SWAYSHOT_WL_COPY_FILE ]; then
			show_message "Screenshot was copied to clipboard.\\nFeel free to paste it." "$SCREENSHOT_FULLNAME" "Screenshot image"
		fi
esac
