#!/bin/bash

## sshot - Take a screenshot

if [[ "$1" == "--partial"  ]]; then
    ARG_PARTIAL=1
elif [[ "$1" == "--pin" ]]; then
    ARG_PARTIAL=1
    ARG_PIN=1
elif [[ "$1" == "--window" ]]; then
    ARG_WINDOW=1
fi

file="$HOME/Pictures/screenshots/$(date --iso-8601=seconds).png"
mkdir -p "$(dirname $file)" 2>/dev/null

if [[ -n "$ARG_PARTIAL" ]]; then
    # prompt the user for the area to take a screenshot from
    geometry="$(slurp)"
    if [ -z "$geometry" ]; then
        exit 1
    fi
elif [[ -n $ARG_WINDOW  ]]; then
    # get the active window geometry
    geometry="$(get-focused-window-geometry)"
fi

# take a screenshot
grim -g "$geometry" -- "$file"

# place to image in the clipboard
wl-copy < "$file"

if [[ -n "$ARG_PIN" ]]; then
    # pin the screenshot
    feh --title "Pinned screenshot" --geometry "$(echo -n "$geometry" | sed -E 's/([0-9]+),([0-9]+) ([0-9]+)x([0-9]+)/\3x\4+\1+\2/')" "$file" &
fi

# print the file name
echo "$file"