summary refs log tree commit diff
path: root/bin/mnts
blob: 56eb185a469ff10ed6c5b30dab6047166dea3d32 (plain) (blame)
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
#!/bin/sh
#
# DESCRIPTION:
#  dmenu-based mount/unmount helper
#

[ -n "$DEBUG" ] && set -x
[ "$1" = "-o" ] && open=1

# check requirements
for app in awk jq lsblk notify-send udisksctl swx vifm; do
	if ! command -v "$app" >/dev/null; then
		printf "%s: %s not installed but required\n" "$(basename "$0")" "$app" >&2
		exit 1
	fi
done

export MENU_PROMPT="󰕓 mnts"

mount() {
	line=$1
	path=$(echo "${line}" | awk -F\\t '{print $3}' | xargs)
	mode=$(echo "${line}" | awk -F\\t '{if(length($3)<1) print "mount";else print "unmount";}' | xargs)
	drv=$(echo "${line}" | awk -F\\t '{print $1}' | xargs)
	label=$(echo "${line}" | awk -F\\t '{print $2}' | xargs)
	[ -n "${DEBUG}" ] && printf 'DEBUG: mode:%s, drv:%s, path:%s\n' "${mode}" "${drv}" "${path}"
	logfile=$(mktemp --tmpdir -t "mnts-${label}-XXXXX")
	if udisksctl "${mode}" -b "${drv}" >"${logfile}" 2>&1; then
		if [ "${mode}" = "mount" ]; then
			op=plus
			_op="+"
		else
			op=remove
			_op="-"
		fi
		notify-send -i /usr/share/icons/mdi/scalable/harddisk-${op}.svg -a mnts mnts "$(cat "${logfile}")"
		printf "%s\t%s\t%s\n" "${_op}" "${drv}" "${path}"
		[ -n "${open}" ] && [ "${mode}" = "mount" ] && swx -t -- vifm "${path}"
	else
		notify-send -i /usr/share/icons/mdi/scalable/harddisk.svg -a mnts -u critical -t 30000 mnts "${mode}ing ${label} failed:\n<i>$(cat "${logfile}")</i>"
	fi
	rm "${logfile}"
}

handle() {
	while read -r line; do
		mount "${line}" &
	done
}

lsblk -fJ \
	| jq -r '.. | select(.uuid?!="" and .name?) | select((.name | test("hibiki|nvme") | not) and .fstype?) | [ .name, .label, .mountpoint // "" ] | @tsv' \
	| awk -F'\t' '{printf "/dev/%-10s\t%-15s\t%s\n", $1, $2, $3;}' \
	| menu --list=8 \
	| handle