#!/bin/sh . "lib_common.sh" . "lib_handle.sh" help() { cat << EOF ${progname}: print the clipboard content options: -s Do not send notifications -c [text] Copy text to clipboard -f [file] Copy content of file into clipboard -i Copy standart input into clipboard -h Print this message and exit EOF exit 0 } clipboard_handle check_program if [ "${#}" -eq 0 ]; then clipboard_handle get-clipboard exit $? fi read_stdin=0 silent=0 while getopts "f:c:sih" option; do case "${option}" in f) filepath="${OPTARG}" ;; c) input="${OPTARG}" ;; s) silent=1 ;; i) read_stdin=1 ;; h) help ;; *) invalid_use -h ;; esac done shift $((OPTIND - 1)) [ "${#}" -ne 0 ] && invalid_use if [ -n "${filepath}" ]; then mime_type=$(file_handle get-mime_type "$filepath") || { err "Failed to detect mime type"; exit 1;} clipboard_handle file "${mime_type}" "${filepath}" status=$? else [ -z "$input" ] && [ "$read_stdin" -eq 1 ] && input=$(cat) [ -z "$input" ] && err "Empty input" printf '%s' "${input}" | clipboard_handle text status=$? fi if [ "${silent}" -eq 0 ]; then if [ "${status}" -eq 0 ]; then if [ -n "${filepath}" ]; then notify_handle "Copied file: ${filepath}" else notify_handle "Copied text" fi else if [ -n "${filepath}" ]; then notify_handle "Failed to copy file: ${filepath}" else notify_handle "Failed to copy text" fi fi fi