| 1 | #!/bin/sh |
| 2 | |
| 3 | # Gives a dmenu prompt to mount unmounted drives. 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 | usbdrives="$(lsblk -rpo "name,type,size,mountpoint" | grep 'part\|rom' | awk '$4==""{printf "%s (%s)\n",$1,$3}')" |
| 35 | |
| 36 | if [ -z "$usbdrives" ]; then |
| 37 | echo "No USB drive detected" && exit |
| 38 | else |
| 39 | echo "USB drive(s) detected." |
| 40 | mountusb |
| 41 | fi |