#!/bin/sh remote_directory='/home/nginx/html/screenshots' url_template='https://example.org/images/' local_root='/home/kageru/screenshots' date_format='%y/%m' random_chars=5 # Leave empty if not needed. I use this to keep track which machine/user uploaded which screenshot. prefix='k' # put something here that’s configured in your ~/.ssh/config ssh_host='localhost' generate_name() { cat /dev/urandom | tr -dc '0-9a-zA-Z' | head -c "$1" } capture() { if [ -n "$WAYLAND_DISPLAY" ]; then slurp | grim -g - "$1" else maim -suk "$1" fi } clipboard() { if [ -n "$WAYLAND_DISPLAY" ]; then wl-copy "$1" else echo "$1" | xsel -b fi } upload() { scp "$1" "$ssh_host:$2" } exists() { ssh "$ssh_host" "ls \"$1\"" } main() { if [ -n "$1" ]; then ext="$(echo "$1" | awk '{ n=split($0, a, "."); print a[n] }')" if [ "$ext" == "$1" ]; then # file has no extension filename="$prefix$(generate_name $random_chars)" else filename="$prefix$(generate_name $random_chars).$ext" fi local_file="$1" else filename="$prefix$(generate_name $random_chars).png" date_folder="$(date "+$date_format")" local_directory="$local_root/$date_folder" mkdir -p "$local_directory" local_file="$local_directory/$filename" fi capture_and_upload "$local_file" "$remote_directory/$filename" } capture_and_upload() { local_file="$1" remote_file="$2" if [ ! "$(exists "$remote_file")" ]; then # If the file already exists, we’re in file upload mode. # If it doesn’t, capture a screenshot and save it there. if [ ! -f "$local_file" ]; then capture "$local_file" fi upload "$local_file" "$remote_file" full_url="$url_template/$filename" clipboard "$full_url" notify-send "$full_url" else echo "Debug: file $local_file already exists, retrying…" # recurse here to generate a new name and start anew if [ -f "$local_file" ]; then main "$local_file" else main fi fi } main "$1"