#!/bin/sh

UBOOT="u-boot-nand.bin.md5"
LINUX="linux.img.md5"
APP="app.img.md5"

KERNEL="uImage_wr.md5"
DTB="ccp6000a.dtb.md5"
ROOTFS="initrd_wr.gz.uboot.md5"

MTD_UBOOT="mtd0"
MTD_LINUX="mtd1"
MTD_APP="mtd3"

MDSUM_OK="OK"
MDSUM_OK2="´°Î»"

INSTALL_TYPE_FS="FS"
INSTALL_TYPE_DIRECT="DIRECT"

create_ubifs_and_empty_volume()
{
    if [ "yes" = $3 ]; then
        ubidetach -p /dev/$2
        if [ $? -ne 0 ]; then
            echo "UBIFS Detach Error!!!"
            exit -1
        fi
    fi

    part_num=`echo $2|sed -e 's/mtd//'|awk '{print $1}'`

    flash_eraseall /dev/mtd$part_num
    if [ $? -ne 0 ]; then
        echo "UBIFS Erase Error!!!"
        exit -1
    fi

    ubiformat /dev/mtd$part_num  -s 512
    if [ $? -ne 0 ]; then
        echo "UBIFS Format Error!!!"
        exit -1
    fi

    ubiattach /dev/ubi_ctrl -m $part_num
    if [ $? -ne 0 ]; then
        echo "UBIFS Attach Error!!!"
        exit -1
    fi

    fname=`ls $1|sed -e 's/\.img//'|awk '{print $1}'`
    ubimkvol /dev/ubi0 -m -N $fname"1"
    if [ $? -ne 0 ]; then
        echo "UBIFS Mkvol Error!!!"
        exit -1
    fi
}

update()
{
    part_num=`echo $2|sed -e 's/mtd//'|awk '{print $1}'`

    ubiattach /dev/ubi_ctrl -m $part_num
    if [ $? -ne 0 ]; then
        create_ubifs_and_empty_volume $1 $2
    fi

    ubifs=`ls /dev|grep ubi0_0|awk '{print $1}'`

    if [ ! $ubifs ]; then
        echo "UBIFS : no volume!!!"
        create_ubifs_and_empty_volume $1 $2 "yes"
    fi

    ubiupdatevol /dev/ubi0_0 $1
    if [ $? -ne 0 ]; then
        echo "UBIFS Update Error!!!"
        exit -1
    fi

    ubidetach -p /dev/$2
    if [ $? -ne 0 ]; then
        echo "UBIFS Detach Error!!!"
        exit -1
    fi
}

update_direct()
{
    flash_eraseall /dev/$2
    if [ $? -ne 0 ]; then
        echo "NAND Erase Error!!!"
        exit -1
    fi

    nandwrite -mp /dev/$2 $1
    if [ $? -ne 0 ]; then
        echo "NAND Write Error!!!"
        exit -1
    fi
}

check_and_update()
{
    echo 'md5 file : '$1
    echo 'mtd      : '$2
    echo 'type     : '$3

    result=`md5sum -c $1 |awk '{print $2}'`
    if [ $result = $MDSUM_OK ]; then
        echo
    elif [ $result = $MDSUM_OK2 ]; then
        echo
    else
        echo "MD5 Check Error!!! : "$1
        exit -1
    fi

    file=`echo $1|sed -e 's/\.md5//'|awk '{print $1}'`
    if [ ! -f $file ]; then
        echo "No file!!! : "$file
        exit -1
    fi

    OS=`uname -r|grep WR`
    if [ $OS ]; then
        if [ $INSTALL_TYPE_FS = $3 ]; then
            update $file $2
        else
            update_direct $file $2
        fi
    else
        echo "OS :"`uname -r`
    fi
}

update_to_nand()
{
    for md_file in `ls`
    do
        case $md_file in
            $UBOOT)
                check_and_update $md_file $MTD_UBOOT $INSTALL_TYPE_DIRECT
            ;;
            $LINUX)
                check_and_update $md_file $MTD_LINUX $INSTALL_TYPE_FS
            ;;
            $APP)
                check_and_update $md_file $MTD_APP $INSTALL_TYPE_FS
            ;;
        esac
    done
}

recover_to_nand()
{
    for md_file in `ls`
    do
        case $md_file in
            $LINUX)
                check_and_update $md_file $MTD_LINUX $INSTALL_TYPE_FS
            ;;
            $APP)
                check_and_update $md_file $MTD_APP $INSTALL_TYPE_FS
            ;;
        esac
    done
}

update_to_sdcard()
{
    mount /dev/mmcblk0p1 /srv

    bin_uboot=`echo $UBOOT |sed -e 's/\.md5//'|awk '{print $1}'`
    bin_kernel=`echo $KERNEL|sed -e 's/\.md5//'|awk '{print $1}'`
    bin_dtb=`echo $DTB   |sed -e 's/\.md5//'|awk '{print $1}'`
    bin_rootfs=`echo $ROOTFS|sed -e 's/\.md5//'|awk '{print $1}'`
    bin_app=`echo $APP   |sed -e 's/\.md5//'|awk '{print $1}'`

    for binfile in `ls`
    do
        case $binfile in
            $bin_uboot)
                echo "update "$binfile" to sd"
                cp $bin_uboot /srv
            ;;
            $bin_dtb)
                echo "update "$binfile" to sd"
                # tako
                cp $bin_dtb /srv/p1022ds.dtb
            ;;
            $bin_kernel)
                echo "update "$binfile" to sd"
                cp $bin_kernel /srv
            ;;
            $bin_rootfs)
                echo "update "$binfile" to sd"
                cp $bin_rootfs /srv
            ;;
            $bin_app)
                echo "update "$binfile" to sd"
                cp $bin_app /srv
            ;;
        esac
    done

    umount /srv
}

if [ $# -eq 0 ]; then
    update_to_nand
else
    if [ $1 = "sd" ]; then
        update_to_sdcard
    elif [ $1 = "recover" ]; then
        recover_to_nand
    else
        echo "ERROR : Illegal Option!!!"
    fi
fi

exit 0
