#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

do_dni_mount_root() {

    if [ "x$(/sbin/artmtd -r wpspin | awk  '{print $2}')" = "x00000000" ];then
        echo "Mount the overlay filesystem to rootfs_data partition..."
        mount_root
        boot_run_hook preinit_mount_root
    else
        echo "Before DNI mount_root"
        mkdir -p /tmp/mnt
        mkdir -p /tmp/overlay
        mkdir -p /tmp/root
        #mount -t overlayfs -olowerdir=/,upperdir=/tmp/overlay "overlayfs:/tmp/overlay" /mnt
        /bin/mount -t tmpfs -o noatime,mode=0755 root /tmp/root
        mkdir -p /tmp/root/root /tmp/root/work

        . /lib/functions/preinit.sh

        #pivot /mnt /rom
        fopivot /tmp/root/root /tmp/root/work /rom 1
    fi

    flash_type="$(cat /tmp/sysinfo/flash_type 2>/dev/null)"
    #flash_type="EMMC"
    [ -n $flash_type ] && echo "$flash_type" > /flash_type

    if [ "x$flash_type" = "xNAND_FLASH" ]; then
            model_name=$(/sbin/artmtd -r board_model_id | cut -f 2 -d ":")
			model_prefix=$(echo $model_name | cut -b3)
            # We use a Base and Satellite common volume to test UBI image is OK or not. Here we choose
            # "vol_oopsdump".
            test_volume="vol_ntgrdata"
            if [ -z "$(grep $test_volume /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
                            ubiattach /dev/ubi_ctrl -m $mtdn
                            if [ -n "$(grep $test_volume /proc/mtd)" ]; then
                                    echo "Info: attach ubi device on ntgrdata success!"
                            else
									if [ "x$model_prefix" = "xR" ]; then
										ubinize -m 2048 -p 128KiB -o /tmp/ntgrdata.ubi /etc/ntgrdata.cfg
									fi
                                    ubidetach /dev/ubi_ctrl -m $mtdn
                                    flash_erase /dev/mtd$mtdn 0 0
                                    nandwrite -p /dev/mtd$mtdn /tmp/ntgrdata.ubi
                                    ubiattach /dev/ubi_ctrl -m $mtdn
                            fi
                    fi

                    if [ -z "$(grep $test_volume /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/forceshield ] && mkdir -p /tmp/ntgrdata/forceshield
							#[ ! -d /tmp/ntgrdata/rae ] && mkdir -p /tmp/ntgrdata/rae
							[ ! -d /tmp/ntgrdata/devtable ] && mkdir -p /tmp/ntgrdata/devtable
							[ ! -d /tmp/ntgrdata/dal ] && mkdir -p /tmp/ntgrdata/dal
						fi
                    fi
            fi
    fi

    echo "After DNI mount_root"
}

do_mount_root() {
	echo "Before mount_root"
	#mount_root
	#boot_run_hook preinit_mount_root
	do_dni_mount_root
	[ -f /sysupgrade.tgz ] && {
		echo "- config restore -"
		cd /
		tar xzf /sysupgrade.tgz
	}
	echo "After mount_root"
}

[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main do_mount_root
