#!/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

#mount $PART on $GNX_PATH and bind device files
mkdir -p "$GNX_PATH" 1>/dev/null
mountpoint -q "$GNX_PATH" 1>/dev/null
if [ "$?" == "0" ]; then
	echo "Error: $GNX_PATH is already a mountpoint, cannot continue" 1>&2
	exit 1
fi
mount -t ext4 "$PART" "$GNX_PATH" 1>/dev/null
if [ "$?" != "0" ]; then
	echo "Error: Failed to mount $PART on $GNX_PATH" 1>&2
	exit 1
fi
mkdir -p $GNX_PATH/dev
mount --bind /dev $GNX_PATH/dev
if [ "$?" != "0" ]; then
        echo "Error: Failed to bind device files to the Guinnux root" 1>&2
	exit 1
fi

exit 0
