aboutsummaryrefslogtreecommitdiff
path: root/dmenuMount.sh
blob: 45250bfac10a0adf473cff0f07a6da88335f3fc7 (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
#!/bin/bash
#       _
#    __| |_ __ ___   ___ _ __  _   _
#   / _` | '_ ` _ \ / _ \ '_ \| | | |
#  | (_| | | | | | |  __/ | | | |_| |
#   \__,_|_| |_| |_|\___|_| |_|\__,_|
#                              _
#  _ __ ___   ___  _   _ _ __ | |_ ___
# | '_ ` _ \ / _ \| | | | '_ \| __/ __|
# | | | | | | (_) | |_| | | | | |_\__ \
# |_| |_| |_|\___/ \__,_|_| |_|\__|___/
#
#
# Gives a dmenu prompt to mount unmounted drives.
# If they're in /etc/fstab, they'll be mounted automatically.
# Otherwise, prompts to give a mountpoint from already existing directories.
# If you input a novel directory, prompts to create that directory.

pgrep -x dmenu && exit

# Exit if no unmounted partition
if ! lsblk -lp | grep -q 'part $'; then
	notify-send --category='moon' 'No unmounted partition available' "Partition table:\n$(lsblk -o 'name,mountpoints,size')"
	exit 0
fi

# Select partition to mount
mountable=""
i=0
while read -r line
do
	i=$((i+1))
	name="$(printf '%s\n' "$line" | awk '{print $1}')"
	size="$(printf '%s\n' "$line" | awk '{print $4}')"
	label="$(lsblk -lpo 'name,label' | grep "$name" | cut -d' ' -f2-)"
	fstype="$(lsblk -lpo 'name,fstype' | grep "$name" | cut -d' ' -f2-)"
	mountable="${mountable}${i}. ${name} (${size}) \"${label}\" [${fstype}]\n"
	#$( echo "$line" | awk '{print $, "(" $2 ")", "\"" $3 "\"", "[" $4 "]"}' )"$'\n'
done <<< "$(lsblk -lp | grep 'part $' )"

lines=$(printf '%s\n' "$mountable" | wc -l)
chosen=$(printf '%s\n' "$mountable" | dmenu -l "$lines" -p "Mount which drive?" | awk '{print $2}')
[ "$chosen" == "" ] && exit 1
sudo mount "$chosen" && pgrep -x dunst && notify-send "$chosen mounted." && exit 0

# Select mount point (reached if previous mount failed, e.g. device not in /etc/fstab)
directories=""
i=0
while read -r line
do
	i=$((i+1))
	directories="$directories$i. $line"$'\n'
done <<< "$(find /mnt "$HOME/mounts" -type d -maxdepth 3 -empty 2>/dev/null)"
lines=$(printf '%s\n' "$directories" | wc -l)
mountpoint=$(printf '%s\n' "$directories" | dmenu -l "$lines" -p "Type in mount point." | awk '{print $2}')
[ "$mountpoint" == "" ] && exit 1
if [[ ! -d "$mountpoint" ]]; then
	prompt.sh "$mountpoint does not exist. Create it?" "sudo mkdir -p $mountpoint"
fi
sudo mount "$chosen" "$mountpoint" && pgrep -x dunst && notify-send "$chosen mounted to $mountpoint."