#!/usr/bin/env bash
set -e
set -u

if [ $# -eq 0 ]; then
  if hash pbcopy 2>/dev/null; then
    exec pbcopy
  elif hash wl-copy 2>/dev/null; then
    exec wl-copy
  elif hash xclip 2>/dev/null; then
    exec xclip -selection clipboard
  elif hash putclip 2>/dev/null; then
    exec putclip
  fi
else
  if hash pbcopy 2>/dev/null; then
    exec pbcopy < "$1"
  elif hash wl-copy 2>/dev/null; then
    exec wl-copy < "$1"
  elif hash xclip 2>/dev/null; then
    exec xclip -selection clipboard < "$1"
  elif hash putclip 2>/dev/null; then
    exec putclip < "$1"
  fi
fi

rm -f /tmp/clipboard 2> /dev/null
if [ $# -eq 0 ]; then
  cat > /tmp/clipboard
else
  cat "$1" > /tmp/clipboard
fi
