#!/bin/sh
#
# /scripts/install
#
# (C)opyright Tecomat a.s.
#
# 2014/06/04 Hosek Martin <hosek@tecomat.cz>
#

INSTALL_DIR="/mnt"
CONTENT_DIR="content"
PRIVATE_DIR="private"

### Show update splash image (Lock screen)

chvt 1

/sbin/fb_noblank

cat $PRIVATE_DIR/id-update.fb0 > /dev/fb0

sleep 2

### Update MLO/Boot

if [ "`hostname | grep 'Boot'`" != "" ]; then

    umount $INSTALL_DIR/data >/dev/null 2>&1
    umount $INSTALL_DIR/root >/dev/null 2>&1

    ret=1

    for i in 0 1 2 3; do
	flash_eraseall /dev/mtd$i > /dev/null 2>&1 || (echo "Unable to erase 'mtd$i' flash memory!"; continue)
	cat $CONTENT_DIR/MLO > /dev/mtdblock$i || (echo "Unable to write MLO to mtdblock$i!"; continue)
	ret=0
    done

    if [ $ret == 1 ]; then
	echo "An error was found no empty block for MLO!"
	exit 1
    fi

    flash_eraseall > /dev/null 2>&1 /dev/mtd4 || (echo "Unable to erase 'mtd4' flash memory!"; exit 1)

    cat $CONTENT_DIR/u-boot.img > /dev/mtdblock4 || (echo "Unable to write U-Boot to mtdblock4!"; exit 1)

    flash_eraseall /dev/mtd6 > /dev/null 2>&1 || (echo "Unable to erase 'mtd6' flash memory!"; exit 1)

    cat $CONTENT_DIR/uImage > /dev/mtdblock6 || (echo "Unable to write uImage to mtdblock6!"; exit 1)

    flash_eraseall /dev/mtd7 > /dev/null 2>&1 || (echo "Unable to erase 'mtd7' flash memory!"; exit 1)
    flash_eraseall /dev/mtd8 > /dev/null 2>&1 || (echo "Unable to erase 'mtd8' flash memory!"; exit 1)

    mount /dev/mtdblock7 $INSTALL_DIR/data -t jffs2 || (echo "Unable to mount DATA partition (mtdblock7)!"; exit 1)
    mount /dev/mtdblock8 $INSTALL_DIR/root -t jffs2 || (echo "Unable to mount ROOT partition (mtdblock8)!"; exit 1)
fi

### Update Boot/Root/Data filesystem

if [ -f /etc/.version ]; then
    VERSION_BOOT=`cat /etc/.version`
fi

if [ `expr $VERSION_BOOT \< 1.2` == 1 ]; then
    cat $CONTENT_DIR/uImage > /dev/mtdblock6 || (echo "Unable to write uImage to mtdblock6!"; exit 1)
fi

if [ -f /etc/network/interfaces ]; then
    rm -f $CONTENT_DIR/root/etc/network/interfaces
fi

if [ -f /etc/default/display ]; then
    rm -f $CONTENT_DIR/root/etc/default/display
fi

if [ -f /etc/default/minibrowser ]; then
    rm -f $CONTENT_DIR/root/etc/default/minibrowser
fi

if [ -f /etc/default/plccoms ]; then
    rm -f $CONTENT_DIR/root/etc/default/plccoms
fi

if [ -f /etc/default/ntpdate ]; then
    rm -f $CONTENT_DIR/root/etc/default/ntpdate
fi

if [ -f /etc/default/xserver ]; then
    rm -f $CONTENT_DIR/root/etc/default/xserver
fi

if [ -f /etc/pointercal ]; then
    rm -f $CONTENT_DIR/root/etc/pointercal.tscadc
    rm -f $CONTENT_DIR/root/etc/pointercal.ft5x06
fi

if [ -f /opt/MiniBrowser/MiniBrowser.ini ]; then
    rm -f $CONTENT_DIR/root/opt/MiniBrowser/MiniBrowser.ini
fi

cp -af $CONTENT_DIR/data $INSTALL_DIR || (echo "Unable to write DATA files to partition (mtdblock7)!"; exit 1)
cp -af $CONTENT_DIR/root $INSTALL_DIR || (echo "Unable to write ROOT files to partition (mtdblock8)!"; exit 1)

if [ ! -d $INSTALL_DIR/data/download ]; then
    mkdir $INSTALL_DIR/data/download
fi

sync

unmount $INSTALL_DIR/data >/dev/null 2>&1
unmount $INSTALL_DIR/root >/dev/null 2>&1

killall -USR1 rebootd

#/sbin/reboot

exit 0
