#!/bin/bash
notify() {
    #notify-send -i /usr/share/icons/Adwaita/96x96/devices/drive-harddisk-usb-symbolic.symbolic.png "$@"
    notify-send -i drive-harddisk "$@"
}

read -r block mnt < <(lsblk -ln -o NAME,HOTPLUG,FSTYPE,MOUNTPOINT \
    | awk '$2==1 && $3 !="" { if ($4 != "") {print $1" (unmount)"} else { print $1}}' \
    | rofi -p 'Mount' -dmenu)
 
if [[ -z "$block" ]]
then
    exit 0
fi

if [[ -z "$mnt" ]]
then
    if pmount "$block"
    then
        notify "$block mounted"
        herbstclient spawn kitty -d "/media/$block"
    else
        notify "$block mount failed"
    fi
else
    res="$(pumount "$block" 2>&1)"
    if (( $? == 0 ))
    then
        notify "$block unmounted"
    else
        notify -u critical "$block unmount failed" "$res"
    fi
fi

