102 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env bash
 | 
						|
shopt -s nullglob
 | 
						|
 | 
						|
# Defaults for XDG
 | 
						|
if ! [[ "$XDG_RUNTIME_DIR" ]]; then
 | 
						|
	XDG_RUNTIME_DIR="/run/user/$UID"
 | 
						|
fi
 | 
						|
 | 
						|
msg() { printf '%s\n' "$*"; }
 | 
						|
err() { echo "$*" >&2; }
 | 
						|
 | 
						|
usage() {
 | 
						|
	printf 'Usage: ufwd [-hn] [-d workdir] -D [check delay]\n'
 | 
						|
	printf '   -h                      # Show this message.\n'
 | 
						|
	printf '   -n                      # Enable notifications with notify-send. Must be installed.\n'
 | 
						|
	printf '   -d [path]               # Set the dir that is to be watched.\n'
 | 
						|
	printf '   -s                      # Push the file url into the clipboard.\n'
 | 
						|
	printf '   --magick <type:format>  # Convert files of `type` into `format` with magick.\n'
 | 
						|
}
 | 
						|
 | 
						|
upload() {
 | 
						|
	declare file=$1 file_type _type _target upload_output upload_return
 | 
						|
	shift
 | 
						|
 | 
						|
	if [[ $cfg_magick ]]; then
 | 
						|
		file_type=$(file -ib "$file")
 | 
						|
		file_type="${file_type%;*}"
 | 
						|
 | 
						|
		for x in "${cfg_magick[@]}"; do
 | 
						|
			_type="${x%:*}"
 | 
						|
			_target="${x#*:}"
 | 
						|
 | 
						|
			[[ $_type == "$file_type" ]] && {
 | 
						|
				magick "$file" "${file%.*}.$_target"
 | 
						|
				rm -v "$file"
 | 
						|
 | 
						|
				exit 0 # The next instance of the script will pick the file up.
 | 
						|
			}
 | 
						|
		done
 | 
						|
	fi
 | 
						|
 | 
						|
	upload_output=$( ufw -R "$@" "$file" )
 | 
						|
	upload_return=$?
 | 
						|
 | 
						|
	if (( flag_enable_notifications )); then
 | 
						|
		if (( $upload_return )); then
 | 
						|
			notify-send 'ufwd' "File upload failed: $upload_output"
 | 
						|
		else
 | 
						|
			notify-send 'ufwd' "File uploaded: $upload_output"
 | 
						|
		fi
 | 
						|
	fi
 | 
						|
 | 
						|
	if (( flag_enable_clipboard )); then
 | 
						|
		read -r fl <<< "$upload_output"
 | 
						|
		xclip <<< "$fl"
 | 
						|
	fi
 | 
						|
 | 
						|
	printf '%s\n' "$upload_output"
 | 
						|
}
 | 
						|
 | 
						|
main() {
 | 
						|
	while (( $# )); do
 | 
						|
		case "$1" in
 | 
						|
			(--help|-h) usage; return 0;;
 | 
						|
			(--notify|-n) flag_enable_notifications=1;;
 | 
						|
			(--clipboard|-c) flag_enable_clipboard=1;;
 | 
						|
 | 
						|
			(--workdir|-d) cfg_workdir+=( "$2" ); shift;;
 | 
						|
			(--magick) cfg_magick+=( "$2" ); shift;;
 | 
						|
 | 
						|
			(--) shift; break;;
 | 
						|
			(-*) err "Unknown key: $1"; usage; return 1;;
 | 
						|
			(*) break;;
 | 
						|
		esac
 | 
						|
		shift
 | 
						|
	done
 | 
						|
 | 
						|
	[[ $cfg_workdir ]] || cfg_workdir="$XDG_RUNTIME_DIR/ufwd"
 | 
						|
 | 
						|
	if (( flag_enable_notifications )); then
 | 
						|
		if type -P notify-send &>/dev/null; then
 | 
						|
			msg "Found notify-send."
 | 
						|
		else
 | 
						|
			err "notify-send not found in PATH, disabling notifications."
 | 
						|
			flag_enable_notifications=0
 | 
						|
		fi
 | 
						|
	fi
 | 
						|
 | 
						|
	for p in "${cfg_workdir[@]}"; do
 | 
						|
		[[ -d "$p" ]] || return 1
 | 
						|
	done
 | 
						|
 | 
						|
	printf 'Working in:\n'
 | 
						|
	printf ' - %s\n' "${cfg_workdir[@]}"
 | 
						|
 | 
						|
	while read -r d e f; do
 | 
						|
		upload "$d$f" "$@" &
 | 
						|
	done < <( inotifywait -m -e close_write "${cfg_workdir[@]}" )
 | 
						|
}
 | 
						|
 | 
						|
main "$@"
 |