#!/usr/bin/env sh

rclone_remote="s3-serguzim-me:public/screenshots"
screenshot_host="https://s3.serguzim.me/public/screenshots"
path="/tmp/screenshot.webp"
file="$(date '+%F_%H-%M-%S').webp"

clip_image()
{
    xclip -t image/png "$1" -selection clipboard
}

upload_image()
{
	# to have link available in clipboard manager
    printf '%s' "$screenshot_host/$file" | xclip -selection clipboard
    printf '%s' "$screenshot_host/$file" | xclip -selection primary
    rclone copyto "$1" "$rclone_remote/$file"
    notify-send -a "Screenshot" "Uploaded screenshot to s3"
}

main_no_upload()
{
    if [ "$(flameshot gui -r | tee $path | wc -c)" -gt 0 ]
    then
        clip_image $path
    fi
}

main()
{
    if [ -n "$1" ] && [ -e "$1" ]
    then
        convert "$1" "$path"
        upload_image "$path"
        clip_image "$path"
    else
        if [ "$(flameshot gui -r | tee $path | wc -c)" -gt 0 ]
        then
            upload_image "$path"
            clip_image "$path"
        fi
    fi
}

case $1 in
    no_upload)   main_no_upload ;;
    *)           main "$1" ;;
esac