#!/usr/bin/env bash
set -eo pipefail

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    cat <<EOF
Usage: $(basename "$0") [-h] [file]

Copy content to system clipboard.

Options:
    -h, --help    Show this help message

Arguments:
    file          File to copy (reads from stdin if not specified)

EOF
    exit 0
fi

if hash pbcopy 2>/dev/null; then
    exec pbcopy
elif hash xclip 2>/dev/null; then
    exec xclip -selection clipboard
elif hash putclip 2>/dev/null; then
    exec putclip
else
    rm -f /tmp/clipboard 2> /dev/null
    if [ $# -eq 0 ]; then
        cat > /tmp/clipboard
    else
        cat "$1" > /tmp/clipboard
    fi
fi
