#!/usr/bin/env bash
#
# wl-shot - small helper script to take screenshots in sway
# dependencies: grim, slurp

set -e

SAVEDIR="${HOME}/Pictures/Screenshots"
mkdir -p -- "${SAVEDIR}"
TIMESTAMP=$(date +'%Y-%m-%d-%H-%M-%S')
FILENAME="${SAVEDIR}/${TIMESTAMP}_screenshot.png"

fullscreen() {
  grim "${FILENAME}"
  notify-send -t 5000 -i "${FILENAME}" "Screenshot" "File saved as ${FILENAME}"
  wl-copy <"${FILENAME}"
  pw-play "${HOME}/.local/share/Sounds/camera-shutter.oga"
}

region() {
  # shellcheck disable=SC2312
  grim -g "$(slurp)" "${FILENAME}"
  notify-send -t 5000 -i "${FILENAME}" "Screenshot" "File saved as ${FILENAME}"
  wl-copy <"${FILENAME}"
  pw-play "${HOME}/.local/share/Sounds/camera-shutter.oga"
}

region_edit() {
  # shellcheck disable=SC2312
  grim -g "$(slurp)" - | swappy -f -
}

timer() {
  sleep 5
  grim "${FILENAME}"
  notify-send -t 5000 -i "${FILENAME}" "Screenshot" "File saved as ${FILENAME}"
  wl-copy <"${FILENAME}"
  pw-play "${HOME}/.local/share/Sounds/camera-shutter.oga"
}

print_help() {
  echo "Usage: wl-shot [options]"
  echo "Options:"
  echo "  -f,  --fullscreen           Capture the fullscreen."
  echo "  -r,  --region               Select a region to capture."
  echo "  -t,  --timer                Capture fullscreen after a 3-second delay."
  echo "  -h,  --help                 Show this help message."
}

while [ $# -gt 0 ]; do
  case "$1" in
    -f | --fullscreen) fullscreen ;;
    -r | --region) region ;;
    -re | --region-edit) region_edit ;;
    -t | --timer) timer ;;
    -h | --help) print_help ;;
    *)
      echo "Unknown option: $1"
      print_help
      exit 1
      ;;
  esac
  shift
done
