#!/bin/sh

# Take a screenshot with the configured program
# Depends: rofi and scrot / maim, xdotool / magick, xdotool / grim, slurp, jq

# User-config---------------------------

# scrot / maim / magick import / grim
# >>> session=xorg
screenshotter="maim"
# <<<
# >>> session=wayland
# screenshotter="grim"
# <<<

# --------------------------------------

if [ "$screenshotter" = "scrot" ]; then
	fullscreen=""
	selection="--select"
	activeWindow="--focused"
	filename="--exec 'mv \$f $CAPTURE'"
elif [ "$screenshotter" = "maim" ]; then
	fullscreen=""
	selection="--select --hidecursor"
	activeWindow="--window \$(xdotool getactivewindow)"
	filename="$CAPTURE/$(date '+%Y-%m-%d-%H%M%S')_maim.png"
elif [ "$screenshotter" = "magick import" ] || [ "$screenshotter" = "import" ]; then
	fullscreen="-window root"
	selection=""
	activeWindow="-window \$(xdotool getactivewindow)"
	filename="$CAPTURE/$(date '+%Y-%m-%d-%H%M%S')_import.png"
elif [ "$screenshotter" = "grim" ]; then
	screenshotter="" # start of command needs to be overwritten
	fullscreen="grim"
	selection="slurp | grim -g -"
	activeWindow="hyprctl -j activewindow | jq -r '\"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])\"' | grim -g -"
	filename="$CAPTURE/$(date '+%Y-%m-%d-%H%M%S')_grim.png"
else
	echo "Printscreen configuration is invalid"
	exit 1
fi

options="\
Selection
Active Window
Fullscreen"

choice="$(echo "$options" | rofi -no-fixed-num-lines -dmenu -i -p "Screenshot")" || exit 0

# Give Rofi menu time to disappear
sleep 0.1

command="$screenshotter"
[ "$choice" = "Selection" ] && command="$command $selection"
[ "$choice" = "Active Window" ] && command="$command $activeWindow"
[ "$choice" = "Fullscreen" ] && command="$command $fullscreen"
command="$command $filename"

eval "$command"