dmenumount.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. # Gives a dmenu prompt to mount unmounted drives and Android phones. If
  3. # they're in /etc/fstab, they'll be mounted automatically. Otherwise, you'll
  4. # be prompted to give a mountpoint from already existsing directories. If you
  5. # input a novel directory, it will prompt you to create that directory.
  6. getmount() { \
  7. [ -z "$chosen" ] && exit 1
  8. mp="$(find "$1" 2>/dev/null | dmenu -i -p "Type in mount point.")"
  9. [ "$mp" = "" ] && exit 1
  10. if [ ! -d "$mp" ]; then
  11. mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?")
  12. [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
  13. fi
  14. }
  15. mountusb() { \
  16. chosen="$(echo "$usbdrives" | dmenu -i -p "Mount which drive?" | awk '{print $1}')"
  17. sudo -A mount "$chosen" 2>/dev/null && notify-send "💻 USB mounting" "$chosen mounted." && exit 0
  18. alreadymounted=$(lsblk -nrpo "name,type,mountpoint" | awk '$2=="part"&&$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not \\( -path *%s -prune \\) \\ \n",$3}')
  19. getmount "/mnt /media /mount /home -maxdepth 5 -type d $alreadymounted"
  20. partitiontype="$(lsblk -no "fstype" "$chosen")"
  21. case "$partitiontype" in
  22. "vfat") sudo -A mount -t vfat "$chosen" "$mp" -o rw,umask=0000;;
  23. *) sudo -A mount "$chosen" "$mp"; user="$(whoami)"; ug="$(groups | awk '{print $1}')"; sudo -A chown "$user":"$ug" "$mp";;
  24. esac
  25. notify-send "💻 USB mounting" "$chosen mounted to $mp."
  26. }
  27. mountandroid() { \
  28. chosen=$(echo "$anddrives" | dmenu -i -p "Which Android device?" | cut -d : -f 1)
  29. getmount "$HOME -maxdepth 3 -type d"
  30. simple-mtpfs --device "$chosen" "$mp"
  31. echo "OK" | dmenu -i -p "Tap Allow on your phone if it asks for permission and then press enter"
  32. simple-mtpfs --device "$chosen" "$mp"
  33. notify-send "🤖 Android Mounting" "Android device mounted to $mp."
  34. }
  35. asktype() { \
  36. case $(printf "USB\\nAndroid" | dmenu -i -p "Mount a USB drive or Android device?") in
  37. USB) mountusb ;;
  38. Android) mountandroid ;;
  39. esac
  40. }
  41. anddrives=$(simple-mtpfs -l 2>/dev/null)
  42. usbdrives="$(lsblk -rpo "name,type,size,mountpoint" | awk '$2=="part"&&$4==""{printf "%s (%s)\n",$1,$3}')"
  43. if [ -z "$usbdrives" ]; then
  44. [ -z "$anddrives" ] && echo "No USB drive or Android device detected" && exit
  45. echo "Android device(s) detected."
  46. mountandroid
  47. else
  48. if [ -z "$anddrives" ]; then
  49. echo "USB drive(s) detected."
  50. mountusb
  51. else
  52. echo "Mountable USB drive(s) and Android device(s) detected."
  53. asktype
  54. fi
  55. fi