#!/bin/sh

# ┌─┐┌─┐┬─┐┌─┐┌─┐┌┐┌┌─┐┬ ┬┌─┐┌┬┐
# └─┐│  ├┬┘├┤ ├┤ │││└─┐├─┤│ │ │ 
# └─┘└─┘┴└─└─┘└─┘┘└┘└─┘┴ ┴└─┘ ┴ 

# author	Sankalp
# github	S4NAKLP

# ┌─┐┌─┐┌┐┌┌─┐┬┌─┐┬ ┬┬─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐
# │  │ ││││├┤ ││ ┬│ │├┬┘├─┤ │ ││ ││││└─┐
# └─┘└─┘┘└┘└  ┴└─┘└─┘┴└─┴ ┴ ┴ ┴└─┘┘└┘└─┘

# Save Screenshots here
screenshotdir=~/media/Pictures/Screenshots
# file name
file=$screenshotdir/$(date '+%y%m%d-%H%M-%S').png
# icon for notification
icon=~/.config/rofi/screenshot/camera.png
# rofi config
roficonfig=~/.config/rofi/screenshot/config.rasi
#make sure you have directory
[ -d "$screenshotdir" ] || mkdir -pv "$screenshotdir"

# ┬─┐┌─┐┌─┐┬
# ├┬┘│ │├┤ │
# ┴└─└─┘└  ┴

area=""
cwin=""
full=""
copy=""
save=""

rofiopt="$area\n$cwin\n$full"
rofi=$(printf "$rofiopt" | rofi -config $roficonfig -dmenu -i )
[ -z "$rofi" ] && exit
option="$save\n$copy"
saveopt=$(printf "$option" | rofi -config $roficonfig -dmenu -i )

case $rofi in
	"$area")
	if [ "$saveopt" = "$copy" ]; then 
	notify-send -i $icon --urgency low 'Select Area'
	maim -u -m 5 -s | xclip -selection clipboard -t image/png && notify-send -i $icon --urgency low 'Screenshot copied' || notify-send -i $icon 'failed to take screenshot'
  elif [ "$saveopt" = "$save" ]; then
	notify-send -i $icon --urgency low 'Select Area'
	maim -u -m 5 -s $file && notify-send -i $icon --urgency low 'Screenshot Taken' || notify-send -i $icon 'failed to take screenshot'
	fi
	;;
	"$cwin")
	if [ "$saveopt" = "$copy" ]; then 
	maim -u -m 5 -i "$(xdotool getactivewindow)" | xclip -selection clipboard -t image/png && notify-send -i $icon --urgency low 'Screenshot copied' || notify-send -i $icon 'failed to take screenshot'
  elif [ "$saveopt" = "$save" ]; then
	maim -u -m 5 -i "$(xdotool getactivewindow)" $file && notify-send -i $icon --urgency low 'Screenshot Taken' || notify-send -i $icon 'failed to take screenshot'
	fi
	;;
	"$full")
	if [ "$saveopt" = "$copy" ]; then 
    sleep 2
	maim -u -m 5 | xclip -selection clipboard -t image/png && notify-send -i $icon --urgency low 'Screenshot copied' || notify-send -i $icon 'failed to take screenshot'
  elif [ "$saveopt" = "$save" ]; then
    sleep 2
	maim -u -m 5 $file && notify-send -i $icon --urgency low 'Screenshot Taken' || notify-send -i $icon 'failed to take screenshot'
	fi
	;;
esac
