#!/bin/sh

source /etc/sysconfig/config
source /etc/sysconfig/devices
source /etc/sysconfig/helpers/functions

IS_ON=/bin/chkconfig
CHKDEV=partitions

if [ "X${CFGDEVICES_ROOTDEV_EXTRA_MTDPARTS}" == "X" ]
then
	exit 0;
fi

case "$1" in
    "start")
		# Mounting data partition if present
		# Assume that the device is already accessible
		# For now assume that the device is on the same device as the root filesystem
		# For now assume that the filesystem is of the same type as the root filesystem

		echo -ne "Mounting extra partitions ... "
		if ! $IS_ON $CHKDEV
		then
			echo "disabled"
			exit 0
		fi
		
		if [ "X${CFGDEVICES_BOOTDEV}" == "Xnor" ]
		then
			CFGDEVICES_PARTITION_MTDPARTS=${CFGDEVICES_BOOTDEV_MTDPARTS}
			LOAD_MODULES="mhif chipreg mtd mtdchar mtdblock mg3500_flash";	
			ROOT_MTDDEV="physmap-flash";
			if [ "X${CFGDEVICES_PARTITION_MTDPARTS}" != "X" ]
			then
				load_module cmdlinepart  "mtdparts=${ROOT_MTDDEV}:${CFGDEVICES_BOOTDEV_MTDPARTS}"
			else
				load_module cmdlinepart
			fi
			if [ $? -ne 0 ]; then
				echo -ne "Failed to load module cmdlinepart.\n";
			fi
			 # Now load all the modules that were found
			 for module in ${LOAD_MODULES}; do 
				 load_module ${module};
				 if [ $? -ne 0 ]; then
					 echo -ne "Failed to load module ${module}.\n";
				 fi
			 done
					  
		else
			CFGDEVICES_PARTITION_MTDPARTS=${CFGDEVICES_ROOTDEV_MTDPARTS}
		fi

		OLD_IFS=${IFS}
		IFS=","

		# have to get the mtd number of first non-core partition
		for part in ${CFGDEVICES_PARTITION_MTDPARTS}
		do
			name=$(echo ${part} | sed -e 's/[^\(]*[\(]\([^\)]*\)[\)].*/\1/')
			case ${name} in
				mboot*) 
					i=$((i+1))
					;;
				initrd*) 
					i=$((i+1))
					;;
				kernel*) 
					i=$((i+1))
					;;
				rootfs*) 
					i=$((i+1))
					;;
				*) break
					;;
			esac
		done

		# now mount any extra partitions
		for part in ${CFGDEVICES_ROOTDEV_EXTRA_MTDPARTS}
		do
			MOUNTPOINT=`echo ${part}|cut -d':' -f1`
			FSTYPE=`echo ${part}|cut -d':' -f2`

			# skip raw partitions
			if [ "X${FSTYPE}" == "Xraw" ]
			then
				i=$((i+1));
				continue
			fi

			if [ "X${CFGDEVICES_ROOTDEV}" == "Xnand" ] || [ "X${CFGDEVICES_BOOTDEV}" == "Xnor" ]
			then
				MOUNT_DEVICE="/dev/mtdblock$((i))";
				if [ ! -b ${MOUNT_DEVICE} ]
				then
					echo -ne "ERROR:\n${MOUNT_DEVICE} not found, not accessible or not a block device.\n";
					 # exit 5;
				fi
			else
				echo -ne "ERROR:\nPartition is on an unsupported device.\n";
				exit 1;
			fi

			if [ ! -d /${MOUNTPOINT} ]
			then
				mkdir -p /${MOUNTPOINT};
				if [ $? -ne 0 ]; then
					echo -ne "ERROR:\nCannot not create mountpoint /${MOUNTPOINT}.\n";
					# exit 15;
				fi
			fi

			mount ${MOUNT_DEVICE} /${MOUNTPOINT} -t ${FSTYPE}
			if [ $? -ne 0 ]; then
				echo -ne "ERROR:\nMount failed, is the data filesystem initialized ?\n";
				# exit 20;
			fi
			chmod 777 /${MOUNTPOINT}

			i=$((i+1));
		done
		IFS=${OLD_IFS};
		echo -ne "done\n";
        ;;
    "stop")
    	echo -ne "Unmounting extra partitions ... "
		OLD_IFS=${IFS}
		IFS=","
		for part in ${CFGDEVICES_ROOTDEV_EXTRA_MTDPARTS}
		do
			MOUNTPOINT=`echo ${part}|cut -d':' -f1`
			FSTYPE=`echo ${part}|cut -d':' -f2`

			if [ "X${FSTYPE}" == "Xraw" ]
			then
				continue
			fi

			if [ "X${CFGDEVICES_ROOTDEV}" == "Xnand" ] || [ "X${CFGDEVICES_BOOTDEV}" == "Xnor" ]
			then
				/bin/umount /${MOUNTPOINT};
				if [ "X$?" != "X0" ]; then 
					echo -ne "ERROR:\nFailed to unmount /${MOUNTPOINT}\n";
				fi
			fi
		done
		IFS=${OLD_IFS};
		echo -ne "done\n";
        ;;
    *)
        echo "Syntax is $0 start | stop"
        exit 255;
esac

exit 0;
