#!/bin/sh

 ##############################################################################
 #                                                                            #
 # SUSE Paste script                                                          #
 #                                                                            #
 # Copyright (C) 2010-2024 openSUSE Contributors                              #
 # Copyright (C) 2007-2010 by Michal Hrusecky <Michal@Hrusecky.net>           #
 #                                                                            #
 # This program is free software: you can redistribute it and/or modify       #
 # it under the terms of the GNU General Public License as published by       #
 # the Free Software Foundation, either version 3 of the License, or          #
 # (at your option) any later version.                                        #
 #                                                                            #
 # This program is distributed in the hope that it will be useful,            #
 # but WITHOUT ANY WARRANTY; without even the implied warranty of             #
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
 # GNU General Public License for more details.                               #
 #                                                                            #
 # You should have received a copy of the GNU General Public License          #
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
 #                                                                            #
 ##############################################################################

WINDOW=select
API_KEY=""

# Read configuration file

[ -r ~/.susepaste ] && . ~/.susepaste

# Process the command line parameters

while [ "$1" ]; do
	case "x$1" in
	"x--all")
		WINDOW="root"
		shift 1
		;;
	"x-t")
		TITLE="$2"
		shift 2
		;;
	"x-k")
		KEY="$2"
		shift 2
		;;
	"x-n")
		NICK="$2"
		shift 2
		;;
	"x-e")
		EXPIRE="$2"
		shift 2
		;;
	*)
		echo "openSUSE Paste screenshot script"
		echo ""
		echo " usage:"
		echo "   susepaste-screenshot [--all] [-n nick] [-t title] [-e expire] [file]"
		echo ""
		exit 0
		;;
	esac
done

# Key handling

if [ "$KEY" ]; then
	if [ "`expr substr "$KEY" 1 4`" = "KEY:" ]; then
		KEY="`echo "$KEY" | sed 's|^KEY:||'`"
	fi
	API_KEY=" -F api_key=$KEY "
fi

# Defaults if nothing was specified

# Nickname displayed as an author
[ "$NICK"     ] || NICK="`whoami`"
# Time to live for your paste in minutes (for possible values check the documentation)
[ "$EXPIRE"   ] || EXPIRE=60


# Real pasting and getting back the URL of the paste

if [ "xselect" = "x$WINDOW" ]; then
	WINDOW="`LANG=C wmctrl -v -a :SELECT: 2>&1 | sed -n 's|Using\ window:[[:blank:]]*0x\([0-9]*\)|0x\1|p'`"
	[ "$TITLE"    ] || TITLE="`wmctrl -l | sed -n "s|$WINDOW[[:blank:]]\+[^[:blank:]]\+[[:blank:]]\+[^[:blank:]]\+[[:blank:]]\+\(.*\)|\1|p"`"
else
	[ "$TITLE"    ] || TITLE="`whoami`'s paste"
fi

TMP="`mktemp --tmpdir XXXXXXXX.jpg`"
import -window $WINDOW -limit disk 500KiB -compress JPEG "$TMP" > /dev/null 2> /dev/null

URL='https://paste.opensuse.org'

OUTURL="$(
curl    -F "file=@$TMP" -F "title=$TITLE"  -F "expire=$EXPIRE"     \
        -F "name=$NICK" -F "submit=submit" -F "lang=image"         \
        -sLH 'Accept: application/json'                            \
        $API_KEY                                                   \
        $URL                                                       \
        | jq -r '.["human_url"]'                                   \
        )"

rm "$TMP"

# Check the results and inform the user

if [ -n "$OUTURL" ]; then
TEXT="Pasted as:
   $OUTURL"
	if [ -x /usr/bin/xclip ]; then
		echo "$OUTURL" | xclip -selection clipboard
		TEXT="$TEXT
Link is also in your clipboard."
	fi
else
	TEXT="Paste failed :-("
fi
if command -v zenity >/dev/null; then
	zenity --info --title="openSUSE Paste" --text="$TEXT"
else
	echo "$TEXT" | xmessage -file -
fi
