#!/bin/sh

set -e

notify()
{
	notify-send -a ${0##*/} -i "$filename" 'Screenshot copied' \
		'Screenshot successfully copied to the clipboard.'
}

if [ $# -eq 1 -a "$1" = "-f" ]
then
	fflag=true
	shift
fi

if [ $# -ge 1 ]
then
	echo "Usage: ${0##*/} [-f]" >&2
	exit 1
fi

readonly outdir=${XDG_PICTURES_DIR:-$HOME/Pictures}/screen
readonly filename="$outdir/`date +%F_%T.png`"
[ -d "$outdir" ] || mkdir -p "$outdir"

if ${fflag:-false}
then
	grim "$filename"
else
	slurp 2>/dev/null | ifne grim -g - "$filename"
fi

[ -f "$filename" ] || exit 1

opt="`printf 'Copy screenshot\nEdit- and copy screenshot\n' | osel`"

case "$opt" in
'Copy screenshot')
	wl-copy <"$filename"
	notify
	;;
'Edit- and copy screenshot')
	swappy -f "$filename" -o - \
		| pee wl-copy cat \
		| sponge "$filename"
	notify
	;;
esac