#!/usr/bin/env bash set -euo pipefail err() { printf "%s\n" "$*" >&2 } NAME='sharpshot' SELF="${0:-$NAME}" VERSION='0.0.1' OUTPUT_FILE=${SHARPSHOT_OUTPUT_FILE:-/tmp/sharpshot.png} COMMAND=${SHARPSHOT_COMMAND:-spectacle -b -o ${OUTPUT_FILE}} COPY_COMMAND=${SHARPSHOT_COPY_COMMAND:-wl-copy} FULL_SWITCH=${SHARPSHOT_FULL_SWITCH:--f} ACTIVE_SWITCH=${SHARPSHOT_ACTIVE_SWITCH:--a} REGION_SWITCH=${SHARPSHOT_REGION_SWITCH:--r} VERBOSE=false DRY=false verbose() { if $VERBOSE; then printf "%s\n" "$*" fi } full() { verbose 'Capturing full desktop' if $DRY; then echo "DRY-RUN, would execute: '$COMMAND $FULL_SWITCH'" else $COMMAND "$FULL_SWITCH" & fi } active() { verbose 'Capturing active window' if $DRY; then echo "DRY-RUN, would execute: '$COMMAND $ACTIVE_SWITCH'" else $COMMAND "$ACTIVE_SWITCH" & fi } region() { verbose 'Capturing region' if $DRY; then echo "DRY-RUN, would execute: '$COMMAND $REGION_SWITCH'" else $COMMAND "$REGION_SWITCH" & fi } printversion() { echo "$NAME v$VERSION" } usage() { echo 'Usage:' echo " $SELF [options] [action]" } help() { cat < Set the output path to write to -c Set the base command to use -f Set the switch to add to the base command for full screen capture -a Set the switch to add to the base command for active window capture -r Set the switch to add to the base command for region capture -v Enable verbose logging -d Enable dry-run -h Display this help -V Display version EOF } while getopts ':hvdVo:c:f:a:r:' option; do case "$option" in v) VERBOSE=true ;; d) DRY=true ;; V) printversion exit ;; h) help exit ;; o) OUTPUT_FILE="$OPTARG" ;; c) COMMAND="$OPTARG" ;; f) FULL_SWITCH="$OPTARG" ;; a) ACTIVE_SWITCH="$OPTARG" ;; r) REGION_SWITCH="$OPTARG" ;; \?) err "Invalid option '$OPTARG'" exit 1 ;; :) err "Missing value for option '$OPTARG'" exit 1 ;; *) usage exit 1 ;; esac done shift $((OPTIND - 1)) ACTION="${*:-full}" $DRY && echo "$NAME running in dry-run mode" verbose "Outputting to $OUTPUT_FILE" verbose "Base command: $COMMAND" verbose "Full switch: $FULL_SWITCH" verbose "Active switch: $ACTIVE_SWITCH" verbose "Region switch: $REGION_SWITCH" verbose "Action: $ACTION" case "$ACTION" in a*) active ;; r*) region ;; f*) full ;; *) err "Unknown capture mode '$ACTION'" exit 1 ;; esac