#!/usr/bin/env bash

if [ $(hostname) = "syl" ]; then
    if [ $XDG_SESSION_TYPE = "x11" ]; then
        style="laptop"
    else
        style="laptop-rbw-wayland"
    fi
else 
    style="desktop-search"
    #echo "this is not hidpi"
fi

timestamp=$(date +%F-%T)

if [ $XDG_SESSION_TYPE = "x11" ]; then
    picture=$(maim --hidecursor --select /tmp/$timestamp.png)
else
    region=$(slurp)
    picture=$(grim -g "$region" /tmp/$timestamp.png)
fi

declare -a options=(
    "clipboard"
    "filesave"
    "cancel"
)

choice=$(printf '%s\n' "${options[@]}" | rofi -sync -i -theme /home/chris/.config/rofi/launchers-git/$style.rasi -dmenu -p 'Clip or Save')

if [[ "$choice" == "cancel" ]]; then
    rm /tmp/$timestamp.png
    echo "Canceled." && exit 1

elif [ "$choice" == "clipboard" ]; then

    if [ $XDG_SESSION_TYPE = "x11" ]; then
        xclip -selection clipboard -target image/png -i /tmp/$timestamp.png
    else
        wl-copy /tmp/$timestamp.png
    fi
    rm /tmp/$timestamp.png
    echo 'saved to clipboard'
    
elif [ "$choice" == "filesave" ]; then
    mv /tmp/$timestamp.png ~/pics/$timestamp.png
    echo 'saved to ~/Pictures/'$timestamp'.png'

fi
