#!/usr/bin/env bash # Public Domain / CC0 # Original written by Nekohime - Nekohime.net # Extra features were added by cr1901 - https://github.com/cr1901 uuid=$(uuidgen -r) tmp_file="$(mktemp)" edit=0 paste_clip=0 keep_fn=0 delete_file="" paste_store="paste" dest_file="$uuid.txt" . $HOME/.vps-store while getopts ":hecd:ks" x; do case $x in h) echo "$0 [-e] [-c] [-d ID] [-k] [-s] [file]" echo " -e open \$EDITOR to create file" echo " -c copy final URL to clipboard" echo " -d delete file with ID" echo " -k keep filename" echo " -s keep filename and copy file to store/ directory" echo "" echo " -d overrides all options except -s and ignores [file]" echo " -s/-k overrides -e and requires [file]" echo " -e overrides and ignores reading from [file] or stdin" echo "" echo "env vars in ~/.vps-store:" echo " VPS_STORE_SSH: Destination server" echo " VPS_STORE_DIR: Path on destination server" echo " VPS_STORE_URL: URL of website corresponding to \$VPS_STORE_DIR" exit ;; e) edit=1 ;; c) paste_clip=1 ;; d) delete_file="$OPTARG" ;; k) # Preserve input filename. keep_fn=1 ;; s) # Preserve input filename, copy directly to store. keep_fn=1 paste_store="store" ;; *) echo "Invalid flag detected." ;; esac done shift $((OPTIND - 1)) # Check for invalid conditions. If were are deleting a file, the script # will end before invalid conditions. if [ "$keep_fn" -ne "0" ] && [ -z "$delete_file" ]; then if [ -n "$1" ]; then # Editing does not make sense if we're keeping the filename. edit=0 dest_file=$(basename "$1") else echo "-s/-k options require a filename" exit 1 fi fi # delete mode if [ -n "$delete_file" ]; then # shellcheck disable=SC2029 ssh "$VPS_STORE_SSH" "rm $VPS_STORE_DIR/$paste_store/$delete_file" exit fi # paste mode if [ "$edit" -ne "0" ]; then "$EDITOR" "$tmp_file" file="$tmp_file" elif [ -n "$1" ]; then file="$1" else cat > "$tmp_file" file="$tmp_file" fi # Don't modify files that exist on your hard disk already. if [ -z "$1" ]; then chmod 664 "$file" fi rsync -lptgoDH "$file" "$VPS_STORE_SSH:$VPS_STORE_DIR/$paste_store/$dest_file" URL="$VPS_STORE_URL/$paste_store/$dest_file" # get URL and cleanup if [ "$paste_clip" -ne "0" ]; then if printf '%s' "$(uname -s)" | grep -q "MINGW64_NT"; then echo "$URL" > /dev/clipboard else echo "$URL" | xclip -sel clip echo "$URL" | xclip fi fi echo "$URL" rm "$tmp_file"