#!/usr/bin/env sh

# clip -- easy copying to clipboard manager with
# wl-copy / xclip / xsel
#
# clips arguments passed to the clipboard
# or stdin if stdin is passed.
#
# idea ~~stolen~~ creatively borrowed from
# https://github.com/kyazdani42/dotfiles/blob/master/bin/copy

clip() {
	if exist wl-copy; then
		printf "%s" "$1" | wl-copy
	elif exist xclip; then
		echo "$1" | xclip -i -selection clipboard
	elif exist xsel; then
		echo "$1" | xsel -i --clipboard
	else
		printf "No working clipboard program found. Install wl-copy/xclip/xsel and try again."
		exit 1
	fi
}

# if we are in a pipe, read from stdin
if [ ! -t 0 ]; then
	clip "$(cat /dev/stdin)"
	exit 0
fi

# TODO check if $1 is a file, and if it's png or similar, clip that

if [ $# -lt 1 ]; then
	printf "No file argument or stdin passed to clip. Exiting."
	exit 1
fi

if [ "$#" -ge 1 ]; then
	clip "$*"
	exit 0
fi