#!/bin/bash

# read -d '' reads until EOF, preserving trailing newlines
# Unlike $(), it does NOT strip them
IFS= read -r -d '' input

case "$(uname -s)" in
  Darwin)
    command -v pbcopy &>/dev/null \
        || { echo "Error: pbcopy not found" >&2; exit 1; }
    printf '%s' "$input" | pbcopy
    ;;
  Linux)
    if command -v wl-copy &>/dev/null; then
        printf '%s' "$input" | wl-copy
    elif command -v xclip &>/dev/null; then
        printf '%s' "$input" | xclip -selection clipboard
    elif command -v xsel &>/dev/null; then
        printf '%s' "$input" | xsel --clipboard --input
    else
        echo "Error: No clipboard tool found (wl-copy, xclip, xsel)" >&2; exit 1
    fi
    ;;
  *)
    echo "Error: Unsupported OS '$(uname -s)'" >&2; exit 1
    ;;
esac
