#!/bin/sh
# 
# Gtkdialog box for the mount/umount commands. Part of SliTaz tools.
# libmountbox: /usr/lib/slitaz/libmountbox
#
# (C) 2009 - SliTaz GNU/Linux project.
#
VERSION=20090802
# Mountbox is only for root.
if test $(id -u) != 0 ; then
	exec subox mountbox
	exit 0
fi

# Commom mount point in /mnt
mkdir -p /mnt/harddisk

# Just basic help.
export HELP='
<window title="Mountbox - Help" icon-name="help">
<vbox>
	<text use-markup="true" width-chars="56">
		<label>"
<b>SliTaz Mountbox - Help</b>"
		</label>
	</text>
	
	<frame English>
		<text wrap="true" use-markup="true">
			<label>
"Mountbox lets you mount devices on mount points. A device 
can be a cdrom, flash key, USB disk or local HD partition.
Mount points are generated from /media and /mnt. Simply
select an unmounted device. Then type or select a folder 
to mount. Then press a button.
"
			</label>
		</text>
	</frame>
	<frame Français>
		<text wrap="true" use-markup="true">
			<label>
"Mountbox permet de monter des périphériques (devices)
sur des points de montage. Un device peut être un cdrom,
une clé USB ou un disque dur local. La liste des points
de montage est généré depuis /media te /mnt.
"
			</label>
		</text>
	</frame>
	
	<hbox>
		<button ok>
			<action type="closewindow">HELP</action>
		</button>
	</hbox>
</vbox>
</window>'

# Mount and umount buttons with fiel for devive and mount point.
MAIN_DIALOG='
<window title="Mountbox" icon-name="media-flash">
<vbox>
	<frame>
	<tree>
		<width>500</width><height>130</height>
		<variable>DEVICE</variable>
		<label>Umounted dev|Size|System|Type|Label|Boot|Start|End|Blocks|Id|UUID</label>
		<input>/usr/lib/slitaz/libmountbox list-umounted</input>
		<action>/usr/lib/slitaz/libmountbox umounted-fs-infos</action>
		<action>refresh:MOUNTED</action>
		<action>refresh:DEVICE</action>
	</tree>
	</frame>
	<frame>
	<tree>
		<width>500</width><height>110</height>
		<variable>MOUNTED</variable>
		<label>Mounted fs|Size|Used|Available|Use%|Mounted on|Option</label>'

# /dev/root
RES=`df -h / | grep rootfs`
dev="/dev/root"
SIZE=`echo $RES | cut -d " " -f 2`
USED=`echo $RES | cut -d " " -f 3`
AVAILABLE=`echo $RES | cut -d " " -f 4`
PCT=`echo $RES | cut -d " " -f 5`
MOUNTED_ON=`echo $RES | cut -d " " -f 6`
if [ $SIZE != 0 ]; then
	ROOT_ITEM="
		<item icon=\"drive-harddisk\">$dev | $SIZE | $USED | $AVAILABLE | $PCT | $MOUNTED_ON</item>"
fi
MAIN_DIALOG=${MAIN_DIALOG}${ROOT_ITEM}

# Now we have rootfs and icons, list all mounted fs.
DEVICE='<input>/usr/lib/slitaz/libmountbox list-mounted</input>
		<action>/usr/lib/slitaz/libmountbox mounted-fs-infos</action>
		<action>refresh:MOUNTED</action>
		<action>refresh:DEVICE</action>
	</tree>
	
	</frame>
	
	<hbox>
		<text use-markup="true" >
			<label>"<b>Mount selected device on:</b>"</label>
		</text>
		<entry accept="directory">
			<default>/media/cdrom</default>
			<variable>MOUNT_POINT</variable>
			</entry>
		<button>
			<label>Browse</label>
			<input file icon="folder-open"></input>
			<action type="fileselect">MOUNT_POINT</action>
		</button>
	</hbox>
	'

# Get the mount points list.
MAIN_DIALOG=${MAIN_DIALOG}${DEVICE}

# Actions buttons (mount, umount, eject, etc).
ACTIONS='
		
	<hbox>
		<button>
			<label>Mount</label>
			<input file icon="edit-redo"></input>
			<action>mkdir -p $MOUNT_POINT</action>
			<action>mount $DEVICE $MOUNT_POINT</action>
			<action>refresh:MOUNTED</action>
			<action>refresh:DEVICE</action>
		</button>
		<button>
			<label>Umount</label>
			<input file icon="undo"></input>
			<action>umount $MOUNT_POINT; sleep 1</action>
			<action>refresh:MOUNTED</action>
			<action>refresh:DEVICE</action>
		</button>
		<button>
			<label>Device list</label>
			<input file icon="reload"></input>
			<action>refresh:DEVICE</action>
		</button>
		<button>
			<label>Eject</label>
			<input file icon="media-cdrom"></input>
			<action>eject</action>
		</button>
		<button>
			<label>Loop</label>
			<input file icon="go-jump"></input>
			<action>/usr/lib/slitaz/libmountbox loopmgr</action>
			<action>refresh:MOUNTED</action>
			<action>refresh:DEVICE</action>
		</button>
		<button>
			<label>Crypto</label>
			<input file icon="passwd"></input>
			<action>/usr/lib/slitaz/libmountbox cryptomgr</action>
			<action>refresh:DEVICE</action>
		</button>
		<button help>
			<input file icon="help"></input>
			<action type="launch">HELP</action>
		</button>
		<button>
			<label>Quit</label>
			<input file icon="exit"></input>
			<action type="exit">Exit</action>
		</button>
		
	</hbox>
	
</vbox>
</window>'

export MAIN_DIALOG=${MAIN_DIALOG}${ACTIONS}
gtkdialog --center --program=MAIN_DIALOG >/dev/null

exit 0
