#!/bin/bash
# 30-Jan-2026 radky: GUI to toggle drive partition (un)mounting
# Select the drive partition and click Toggle (or double-click)
# Ctrl+click to select multiple partitions for batch processing

########################################################################
#                                                                      #
# FUNCTIONS                                                            #
#                                                                      #
########################################################################

# define error message
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"
	drive-mount-toggle
}

# define usage guidelines
HELP() {
	yad --title="Help" \
	--borders=20 \
	--image "/usr/share/icons/Puppy Standard/48/actions/help.svg" \
	--text-align=center \
	--text="<b>Usage Guidelines</b>\n• Toggle mount/unmount state of selected drive partitions\n• Open selected drive partitions in the default file manager\n• Press Refresh to update the current list of drive partitions\n• To prevent file corruption do not remove mounted media\n\n<b>Toggle Mount State</b>\n• Select the drive partitions by Double-Click or Click+Toggle\n• Ctrl+Click to select multiple partitions for batch processing\n• Optional autoclose of GUI after Toggle button is pressed\n\n<b>Open in File Manager</b>\n• Click+Open to open partitions in the default file manager\n• Maiin GUI always closes after the Open button is pressed  		\n" \
	--buttons-layout=center \
	--button="OK:0"
}

# define configuration options
OPTIONS() {
	yad --title="Options" \
	--borders=15 \
	--text-align=center \
	--text="\n<b>GUI Preferences</b>\n" \
	--buttons-layout=center \
	--button="AutoClose":1 \
	--button="Icons":2 \
	--button="Cancel":0 
	ret=$?
		if [ "$ret" == 1 ]; then
		AUTOCLOSE=$(cut -d'=' -f2 ~/.config/zzzfm/drive-mount-close 2>/dev/null)
		[ "$AUTOCLOSE" != "Enabled" ] && AUTOCLOSE=Disabled
		yad --title="GUI AutoClose" \
		--borders=15 \
		--text-align=center \
		--text="\n AutoClose of GUI is currently <b>$AUTOCLOSE</b>\n" \
		--buttons-layout=center \
		--button="Enable":1 \
		--button="Disable":2 \
		--button="Cancel":0 
		ret=$?
		if [ "$ret" == 1 ]; then
			echo "AUTOCLOSE=Enabled" > ~/.config/zzzfm/drive-mount-close
			drive-mount-toggle
			exit
		elif [ "$ret" == 2 ]; then
			echo "AUTOCLOSE=Disabled" > ~/.config/zzzfm/drive-mount-close
			drive-mount-toggle
			exit
		fi
	elif [ "$ret" == 2 ]; then
		ICONS=$(cut -d'=' -f2 ~/.config/zzzfm/drive-mount-icons 2>/dev/null)
		[ "$ICONS" != "blue" ] && ICONS=red-green
		yad --title="GUI Icons" \
		--borders=15 \
		--text-align=center \
		--text="\n Drive mount icons are currently <b>$ICONS</b>\n" \
		--buttons-layout=center \
		--button="Red/Green":1 \
		--button="Blue":2 \
		--button="Cancel":0 
		ret=$?
		if [ "$ret" == 1 ]; then
			echo "ICONS=red-green" > ~/.config/zzzfm/drive-mount-icons
			drive-mount-toggle
			exit
		elif [ "$ret" == 2 ]; then
			echo "ICONS=blue" > ~/.config/zzzfm/drive-mount-icons
			drive-mount-toggle
			exit
		fi
	fi
}

# define drive partitions
PARTITIONS=$(
	ICONS=$(cut -d'=' -f2 ~/.config/zzzfm/drive-mount-icons 2>/dev/null)
	[ "$ICONS" != "blue" ] && ICONS=red-green
	[ "$ICONS" = "red-green" ] && \
	ICON1="gtk-yes" ICON2="gtk-no" || ICON1="gtk-add" ICON2="gtk-remove" 
	while read DEV FSTYPE SIZE LABEL F5; do
		PARTITION="${DEV##*/}"
		[ "`findmnt $DEV`" ] && IMG=${ICON1} || IMG=${ICON2}
		echo "$IMG" "$PARTITION" "$DEV" "$SIZE" "$FSTYPE" "$LABEL"
	done < <(probepart -extra-info -hr | grep -v '|none|' | sed -e 's/ /-/g' -e 's/|/ /g' -e 's/not_mounted/none/g' -e 's/mounted/none/g')
)

# open drive partitions in default file manager
OPEN() {
	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
}

# toggle mount/unmount state of drive partitions
TOGGLE() {
	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 >/dev/null 2>&1
	else # partition mounted, so unmount now
		test -n "$NAME"
		for FILE in "/mnt/$NAME"/*; do
			[ -e "$FILE" ] && sync -f "$FILE"
			break
		done
		umount -l "/mnt/$NAME"
		rmdir "/mnt/$NAME"
		particons >/dev/null 2>&1
	fi
}

########################################################################
#                                                                      #
# MAIN DIALOG                                                          #
#                                                                      #
########################################################################

# yad list of drive partitions
	AUTOCLOSE=$(cut -d'=' -f2 ~/.config/zzzfm/drive-mount-close 2>/dev/null)
	[ "$AUTOCLOSE" != "Enabled" ] && AUTOCLOSE=Disabled
	PART_FILE=$(yad 2>/dev/null --list \
	--title="Drive Mount Toggle" \
	--width=400 --height=400 \
	--list \
	--column="Mnt:IMG" \
	--column="Partition:PARTITION" \
	--column="Device File:DEV" \
	--column="Size:SIZE" \
	--column="FStype:FSTYPE" \
	--column="Label:LABEL" \
	--print-column=3 \
	--separator=' ' \
	--multiple \
	--buttons-layout=center \
	--button="Help":5 \
	--button="Options"\!""\!"AutoClose is $AUTOCLOSE":4 \
	--button="Refresh"\!""\!"Refresh list of drive partitions":3 \
	--button="Open"\!""\!"Open selected drive partitions in file manager":2 \
	--button="Toggle"\!""\!"Toggle mount state of selected drive partitions":0 \
	--button="Quit":1 \
	$PARTITIONS)
	ret=$?
	if [ $ret == 5 ]; then
		HELP
		drive-mount-toggle
		exit
	elif [ $ret == 4 ]; then
		OPTIONS
		drive-mount-toggle
		exit
	elif [ $ret == 3 ]; then
		drive-mount-toggle
		exit
	elif [ $ret == 2 ]; then
		for p in $PART_FILE; do
			OPEN $p
		done
	elif [ $ret == 0 ]; then
		for p in $PART_FILE; do
			TOGGLE $p
		done
		if [ "$AUTOCLOSE" = "Disabled" ]; then
			sleep 0.5
			drive-mount-toggle
		fi
	else
		exit
	fi
