| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | #!/usr/bin/env bash | 
					
						
							|  |  |  | shopt -s nullglob | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-13 13:52:26 +03:00
										 |  |  | # Defaults for XDG | 
					
						
							|  |  |  | if ! [[ "$XDG_RUNTIME_DIR" ]]; then | 
					
						
							|  |  |  | 	XDG_RUNTIME_DIR="/run/user/$UID" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | msg() { printf '%s\n' "$*"; } | 
					
						
							|  |  |  | err() { echo "$*" >&2; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-06 07:18:59 +03:00
										 |  |  | 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' | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 23:50:33 +03:00
										 |  |  | upload() { | 
					
						
							| 
									
										
										
										
											2019-05-10 00:18:29 +03:00
										 |  |  | 	declare file=$1; shift | 
					
						
							| 
									
										
										
										
											2019-05-09 23:50:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 23:55:03 +03:00
										 |  |  | 	upload_output=$( ufw -R "$@" "$file" ) | 
					
						
							| 
									
										
										
										
											2019-05-09 23:50:33 +03:00
										 |  |  | 	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" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | main() { | 
					
						
							|  |  |  | 	while (( $# )); do | 
					
						
							|  |  |  | 		case "$1" in | 
					
						
							|  |  |  | 			(--help|-h) usage; return 0;; | 
					
						
							| 
									
										
										
										
											2015-12-20 15:46:24 +03:00
										 |  |  | 			(--notify|-n) flag_enable_notifications=1;; | 
					
						
							| 
									
										
										
										
											2019-05-09 23:31:25 +03:00
										 |  |  | 			(--clipboard|-c) flag_enable_clipboard=1;; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 15:30:35 +03:00
										 |  |  | 			(--workdir|-d) cfg_workdir+=( "$2" ); shift;; | 
					
						
							| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 15:30:35 +03:00
										 |  |  | 			(--) shift; break;; | 
					
						
							|  |  |  | 			(-*) err "Unknown key: $1"; usage; return 1;; | 
					
						
							| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | 			(*) break;; | 
					
						
							|  |  |  | 		esac | 
					
						
							|  |  |  | 		shift | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 15:28:03 +03:00
										 |  |  | 	[[ $cfg_workdir ]] || cfg_workdir="$XDG_RUNTIME_DIR/ufwd" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-20 15:46:24 +03:00
										 |  |  | 	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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 15:28:03 +03:00
										 |  |  | 	for p in "${cfg_workdir[@]}"; do | 
					
						
							|  |  |  | 		[[ -d "$p" ]] || return 1 | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printf 'Working in:\n' | 
					
						
							|  |  |  | 	printf ' - %s\n' "${cfg_workdir[@]}" | 
					
						
							| 
									
										
										
										
											2019-05-09 23:31:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 15:28:03 +03:00
										 |  |  | 	while read -r d e f; do | 
					
						
							|  |  |  | 		upload "$d$f" "$@" & | 
					
						
							|  |  |  | 	done < <( inotifywait -m -e close_write "${cfg_workdir[@]}" ) | 
					
						
							| 
									
										
										
										
											2015-04-17 00:42:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main "$@" |