#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
# Copyright (C) 2010 Vertical Communications
#START=13

mount_ubi() {
	if [ -z "$(grep vol_ntgrdata /proc/mtd)" -a -n "$(grep ntgrdata /proc/mtd)" ]; then
		echo "Info: init ubi volumes on ntgrdata raw partition"

		mtdn=$(sed -nr 's/mtd([0-9]*):.*"ntgrdata"/\1/p' /proc/mtd)
		if [ -z "$mtdn" ]; then
			echo "Error: can't find ntgrdata partition in mtd device!"
		else
			UBI=`ubiattach /dev/ubi_ctrl -m $mtdn`
			if [ -n "$(grep vol_ntgrdata /proc/mtd)" ]; then
                                echo "Info: attach ubi device on ntgrdata success!"
                        else
				echo ">>>>> erash partition to attach ubi again <<<<<"
				ubidetach -m $mtdn
				flash_erase /dev/mtd$mtdn 0 0
				UBI=`ubiattach -m "$mtdn"`
				echo ">>>>> UBI info = $UBI <<<<<"
				if [ "$UBI" != "" ]; then
					UBI=`grep -l $mtdn /sys/class/ubi/*/mtd_num`
					UBI=${UBI/\/mtd_num/}; # remove "/mtd_num" from the path
					UBI=${UBI/\/sys\/class\/ubi\//}; # remove "/sys/class/ubi/" from the path
					DEV=/dev/$UBI # mdev should already populate the new device node for ubi in /dev.
					echo "vol_ntgrdata: DEV=$DEV UBI=$UBI"
					if [ "$DEV" != "" ]; then
						echo ">>>>> Creating ubi volume $DEV <<<<<"
						ubimkvol $DEV --name=vol_ntgrdata --type=dynamic --maxavsize
					fi
				fi
			fi

			if [ -z "$(grep vol_ntgrdata /proc/mtd)" ]; then
				echo "Error: create ubi volumes on ntgrdata raw partition fail!"
			else
				ubidev=$(ubinfo -a | grep -B5 vol_ntgrdata | sed -nr 's/Volume ID:.*\(on (ubi.*)\)/\1/p')
				if [ "x$(df |grep $ubidev:vol_ntgrdata)" = "x" ]; then
					[ ! -d /tmp/ntgrdata ] && mkdir -p /tmp/ntgrdata
					mount $ubidev:vol_ntgrdata /tmp/ntgrdata -t ubifs
					if [ $? -ne 0 ]; then
						mount $ubidev:vol_ntgrdata /tmp/ntgrdata -t ubifs
					fi
					[ ! -d /tmp/ntgrdata/circle ] && mkdir -p /tmp/ntgrdata/circle
					[ ! -d /tmp/ntgrdata/armor ] && mkdir -p /tmp/ntgrdata/armor
					[ ! -d /tmp/ntgrdata/dal ] && mkdir -p /tmp/ntgrdata/dal
					ln -sf /tmp/ntgrdata/dal /tmp/dal
				fi
                        fi
		fi
	fi

	if [ ! -d /tmp/ntgrdata ]; then
		[ ! -d /data/circle ] && mkdir -p /data/circle
		[ ! -d /data/armor ] && mkdir -p /data/armor
		[ ! -d /data/dal ] && mkdir -p /data/dal
		ln -sf /data/dal /tmp/dal
	fi

	if [ -z "$(grep pot /proc/mtd)" -a -n "$(grep dnidata /proc/mtd)" ]; then
		echo "Info: init ubi volumes on dnidata raw partition"

		mtdn=$(sed -nr 's/mtd([0-9]*):.*"dnidata"/\1/p' /proc/mtd)
		if [ -z "$mtdn" ]; then
			echo "Error: can't find dnidata partition in mtd device!"
		else
			ubiattach /dev/ubi_ctrl -m $mtdn
			if [ -n "$(grep pot /proc/mtd)" ]; then
				echo "Info: attach ubi device on dnidata success!"
			else
				ubinize -m 2048 -p 128KiB -o /tmp/dnidata.ubi /etc/dnidata.cfg
				ubidetach /dev/ubi_ctrl -m $mtdn
				flash_erase /dev/mtd$mtdn 0 0
				nandwrite -p /dev/mtd$mtdn /tmp/dnidata.ubi
				ubiattach /dev/ubi_ctrl -m $mtdn
			fi
		fi
		
		if [ -z "$(grep pot /proc/mtd)" ]; then
			echo "Error: create ubi volumes on dnidata raw partition fail!"
		fi
	fi
}

start(){
	mount_ubi
}

boot(){
	start
}
