summaryrefslogtreecommitdiff
path: root/.local/bin/mountdisks
blob: edaa76a4c95a9840ea5eebe4ff00f5bf0e78e554 (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
#!/bin/sh

# Configuration
MOUNT_BASE="/run/media/$USER"
LINK_DIR="$HOME/mnt"
mkdir -p "$LINK_DIR"

# List partitions that are NOT mounted
list_unmounted() {
    lsblk -npP -o NAME,SIZE,MOUNTPOINT,TYPE | while read -r line; do
        eval "$line"
        if [ "$TYPE" = "part" ] && [ -z "$MOUNTPOINT" ]; then
            echo "$NAME ($SIZE)"
        fi
    done
}

# List partitions mounted within the udisks2 path
list_mounted() {
    lsblk -npP -o NAME,SIZE,MOUNTPOINT,TYPE | while read -r line; do
        eval "$line"
        if echo "$MOUNTPOINT" | grep -q "$MOUNT_BASE"; then
            echo "$NAME ($SIZE) -> $MOUNTPOINT"
        fi
    done
}

# Main menu via dmenu
action=$(printf "Mount\nUnmount" | dmenu -i -p "Storage:")

case "$action" in
    Mount)
        selection=$(list_unmounted | dmenu -i -l 10 -p "Select device:")
        [ -z "$selection" ] && exit
        
        dev=$(echo "$selection" | cut -d' ' -f1)
        
        output=$(udisksctl mount -b "$dev")
        if [ $? -eq 0 ]; then
            # Extract mount path from udisksctl output
            mnt_path=$(echo "$output" | grep -oP '/run/media/[^ ]+')
            label=$(basename "$mnt_path")
            
            # Create symbolic link for easier access
            ln -sfn "$mnt_path" "$LINK_DIR/$label"
            notify-send "Drive Mounted" "Label: $label\nLink: $LINK_DIR/$label" -i drive-removable-media
        fi
        ;;
        
    Unmount)
        selection=$(list_mounted | dmenu -i -l 10 -p "Select to unmount:")
        [ -z "$selection" ] && exit
        
        dev=$(echo "$selection" | cut -d' ' -f1)
        mnt_path=$(echo "$selection" | awk '{print $NF}')
        label=$(basename "$mnt_path")
        
        if udisksctl unmount -b "$dev"; then
            # Remove symlink and attempt to clean up the mount directory if empty
            rm -f "$LINK_DIR/$label"
            rmdir "$LINK_DIR" 2>/dev/null
            
            notify-send "Drive Unmounted" "$label has been safely removed." -i drive-removable-media
        fi
        ;;
esac