#!/usr/bin/env sh # x0.at paste client # Vars autocopy=1 # Functions usage () { filename=$(basename "$0") cat <<- EOF x0.at paste client Usage : # Upload a file $ $filename [OPTIONS] file $ cat file.ext | $filename [OPTIONS] # Upload text $ echo "upaste rocks" | up $ up upaste really rocks C-d C-d Options : -h Print this usage -a Disable autocopy in clipboard -t Set extension, without dot EOF } upload () { # Set vars local args local ext local file # If extension is set with -t option, set curl ext if [ -n "$extension" ] then ext="filename=.$extension" fi if [ -z "$1" ] then # If argument is empty, upload stdin local file="file=@-" # If curl ext not set, set it to .txt if [ -z "$ext" ] then ext="filename=.txt" fi else # Else upload filename local file="file=@\"$1\"" fi args="$file;$ext" # Curl call curl -s -F "$args" 'https://x0.at/' } autocopy () { # If xorg or wayland is running if [ -n 'DISPLAY' ] then # If xclip is installed if which xclip >/dev/null 2>&1 then xclip "$1" # Else if wl-clipboard is installed elif which wl-copy >/dev/null 2>&1 then wl-copy -p "$1" fi fi } is_file () { # Test if first argument is an existing file if [ ! -f "$(readlink $1)" ] then echo "File \"$1\" does not exist or is not a regular file" exit 2 fi } main () { # Vars local url # If no piped data if [ -t 0 ] then # If no argument, read stdin and save url if [ -z "$1" ] then url=$(upload) # Force a newline printf "\n" # Else upload argument and save url else # Test if file exists and is a regular file if [ -n "$1" ] then is_file "$1" fi url=$(upload "$1") fi # Else if piped data else # Test if argument exists if [ -n "$1" ] then echo "Warning: ignoring file $1..." fi # Upload and save url url=$(upload) fi # Print url echo "$url" # If autocopy is set if [ "$autocopy" = "1" ] then # Autocopy url autocopy "$url" fi } # Main while getopts "hat:" arg do case $arg in a) AUTOCOPY=0 ;; h) usage exit ;; t) extension=${OPTARG} ;; *) usage exit 1 ;; esac done shift $((OPTIND-1)) main "$@"