ufw-tools/ufwd

88 lines
1.8 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env bash
shopt -s nullglob
2015-10-13 10:52:26 +00:00
# Defaults for XDG
if ! [[ "$XDG_RUNTIME_DIR" ]]; then
XDG_RUNTIME_DIR="/run/user/$UID"
fi
# Defaults
2015-11-19 15:59:09 +00:00
cfg_workdir="$XDG_RUNTIME_DIR/ufwd"
cfg_scan_delay='30'
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 ' -D [sec] # Set the check interval.\n'
}
upload() {
declare file=$1; shift
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;;
2015-12-20 12:46:24 +00:00
(--workdir|-d) cfg_workdir="$2"; shift;;
(--scan-delay|-D) cfg_scan_delay="$2"; shift;;
2015-12-20 12:46:24 +00:00
(--notify|-n) flag_enable_notifications=1;;
(--clipboard|-c) flag_enable_clipboard=1;;
(--) shift; break;;
(-*)
err "Unknown key: $1"
usage
return 1
;;
(*) break;;
esac
shift
done
2015-12-20 12:46:24 +00: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
mkdir -p "$cfg_workdir" || return 1
cd "$cfg_workdir" || return 1
while read -r dir action path; do
upload "$dir$path" "$@" &
done < <( inotifywait -m -e create "$cfg_workdir" )
}
main "$@"