summaryrefslogtreecommitdiff
path: root/scripts/.local/bin/dmnt
blob: b840507c0e4be86dfcb8f4604de952883116694d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/zsh

#shopt -s expand_aliases
dmenu="cdmenu"

getmount() { \
	[ -z "$chosen" ] && exit 1
        # shellcheck disable=SC2086
	mp="$(find $1 2>/dev/null | "$dmenu" -l 30 -i -p "Type in mount point.")" || exit 1
	[ "$mp" = "" ] && exit 1
	if [ ! -d "$mp" ]; then
		mkdiryn=$(printf "No\\nYes" | "$dmenu" -i -p "$mp does not exist. Create it?") || exit 1
		[ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
	fi
	}

mountusb() { \
    sudo -A mount "$1" 2>/dev/null && notify-send "USB mounting" "$1 mounted." && exit 0
    alreadymounted=$(lsblk -nrpo "name,type,mountpoint" | awk '$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not ( -path *%s -prune ) ",$3}')
    getmount "/mnt -maxdepth 1 -type d $alreadymounted"
    partitiontype="$(lsblk -no "fstype" "$1")"
    case "$partitiontype" in
        "vfat") sudo -A mount -t vfat "$1" "$mp" -o rw,umask=0000;;
        "exfat") sudo -A mount "$1" "$mp" -o uid="$(id -u)",gid="$(id -g)";;
        *) sudo -A mount "$1" "$mp"; user="$(whoami)"; ug="$(groups | awk '{print $1}')"; sudo -A chown "$user":"$ug" "$mp";;
    esac
    notify-send "USB mounting" "$1 mounted to $mp."
}

mountandroid() { \
    getmount "/mnt -maxdepth 1 -type d"
    jmtpfs -device="$1" "$mp"
    #echo "OK" | "$dmenu" -i -p "Tap Allow on your phone if it asks for permission and then press enter" || exit 1
    #jmtpfs -device="$1" "$mp"
    notify-send "Android mounting" "Android device mounted to $mp."
}

unmountdev() {
    result=$(sudo -A umount "$1" 2>&1)
    if [ -z "$result" ]; then
        notify-send "Unmount device" "$1 unmounted"
    else
        notify-send -u critical -t 4000 "Unmount device" "$result"
    fi
}

selectusb() { \
    chosen=$(lsblk -lpo "vendor,model,name,label,size,mountpoint" | "$dmenu" -l 30 | xargs | cut -d' ' -f 1)
    # if mounted
    if [ ! -z "$(grep $chosen "/etc/mtab")" ] ; then
        choice=$(echo "No\nYes" | "$dmenu" -l 2 -p "Partition is mounted. Unmount?")
        case $choice in
            Yes) unmountdev $chosen ;;
            No) exit ;;
        esac
    else
        mountusb $chosen
    fi
}

selectandroid() { \
    unmounted=$(jmtpfs -l | sed -e '1,2d')
    mounted=$(grep "jmtpfs" "/etc/mtab")

    list="Mounted:\n"
    if [ ! -z "$mounted" ]; then
        list="$list$mounted"
    fi
    list="$list\nUnmounted:\n"
    if [ ! -z "$unmounted" ]; then
        list="$list$unmounted"
    fi

    chosen=$(echo "$list" "$mounted" | "$dmenu" -i -l 30)
    echo "CHOSEN" $chosen
    case $chosen in
        *jmtpfs*) unmountdev $(echo $chosen | cut -d' ' -f 2) ;;
        *) mountandroid $(echo $chosen | awk '{print $1 $2}' | cut -d, -f1-2) ;;
    esac
}

choice="$(printf "USB\\nAndroid" | "$dmenu" -i -p "(Un-)mount a USB drive or Android device?")" || exit 1
case $choice in
    USB) selectusb ;;
    Android) selectandroid ;;
esac