#!/bin/sh

#check if root is running this
if [ "`id -u`" != "0" ]; then
        echo "Permission denied, must be root!" 1>&2
        exit 1
fi

#establish default for the program
if [ -z "$GNX_PATH" ]; then
	GNX_PATH="/"
fi
if [ -z "$DISK" ]; then
        DISK="/dev/mmcblk0"
fi
if [ -z "$PART" ]; then
	PART="/dev/mmcblk0p1"
fi

#open gnx root up for editing
if [ "$GNX_PATH" == "/" ]; then
	# we are running from the guinnux root so don't do any special mounting/unmounting
	exit 0
fi

#unmount $GNX_PATH and bind mounted device files
mountpoint -q "$GNX_PATH" 1>/dev/null
if [ "$?" != "0" ]; then
        echo "Warning: ${GNX_PATH}/dev is not a mountpoint, $GNX_PATH may not be the Guinnux root" 1>&2
else
	umount "$GNX_PATH"/dev 1>/dev/null
	if [ "$?" != "0" ]; then
        	echo "Error: Failed to unmount ${GNX_PATH}/dev!" 1>&2
        	exit 1
	fi
fi
mountpoint -q "$GNX_PATH" 1>/dev/null
if [ "$?" != "0" ]; then
	echo "Error: $GNX_PATH is not a mountpoint, $GNX_PATH may not be the Guinnux root" 1>&2
	exit 1
fi
umount "$GNX_PATH" 1>/dev/null
if [ "$?" != "0" ]; then
	echo "Error: Failed to unmount $PART on $GNX_PATH" 1>&2
	exit 1
fi

exit 0
