#!/bin/sh

source /etc/utils/shell-utils.sh
DEBUG=0

wait_for_dev_node()
{
# $1 - node abs filepath
# $2 - timeout (seconds)
    LLL=`ls -la $1`
    #log "dev_filepath = $1, debug = $LLL"
    time_elapsed=0
    time_t0=100000
    time_t1=1000000
    while [ $time_elapsed -lt $time_t1 ]; do
        if [ -e $1 ]; then
	    #log "[i] found"
	    return 0
	fi
	#log "not found! time_elapsed = $time_elapsed"
	usleep $time_t0
	time_elapsed=$((time_elapsed+$time_t0))
    done
    return 1
}

create_helper_symlinks() {
# $1 - kernel command line
# $3 - where to create symlinks

    kernelCmdline=$1
    symlinksDir=$2

    mkdir -p $symlinksDir

    get_flash_type flash_type
    if [ "$flash_type" == "nand" ]; then
        for i in `ls -d /sys/devices/platform/mtd/mtd*[0-9] 2>/dev/null`; do
            name=`cat $i/name`
            dev="`echo $i |sed -e 's|/sys/devices/platform/mtd|/dev|g'`"
            ln -s $dev $symlinksDir/$name
        done
        check_nand_vendor_section
    elif [ "$flash_type" == "mmc" ]; then
        mmc_base_path="/sys/devices/platform/ffe07000.emmc/mmc_host/emmc/emmc:0001"
        if [ ! -d $mmc_base_path ] ; then
            mmc_base_path="/sys/devices/platform/soc/fe08c000.mmc/mmc_host/mmc0/mmc0:0001"
        fi
        list=`find ${mmc_base_path}/block/mmcblk0/ -name partition`
        for i in $list ; do
            name=${i#${mmc_base_path}/block/mmcblk0/};
            name=${name%/partition}
            ln -s /dev/$name $symlinksDir/$name;
        done
    fi
}

create_helper_symlinks "`cat /proc/cmdline`" $SYMLINKS_DIR

#temporary workaround for non-Infomir partition table
if [ -e $SYMLINKS_DIR/boot_a ] ; then
    cp -a $SYMLINKS_DIR/boot_a $SYMLINKS_DIR/Kernel
    cp -a $SYMLINKS_DIR/boot_b $SYMLINKS_DIR/Kernel2
    cp -a $SYMLINKS_DIR/system_a $SYMLINKS_DIR/Rootfs
    cp -a $SYMLINKS_DIR/system_b $SYMLINKS_DIR/Rootfs2
    cp -a $SYMLINKS_DIR/data $SYMLINKS_DIR/Userfs
fi

exit 0
