#!/bin/sh
. ~/.config/red.conf

sflag=0
oflag=0
cflag=0
while getopts soc name
do
	case $name in
	c)
		cflag=1
		;;
	s)
		sflag=1
		;;
	o)
		oflag=1
		;;
	?)
		echo "Usage: red [-s] <files...>"
		exit 1
		;;
	esac
done

shift $(($OPTIND - 1))
files=$*

upload() {
	url=$(curl -s --oauth2-bearer "$API_KEY" \
		-Ffiles=@"$1" \
		https://redacted.moe/api/upload \
		| jq -r ".[].direct_url")
	if [ $oflag -eq 1 ]
	then
		xdg-open $url
	fi
	echo $url
}

if [ $sflag -eq 1 ]
then
	out=$HOME/pictures/screenshots/"$(date -Is)".png
	grim -g "$(slurp -db '#44444480')" "$out"
	upload "$out"
fi

for file in $*
do
	url=$(upload "$file")
	echo $url
	if [ $cflag -eq 1 ]
	then
		wl-copy $url
	fi
done
