Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

82 lines
1.9 KiB

4 years ago
  1. #!/bin/bash
  2. DMENU=${DMENU:-dmenu}
  3. DEV_LABEL="/dev/disk/by-label/"
  4. TMP="/tmp/dmnt-udevil-$(date +%s)"
  5. trap "rm -f $TMP" EXIT
  6. opt_mount_type=0
  7. opt_ignore_filter=1
  8. opt_notify=1
  9. udevil_cmd="mount"
  10. usage() {
  11. cat <<-EOF
  12. usage: dmenu-udevil [-mudihn]
  13. -m Mount devices
  14. -u Unmount devices
  15. -d Select by device rather than by label
  16. -i Ignore filter and list all devices in /dev (with -d)
  17. -n Pass udevil output to notify-send
  18. -h Print help
  19. EOF
  20. }
  21. dmenu_mnt() {
  22. if [[ $opt_mount_type -eq 1 ]]; then
  23. prompt="$udevil_cmd by-device:"
  24. if [[ $opt_ignore_filter -eq 0 ]]; then
  25. res="$(find /dev -maxdepth 1 -not -type d -name "s[dr]*" -or -name "hd*" | cut -d'/' -f3 | ${DMENU} -p "$prompt")"
  26. else
  27. res="$(find /dev -maxdepth 1 -not -type d | cut -d'/' -f3 | ${DMENU} -p "$prompt")"
  28. fi
  29. path="/dev/$res"
  30. [[ -z $res ]] && echo "Cancelled." && exit
  31. else
  32. prompt="$udevil_cmd by-label:"
  33. res="$(find $DEV_LABEL* | cut -d'/' -f5 | ${DMENU} -p "$prompt")"
  34. path="$DEV_LABEL/$res"
  35. [[ -z $res ]] && echo "Cancelled." && exit
  36. fi
  37. if [ "$udevil_cmd" = "mount" ]; then
  38. udevil $udevil_cmd "$path" "/media/$(whoami)/$res"> "$TMP" 2>&1
  39. msg=$(cat $TMP)
  40. else
  41. udevil --verbose $udevil_cmd "$path" > "$TMP" 2>&1
  42. msg=$(grep "umount: " "$TMP" | cut -c '9-')
  43. fi
  44. exitc=$?
  45. if [[ $opt_notify -eq 1 ]]; then
  46. case $exitc in
  47. 0) urgency="normal";;
  48. *) urgency="critical";;
  49. esac
  50. notify-send -u $urgency "$msg"
  51. else
  52. cat $TMP
  53. fi
  54. }
  55. while getopts ':mudhin' opt; do
  56. case "$opt" in
  57. m) ;;
  58. u) udevil_cmd="umount";;
  59. d) opt_mount_type=1;;
  60. i) opt_ignore_filter=1;;
  61. h) usage && exit;;
  62. n) opt_notify=1;;
  63. /?) echo "Unrecognized command: $OPTARG";;
  64. esac
  65. done
  66. dmenu_mnt && exit