#!/usr/bin/env bash

# Safer shell script with these options
# -e          : exit if a command exits with non-zero status
# -u          : exit if an expanded variable does not exist
# -o pipefail : if a command in a pipeline fail, fail the pipeline
#               (e.g this now fails: false | true)
set -euo pipefail

# Clipboard provider using wl-copy/wl-paste.

[[ -n "$WAYLAND_DISPLAY" ]] || exit 1

command -v wl-copy >/dev/null || exit 1

action="$1"
case "$action" in
  copy)
    # NOTE: Ensure MIME type is set, to ensure wl-copy auto-detection doesn't do something weird
    # that makes the text un-paste-able afterward..
    wl-copy --type text/plain
    ;;
  paste)
    wl-paste
    ;;
  *)
    >&2 echo "ERROR: invalid action '$action', must be copy or paste"
    exit 1
    ;;
esac

# vim:set sw=2:
