You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.7 KiB

  1. #!/usr/bin/env bash
  2. shopt -s nullglob
  3. # Defaults for XDG
  4. if ! [[ "$XDG_RUNTIME_DIR" ]]; then
  5. XDG_RUNTIME_DIR="/run/user/$UID"
  6. fi
  7. msg() { printf '%s\n' "$*"; }
  8. err() { echo "$*" >&2; }
  9. usage() {
  10. printf 'Usage: ufwd [-hn] [-d workdir] -D [check delay]\n'
  11. printf ' -h # Show this message.\n'
  12. printf ' -n # Enable notifications with notify-send. Must be installed.\n'
  13. printf ' -d [path] # Set the dir that is to be watched.\n'
  14. }
  15. upload() {
  16. declare file=$1; shift
  17. upload_output=$( ufw -R "$@" "$file" )
  18. upload_return=$?
  19. if (( flag_enable_notifications )); then
  20. if (( $upload_return )); then
  21. notify-send 'ufwd' "File upload failed: $upload_output"
  22. else
  23. notify-send 'ufwd' "File uploaded: $upload_output"
  24. fi
  25. fi
  26. if (( flag_enable_clipboard )); then
  27. read -r fl <<< "$upload_output"
  28. xclip <<< "$fl"
  29. fi
  30. printf '%s\n' "$upload_output"
  31. }
  32. main() {
  33. while (( $# )); do
  34. case "$1" in
  35. (--help|-h) usage; return 0;;
  36. (--notify|-n) flag_enable_notifications=1;;
  37. (--clipboard|-c) flag_enable_clipboard=1;;
  38. (--workdir|-d) cfg_workdir+=( "$2" ); shift;;
  39. (--) shift; break;;
  40. (-*) err "Unknown key: $1"; usage; return 1;;
  41. (*) break;;
  42. esac
  43. shift
  44. done
  45. [[ $cfg_workdir ]] || cfg_workdir="$XDG_RUNTIME_DIR/ufwd"
  46. if (( flag_enable_notifications )); then
  47. if type -P notify-send &>/dev/null; then
  48. msg "Found notify-send."
  49. else
  50. err "notify-send not found in PATH, disabling notifications."
  51. flag_enable_notifications=0
  52. fi
  53. fi
  54. for p in "${cfg_workdir[@]}"; do
  55. [[ -d "$p" ]] || return 1
  56. done
  57. printf 'Working in:\n'
  58. printf ' - %s\n' "${cfg_workdir[@]}"
  59. while read -r d e f; do
  60. upload "$d$f" "$@" &
  61. done < <( inotifywait -m -e close_write "${cfg_workdir[@]}" )
  62. }
  63. main "$@"