#!/usr/bin/env sh
#
# pbcopy - clipboard wrapper: wl-copy xsel xclip

if [ "${XDG_SESSION_TYPE:-}" = "wayland" ]; then
  if command -v wl-copy >/dev/null 2>&1; then
    WAYLAND_DISPLAY=wayland-0 wl-copy --trim-newline "$@"
  else
    echo "wl-clipboard is required" >&2
    exit 1
  fi
elif [ "${XDG_SESSION_TYPE:-}" = "x11" ]; then
  if command -v xsel >/dev/null 2>&1; then
    DISPLAY=:0.0 xsel --clipboard --input "$@"
  elif command -v xclip >/dev/null 2>&1; then
    DISPLAY=:0.0 xclip -selection clipboard "$@"
  else
    echo "xsel or xclip is required" >&2
    exit 1
  fi
elif [ -n "${SSH_CLIENT:-}" ]; then
  # get data either from stdin or from a file
  buf=$(cat -- "$@")
  # printf '%b' "${buf}" | eval "${copy_backend}"
  # shellcheck disable=SC2312
  printf '%b' "${buf}" | printf "\033]52;c;%s\007" "$(base64 <&0)"
  # printf "\033]52;c;%s\007" "$(base64 | tr -d '\n')"
fi
