#!/usr/local/bin/andy

set -e NOTIFY_LONG  screenshot
set -e NOTIFY_SHORT `basename $args[0]

func xnotify {
	notify (
		'Screenshot Copied'
		'Screenshot successfully copied to the clipboard'
		-i $filename
	)
}

if test $#args -eq 2 && test $args[1] = '-f' {
	set fflag true
	set args $args[1..]
}

if test $#args -gt 1 {
	echo "Usage: `{basename $args[0]} [-f]" >/dev/stderr
	exit 1
}

set outdir $(XDG_PICTURES_DIR:$HOME/Pictures)/screen
set filebase `date +%F_%T.png
set filename $outdir/$filebase
[ -d $outdir ] || mkdir -p $outdir

if $(fflag:false) {
	grim $filename
} else {
	# TODO: Redirect slurp stderr to /dev/null
	slurp | ifne grim -g - $filename
}

[ -f $filename ] || exit 1

set opt `echo "Copy screenshot\nEdit- and copy screenshot" | { osel || true }
if test $opt = 'Copy screenshot' {
	wl-copy <$filename
	xnotify
} else if test $opt = 'Edit- and copy screenshot' {
	swappy -f $filename -o - | tee >{wl-copy} | sponge $filename
	xnotify
} else {
	notify (
		'Screenshot Saved'
		"The screenshot ‘$filebase’ was successfully saved"
		-i $filename
	)
}