#!/bin/sh
# Shooter wl-paste shim — reads clipboard image from SHOOTER_CLIPBOARD_DIR
# Used by CLI tools that invoke wl-paste to read pasted images

if [ -z "$SHOOTER_CLIPBOARD_DIR" ]; then
  exit 1
fi

# Handle --list-types
for arg in "$@"; do
  if [ "$arg" = "--list-types" ]; then
    if [ -f "$SHOOTER_CLIPBOARD_DIR/image.png" ]; then
      echo "image/png"
      exit 0
    fi
    exit 1
  fi
done

# Parse --type argument
REQUESTED_TYPE=""
while [ $# -gt 0 ]; do
  case "$1" in
    --type)
      REQUESTED_TYPE="$2"
      shift 2
      ;;
    --type=*)
      REQUESTED_TYPE="${1#--type=}"
      shift
      ;;
    *)
      shift
      ;;
  esac
done

# Only emit PNG when requested type is image/png or no type specified
if [ -n "$REQUESTED_TYPE" ] && [ "$REQUESTED_TYPE" != "image/png" ]; then
  exit 1
fi

if [ -f "$SHOOTER_CLIPBOARD_DIR/image.png" ]; then
  cat "$SHOOTER_CLIPBOARD_DIR/image.png"
  exit 0
fi

exit 1
