summaryrefslogtreecommitdiff
path: root/keyboard/mounter
blob: 7718cc4321a1494aceb4bb7e97051e320b2ae04f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh
# TODO: add support for mounting discs
sudo=${SUDO:-sudo}
mps=/run/media/$USER # User should have ownership of this directory
notify='notify-send --urgency low'

devexist() { [ -z $1 ] || ([ ! -e "$1" ] && $notify "Device $1 does not exist!") && exit 1; }

mnt() {
	# Select a device with unmounted partitions
	awk_printdisks='
	/^disk/ { diskinfo = $2 " " toupper($3) " Device \"" substr($0, i ? i : i = index($0, $5)) "\" (" $4 ")" } # Fifth field is label since mp is null
	/^part/ && !$4 && diskinfo { print diskinfo; diskinfo = "" } # Fourth field is the mp since type is null
	'
	sed_getdev='s/.*" (\(.*\))$/\/dev\/\1/; s/.* /\/dev\//'
	devexist ${disk=$(lsblk -no TYPE,SIZE,TRAN,KNAME,MOUNTPOINT,MODEL | awk "$awk_printdisks" | dmenu -p 'Disk: ' | sed "$sed_getdev")}

	# Select an unmounted partition
	awk_printparts='/^part\s*[0-9]/ { print $2 " " toupper($3) " Partition " ($5 ? "\"" substr($0, i ? i : i = index($0, $5)) "\" (" $4 ")" : $4) }'
	devexist ${part=$(lsblk -no TYPE,MOUNTPOINT,SIZE,FSTYPE,KNAME,LABEL $disk | awk "$awk_printparts" | dmenu -p 'Mount partition: ' | sed "$sed_getdev")}

	# Create a mountpoint automatically
	awk_mpname='{ print $3 ? substr($0, index($0, $3)) : $1 " " toupper($2) " Volume" }'
	mp="$mps/$(lsblk -no SIZE,FSTYPE,LABEL $part | awk "$awk_mpname")"
	[ -e "$mp" ] && mp="$mp $(lsblk -no PARTUUID $part)"
	$sudo mkdir -p "$mp"

	# Handle filesystem cases
	case `lsblk -no FSTYPE $part` in
		vfat)	opts="-o rw,umask=0000" ;;
		exfat)	opts="-o uid=$(id -u),gid=$(id -g)" ;;
	esac

	# Mount the partition
	mountout="`$sudo mount $part "$mp" $opts 2>&1`" \
		&& $notify "Device Successfully Mounted" "Mounted \"$part\" to \"$mp\"" \
		|| $notify "Error Mounting Device \"$part\" to \"$mp\"" "$mountout"

	# Change permissions
	user=$(whoami) && $doas chown -R $user:$user "$mp"
}

umnt() {
	# Exclude mountpoints from /etc/fstab
	awk_fstabmps='!/$^/ && !/^#/ { gsub(/\//, "\\/"); regex = (regex ? regex "|" : "") $2 "$" } END { print regex }'
	excludere="$(awk "$awk_fstabmps" /etc/fstab)"

	# Select a partition to unmount
	awk_mpname='$2 &&'"!/$excludere/"'{ print $1 " on " substr($0, index($0, $2))  }'
	selpart="$(lsblk -no KNAME,MOUNTPOINT | awk "$awk_mpname" | dmenu -p 'Unmount Partition: ')" || exit 1
	devexist ${part=$(echo "$selpart" | sed 's/^/\/dev\//; s/ .*//')}
	mp="$(echo "$selpart" | cut -d' ' -f3-)"

	# Unmount the partition
	umountout="`$sudo umount -A $part`" \
		&& $notify "Device Successfully Unmounted" "Unmounted \"$part\" from \"$mp\"" \
		|| $notify "Error Unmounting Device \"$part\" from \"$mp\"" "$umountout"

	# Remove mp directory if it is in mps
	[ "$(dirname "$mp")" = $mps ] && [ ! "$(ls -A "$mp")" ] && rm -rf "$mp"
}

case $1 in
	-m) mnt ;;
	-u) umnt ;;
	*) echo "Usage: $(basename "$0") [-m|-u]\n\nOPTIONS:\n  -m\tMount mode\n  -u\tUnmount mode"; exit 2 ;;
esac