| 1 | #!/bin/sh |
| 2 | |
| 3 | # A dmenu prompt to unmount drives. |
| 4 | # Provides you with mounted partitions, select one to unmount. |
| 5 | # Drives mounted at /, /boot and /home will not be options to unmount. |
| 6 | |
| 7 | unmountusb() { |
| 8 | [ -z "$drives" ] && exit |
| 9 | chosen="$(echo "$drives" | dmenu -i -p "Unmount which drive?")" || exit 1 |
| 10 | chosen="$(echo "$chosen" | awk '{print $1}')" |
| 11 | [ -z "$chosen" ] && exit |
| 12 | sudo -A umount "$chosen" && notify-send "💻 USB unmounting" "$chosen unmounted." |
| 13 | } |
| 14 | |
| 15 | unmountandroid() { \ |
| 16 | chosen="$(awk '/simple-mtpfs/ {print $2}' /etc/mtab | dmenu -i -p "Unmount which device?")" || exit 1 |
| 17 | [ -z "$chosen" ] && exit |
| 18 | sudo -A umount -l "$chosen" && notify-send "🤖 Android unmounting" "$chosen unmounted." |
| 19 | } |
| 20 | |
| 21 | asktype() { \ |
| 22 | choice="$(printf "USB\\nAndroid" | dmenu -i -p "Unmount a USB drive or Android device?")" || exit 1 |
| 23 | case "$choice" in |
| 24 | USB) unmountusb ;; |
| 25 | Android) unmountandroid ;; |
| 26 | esac |
| 27 | } |
| 28 | |
| 29 | drives=$(lsblk -nrpo "name,type,size,mountpoint,label" | awk -F':' '{gsub(/ /,":")}$4!~/\/boot|\/efi|\/home$|SWAP/&&length($4)>1{printf "%s (%s) %s\n",$4,$3,$5}') |
| 30 | |
| 31 | if ! grep simple-mtpfs /etc/mtab; then |
| 32 | [ -z "$drives" ] && echo "No drives to unmount." && exit |
| 33 | echo "Unmountable USB drive detected." |
| 34 | unmountusb |
| 35 | else |
| 36 | if [ -z "$drives" ] |
| 37 | then |
| 38 | echo "Unmountable Android device detected." |
| 39 | unmountandroid |
| 40 | else |
| 41 | echo "Unmountable USB drive(s) and Android device(s) detected." |
| 42 | asktype |
| 43 | fi |
| 44 | fi |