#!/usr/bin/env sh

tool="grim"

now=$(date '+%F_%H-%M-%S')
now_iso=$(date -Is)
expire_date=$(date -Is -d "+1hour")
rclone_remote="s3-serguzim-me:public/screenshots"
screenshot_host="https://s3.serguzim.me/public/screenshots"
screenshot_path="/tmp/screenshot_$now.webp"
file="$now.webp"

clip_image()
{
	if [ -n "$WAYLAND_DISPLAY" ]
	then
		wl-copy -t image/png < "$screenshot_path"
	else
		xclip -t image/png "$screenshot_path" -selection clipboard
	fi
}

upload_image()
{
	share_url=$(upload_image_immich)
	echo "$share_url"

	if [ -n "$share_url" ]
	then
		notify-send "Screenshot" "Uploaded screenshot"
	else
		notify-send "Screenshot" "Failed to upload screenshot"
	fi

	xdg-open "$share_url"

	# to have link available in clipboard manager
	if [ -n "$WAYLAND_DISPLAY" ]
	then
		wl-copy "$share_url"
		wl-copy -p "$share_url"
	else
		printf '%s' "$share_url" | xclip -selection clipboard
		printf '%s' "$share_url" | xclip -selection primary
	fi
}

upload_image_rclone()
{
	set -e

  rclone copyto "$screenshot_path" "$rclone_remote/$file" >/dev/null
  echo "$screenshot_host/$file"
}

# Immich url to get api key for service account (skip oauth)
# https://my.immich.app/auth/login?autoLaunch=0
upload_image_immich()
{
	set -e

	result_upload=$(curl -sSfL "$IMMICH_INSTANCE_URL/api/assets" \
		-H 'Content-Type: multipart/form-data' \
		-H 'Accept: application/json' \
		-H "x-api-key: $IMMICH_API_KEY_SCREENSHOTS" \
		-F 'deviceId="screenshots"' \
		-F "deviceAssetId=\"screenshots-$screenshot_path\"" \
		-F "fileModifiedAt=\"$now_iso\"" \
		-F "fileCreatedAt=\"$now_iso\"" \
		-F "assetData=@\"$screenshot_path\"")
	asset_id=$(echo "$result_upload" | jq -r '.id')

	result_share=$(curl -sSfL "$IMMICH_INSTANCE_URL/api/shared-links" \
		-H 'Content-Type: application/json' \
		-H 'Accept: application/json' \
		-H "x-api-key: $IMMICH_API_KEY_SCREENSHOTS" \
		-d "{
  		\"allowDownload\": true,
  		\"allowUpload\": false,
  		\"assetIds\": [
    		\"$asset_id\"
  		],
  		\"expiresAt\": \"$expire_date\",
  		\"showMetadata\": true,
  		\"type\": \"INDIVIDUAL\"
		}")
	share_key=$(echo "$result_share" | jq -r '.key')

	#share_url="$IMMICH_INSTANCE_URL/share/$share_key/photos/$asset_id"
	share_url="$IMMICH_INSTANCE_URL/api/assets/$asset_id/original?key=$share_key"
	echo "$share_url"
}

call_screenshot_tool() {
	case $tool in
		flameshot)
			result=$(flameshot gui -r | tee $screenshot_path | wc -c) ;;
		grim)
			result=$(grim -g "$(slurp -c bd93f9)" - | tee $screenshot_path | wc -c) ;;
		*)
			echo "Unknown screenshot tool: $tool"
			return 1 ;;
	esac

	if [ "$result" -gt 0 ]
	then
		return 0
	else
		return 1
	fi
}

main_no_upload()
{
	if call_screenshot_tool
    then
        clip_image
    fi
}

main()
{
    if [ -n "$1" ] && [ -e "$1" ]
    then
        magick "$1" "$screenshot_path"
        upload_image
        clip_image
    else
		if call_screenshot_tool
        then
            upload_image
            clip_image
        fi
    fi
}

case $1 in
    "--no-upload")	main_no_upload ;;
    *)				main "$1" ;;
esac