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