#!/bin/sh
function search {
for i in $1;
do
  if [ "$i" != "$1" ]; then
    partname=`cat /$i/uevent | grep PARTNAME | cut -d '=' -f 2`
    devname=`cat /$i/uevent | grep DEVNAME | cut -d '=' -f 2`
    mkdir -p /dev/block/by-name/
    if [ ! -e /dev/block/by-name/$partname ]; then
      ln -sf /dev/$devname /dev/block/by-name/$partname
    else
      ln -sf /dev/$devname /dev/block/by-name/$partname-$devname
    fi
    if [ -d "/usr/share/u-boot-bin/" ] && [ -e "/dev/mmcblk0p7" ]; then
      mkdir -p /dev/disk/by-label/
      mkdir -p /dev/disk/by-partlabel/
      ln -sf /dev/$devname /dev/disk/by-label/$partname
      ln -sf /dev/$devname /dev/disk/by-partlabel/$partname
      if [ ! -e /dev/$partname ]; then
        ln -sf /dev/$devname /dev/$partname
      fi
    fi
  fi
done
}

if [ -d "/sys/block/mmcblk0" ]; then
    search "/sys/block/mmcblk0/mmcblk0p*"
fi
if [ -d "/sys/block/mmcblk1" ]; then
    search "/sys/block/mmcblk1/mmcblk1p*"
fi
if [ -d "/sys/block/sda" ]; then
    search "/sys/block/sda/sda*"
fi
if [ -d "/sys/block/sdb" ]; then
    search "/sys/block/sdb/sdb*"
fi
if [ -d "/sys/block/sdc" ]; then
    search "/sys/block/sdc/sdc*"
fi
if [ -d "/sys/block/sdd" ]; then
    search "/sys/block/sdd/sdd*"
fi
if [ -e "/dev/block/by-name/flag" ]; then
    if [ ! -e /dev/block/by-name/bootoptions ]; then
      ln -sf /boot/ /dev/block/by-name/bootoptions
    fi
fi

if [ -e "/sys/block/mtdblock2/device/name" ]; then
    devname=`cat /sys/block/mtdblock2/device/name`
    if [ $devname == "bootoptions" ]; then
      mkdir -p /dev/block/by-name/
      ln -sf /dev/mtdblock2 /dev/block/by-name/$devname
    fi
fi

if  [ -e "/dev/block/by-name/other1" ] && [ -e "/dev/block/by-name/other2" ]; then
    echo 0 > /sys/block/mmcblk0boot1/force_ro
    if [ ! -e /dev/block/by-name/others ]; then
      ln -sf /dev/mmcblk0boot1 /dev/block/by-name/others
    fi
fi

if grep -q -E "dm820|dm7080|dm900|dm920" /proc/stb/info/model; then
    if [ -e "/dev/block/by-name/-mmcblk0p1" ] && [ -e "/dev/block/by-name/-mmcblk0p2" ]; then
        echo 0 > /sys/block/mmcblk0boot1/force_ro
        if [ ! -e /dev/block/by-name/others ]; then
            ln -sf /dev/mmcblk0boot1 /dev/block/by-name/others
        fi
    fi
fi

if grep -q -E "et11000|sf4008" /proc/stb/info/boxtype; then
    if [ -e "/dev/block/by-name/-mmcblk0p1" ] && [ -e "/dev/block/by-name/-mmcblk0p2" ]; then
        echo 0 > /sys/block/mmcblk0boot1/force_ro
        if [ ! -e /dev/block/by-name/others ]; then
            ln -sf /dev/mmcblk0boot1 /dev/block/by-name/others
        fi
    fi
fi

if grep -q -E "beyonwizu4" /proc/stb/info/boxtype; then
    if [ -e "/dev/block/by-name/update" ] && [ -e "/dev/block/by-name/splash" ]; then
        echo 0 > /sys/block/mmcblk0boot1/force_ro
        if [ ! -e /dev/block/by-name/others ]; then
            ln -sf /dev/mmcblk0boot1 /dev/block/by-name/others
        fi
    fi
fi

if grep -q -E "u57" /proc/stb/info/boxtype; then
        echo 0 > /sys/block/mmcblk0boot1/force_ro
        if [ ! -e /dev/block/by-name/others ]; then
            ln -sf /dev/mmcblk0boot1 /dev/block/by-name/others
        fi
fi

