Krolyxon
Commit 48fb5ab

Branches

Refs →

dotfiles-x11.localbinmounter

41 lines · 1.7 KB · Bourne Again Shell (bash)

1#!/bin/sh
2
3# Gives a rofi -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
8getmount() { \
9 [ -z "$chosen" ] && exit 1
10 # shellcheck disable=SC2086
11 mp="$(find $1 2>/dev/null | rofi -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" | rofi -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
19mountusb() { \
20 chosen="$(echo "$usbdrives" | rofi -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
34usbdrives="$(lsblk -rpo "name,type,size,mountpoint" | grep 'part\|rom' | awk '$4==""{printf "%s (%s)\n",$1,$3}')"
35
36if [ -z "$usbdrives" ]; then
37 echo "No USB drive detected" && exit
38else
39 echo "USB drive(s) detected."
40 mountusb
41fi