#!/bin/bash

ERROR() {
	yad --title="Mount Error" \
	--borders=20 \
	--timeout=5 \
	--image "/usr/share/icons/Puppy Standard/48/status/dialog-error.svg" \
	--text-align=center \
	--text="\n Failed mount of <b>$NAME</b>\n" \
	--buttons-layout=center \
	--button="OK:0"
}

FILEMANAGER=defaultfilemanager
[[ $(grep spacefm /usr/local/bin/defaultfilemanager 2>/dev/null) ]] && FILEMANAGER="spacefm -w"

NAME="${1#/dev/}"
if [ ! "`findmnt /mnt/$NAME`" ]; then # partition not mounted, so mount now
	mkdir -p "/mnt/$NAME" || exit 1
	mount "/dev/$NAME" "/mnt/$NAME"
	if [ $? -ne 0 ]; then
		rmdir "/mnt/$NAME"
		ERROR &
		exit 1
	fi
	particons
	$FILEMANAGER "/mnt/$NAME" &
else # partition already mounted
	$FILEMANAGER "/mnt/$NAME" &
fi
