#!/bin/sh

DEFAULT_UPD_DECOMPRESSED_FOLDER=/upgrade/upd_files
DEFAULT_ADDITIONAL_FILES_FOLDER_NAME=additional_files

#
# the upgrading folder will be argv[1] while executing
#
if [ $# = 1 ]; then
    UPGRADING_FILES_FOLDER=$1
else
    UPGRADING_FILES_FOLDER=${DEFAULT_UPD_DECOMPRESSED_FOLDER}
fi
ADDITIONAL_FILES_FOLDER="${UPGRADING_FILES_FOLDER}/${DEFAULT_ADDITIONAL_FILES_FOLDER_NAME}"

# Delete some tools to make it more available memory
rm -f /bin/cdrecord
rm -f /bin/dvd+rw-format
rm -f /bin/dvd+rw-mediainfo
rm -f /bin/gpg
rm -f /bin/growisofs
rm -f /bin/mkisofs
rm -f /bin/smartctl
rm -f /bin/zip
rm -f /sbin/DM
rm -f /sbin/e2fsck
rm -f /sbin/mke2fs
rm -f /sbin/parted
rm -f /sbin/tune2fs
rm -f /sbin/pppd

#
# To upgrading the bootlogo
#
UPD_LOGO_FILENAME=dvr_logo.bmp
BOOTLOGO_PATH=/mtd0/data/logo.bmp
if [ -f ${BOOTLOGO_PATH} ]; then
    echo "the bootlogo exist, bootlogo upgrading is not necessary"
else
    echo "there is no bootlogo exist, updating it"
    if [ -f ${ADDITIONAL_FILES_FOLDER}/${UPD_LOGO_FILENAME} ]; then
        cp ${ADDITIONAL_FILES_FOLDER}/${UPD_LOGO_FILENAME} ${BOOTLOGO_PATH}
    else
        echo "the ${ADDITIONAL_FILES_FOLDER}/${UPD_LOGO_FILENAME} doesn't exist, bootlogo upgrading failed."
    fi
fi

#
# To upgrading the u-boot
#
if [ -f ${ADDITIONAL_FILES_FOLDER}/program_nor ]; then
    echo "cp ${ADDITIONAL_FILES_FOLDER}/program_nor /sbin/program_nor"
    cp ${ADDITIONAL_FILES_FOLDER}/program_nor /sbin/program_nor
    chmod +x /sbin/program_nor
fi
echo "/bin/mount -o remount,ro /mtd0"
/bin/mount -o remount,ro /mtd0

#
# Check total memory size to decide which u-boot we will use
#
TOTAL_RAM=`cat /proc/spi | grep "Total RAM" | sed -e 's/  */ /g' | cut -d " " -f 3`
PCIE_SUPPORT=`cat /proc/ioports | grep "PCIe" | cut -d " " -f 3`

if [ ${TOTAL_RAM} = 1073741824 ]; then
    if [ ${PCIE_SUPPORT} = PCIe ]; then
        UBOOT_IMG=${ADDITIONAL_FILES_FOLDER}/u-boot_1024_pcie.bin
    else
        UBOOT_IMG=${ADDITIONAL_FILES_FOLDER}/u-boot_1024.bin
    fi
else
    if [ ${PCIE_SUPPORT} = PCIe ]; then
        UBOOT_IMG=${ADDITIONAL_FILES_FOLDER}/u-boot_512_pcie.bin
    else
        UBOOT_IMG=${ADDITIONAL_FILES_FOLDER}/u-boot_512.bin
    fi
fi

echo "/sbin/program_nor -w -u ${UBOOT_IMG}"
/sbin/program_nor -w -u ${UBOOT_IMG}

echo "/bin/mount -o remount,rw /mtd0"
/bin/mount -o remount,rw /mtd0

#
# To remove the QT lib link
#
rm -rf /mtd0/data/QT/lib/*.so
rm -rf /mtd0/data/QT/lib/*.so.4
rm -rf /mtd0/data/QT/lib/*.so.4.4
rm -rf /mtd0/data/QT/lib/*.so.4.8


