#!/bin/bash

COLS="name,type,size,mountpoint"

if [[ "$1" == "unmount" ]]
then
	drives="$(lsblk -rpo "$COLS" | awk '$2=="part"&&$4!=""{printf "%s (%s)\n",$1,$3}')"
	[ -z "$drives" ] && exit 1
	chosen="$(echo "$drives" | dmenu -i -l 10 -m 0 -fn 'Inconsolata:size=10' -nb '#222222' -nf '#bbbbbb' -sb '#005577' -sf '#eeeeee' -p 'Drive to unmount: ' | awk '{print $1}')"
	[ -z "$chosen" ] && exit 1
	umount $chosen && notify-send "$chosen umounted" || notfiy-send "Error unmounting $chosen"
else
	drives="$(lsblk -rpo "$COLS" | awk '$2=="part"&&$4==""{printf "%s (%s)\n",$1,$3}')"
	[ -z "$drives" ] && exit 1
	chosen="$(echo "$drives" | dmenu -i -l 10 -m 0 -fn 'Inconsolata:size=10' -nb '#222222' -nf '#bbbbbb' -sb '#005577' -sf '#eeeeee' -p 'Drive to mount: ' | awk '{print $1}')"
	[ -z "$chosen" ] && exit 1
	mount "$chosen" && exit 0
	mp="$(find /mnt /media /mount /home -type d -maxdepth 1 2>/dev/null | dmenu -i -l 10 -m 0 -fn 'Inconsolata:size=10' -nb '#222222' -nf '#bbbbbb' -sb '#005577' -sf '#eeeeee' -p 'Mount point: ' | awk '{print $1}')"
	[ "$mp" = "" ] && exit 1
	if [ ! -d "$mp" ]; then
		mkdiryn=$(printf "No\\nYes" | dmenu -i -m 0 -fn 'Inconsolata:size=10' -nb '#222222' -nf '#bbbbbb' -sb '#005577' -sf '#eeeeee' -p 'Mount point does not exist, create it?')
		[ "$mkdiryn" = "Yes" ] && sudo -A mkdir -p "$mp"
	fi
	echo $mp
	mount "$chosen" "$mp" && notify-send "$chosen mounted to $mp." || notify-send "Error mounting $chosen to $mp"
fi


