#!/bin/bash

_TIMESTAMP_=$(date +%Y%m%d-%H%M%S-%Z)
_YEAR_=${_TIMESTAMP_:0:4}
_MONTH_=${_TIMESTAMP_:4:2}
_SCREENSHOT_DIR_=${HOME}/Pictures/screenshots/${_YEAR_}-${_MONTH_}
_SCREENSHOT_FILE_=${USER}_${_TIMESTAMP_}.png

show_error () {
  zenity \
    --error \
    --width=300 \
    --title="${0##*/}: ERROR" \
    --text="$1"
}

if [ ! -d "${_SCREENSHOT_DIR_}" ] ; then
  mkdir -m 0700 -p "${_SCREENSHOT_DIR_}"

  if [ $? -ne 0 ] ; then
    show_error "Failed to create directory for screenshot:\n\n${_SCREENSHOT_DIR_}"
    exit 1
  fi
fi

_TYPE_=$1
_OPTIONS_=-m

case "${_TYPE_}" in
  --fullscreen)
    _OPTIONS_="${_OPTIONS_} -f"
  ;;
  --region)
    _OPTIONS_="${_OPTIONS_} -d 3 -r"
  ;;
  --window)
    _OPTIONS_="${_OPTIONS_} -w"
  ;;
  *)
    show_error 'unknown option: '${_TYPE_}
    exit 1
  ;;
esac

/usr/bin/xfce4-screenshooter \
  ${_OPTIONS_} \
  -s "${_SCREENSHOT_DIR_}/${_SCREENSHOT_FILE_}"

