#!/bin/sh
#
# /scripts/install
#
# (C)opyright Tecomat a.s.
#
# 2018/02/20 Hosek Martin <hosek@tecomat.cz>
#

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

### Show update splash image (Lock screen)

/usr/bin/killall -9 mloop >/dev/null 2&>1
/usr/bin/killall -9 ploop >/dev/null 2&>1
/usr/bin/killall -9 bloop >/dev/null 2&>1
/usr/bin/killall -9 MiniBrowser >/dev/null 2&>1
/usr/bin/killall -9 PLCComS >/dev/null 2&>1
/usr/bin/killall -9 bkld >/dev/null 2&>1

chvt 1

/sbin/fb_noblank

# Disable FB cursor
echo -en '\e[?25l' >/dev/tty0

if [ -r /etc/default/xserver ]; then
    . /etc/default/xserver
fi

LOGO_ROTATION="$XSERVER_ROTATION"

if [ ! "$LOGO_ROTATION" ]; then
    LOGO_ROTATION=`nanddump -o -b -l 4096 -f - /dev/mtd8 2>/dev/null | grep "logo_rotation" | sed s/"^.*="/""/`

    if [ ! "$LOGO_ROTATION" ]; then
	LOGO_ROTATION="0"
    fi
fi

"$PRIVATE_DIR"/bmp2fb /dev/fb0 "-$LOGO_ROTATION" "$PRIVATE_DIR/id-update-$LOGO_ROTATION.bmp" >/dev/null 2>&1

sleep 2

### Get version BOOT and OS

if [ -f /etc/.mbinfo ]; then
    . /etc/.mbinfo
fi

VERSION_BOOT="0"

if [ -f /etc/.version ]; then
    VERSION_BOOT=`cat /etc/.version`
    VERSION_BOOT=`printf "%.1f" $VERSION_BOOT  2>/dev/null | sed s/'\.'/''/`
fi

VERSION_OS="0"

if [ -f /mnt/root/etc/.version ]; then
    VERSION_OS=`cat /mnt/root/etc/.version`
    VERSION_OS=`printf "%.1f" $VERSION_OS 2>/dev/null | sed s/'\.'/''/`
fi

BOOT=`hostname | grep 'Boot'`

case "$HW_Version" in
    "0100")     uImage="uImage_0100"
		;;
    "0200"|"0300"|"0301")
		uImage="uImage_0200"
		;;
	*)
		echo "Unsupported HW version $HW_Version!"
		exit 1
		;;
esac

### Update MLO/Boot

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)
    nandwrite -p /dev/mtd$i $CONTENT_DIR/MLO > /dev/null 2>&1 || (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)

nandwrite -p /dev/mtd4 $CONTENT_DIR/u-boot.img > /dev/null 2>&1 || (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)

nandwrite -p /dev/mtd6 $CONTENT_DIR/$uImage > /dev/null 2>&1 || (echo "Unable to write uImage to mtdblock6!"; exit 1)

if [ "$BOOT" ]; then
    mount $INSTALL_DIR/data >/dev/null 2>&1

    if [ -f /mnt/data/.bklmon ]; then
	cp -af /mnt/data/.bklmon $CONTENT_DIR/data/.bklmon
    fi

    if [ -f /mnt/data/.bootlog ]; then
	cp -af /mnt/data/.bootlog $CONTENT_DIR/data/.bootlog
    fi

    if [ -f /mnt/data/.mac ]; then
	cp -af /mnt/data/.mac $CONTENT_DIR/data/.mac
    fi

    if [ -f /mnt/data/.update.log ]; then
	cp -af /mnt/data/.update.log $CONTENT_DIR/data/.update.log
    fi

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

    if [ `expr $VERSION_BOOT \< 21` == 1 ]; then
	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)
    else
	flash_eraseall /dev/mtd9 > /dev/null 2>&1 || (echo "Unable to erase 'mtd9' flash memory!"; exit 1)
	flash_eraseall /dev/mtd10 > /dev/null 2>&1 || (echo "Unable to erase 'mtd10' flash memory!"; exit 1)

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

### Backup of MAC address

if [ `expr $VERSION_OS \< 60` == 1 ]; then
    if [ ! -f /mnt/data/.mac ]; then
	MAC=`genmac`
	echo "$MAC" > /mnt/data/.mac
    fi
fi

### Backup Data filesystem

if [ ! "$BOOT" ]; then
    if [ `expr $VERSION_BOOT \< 21` == 1 ]; then
	tar cfz /mnt/root/.backup.tgz /mnt/data
    fi
fi

### Fixup Boot Logo

LOGO_CONFIG="/tmp/.logo.cfg"

echo "logo_rotation=$LOGO_ROTATION" > $LOGO_CONFIG

if [ `expr $VERSION_BOOT \< 21` == 1 ]; then
    unmount $INSTALL_DIR/data >/dev/null 2>&1

    flash_eraseall /dev/mtd7 >/dev/null 2>&1

    nandwrite -p -s 0x00000000 /dev/mtd7 "$PRIVATE_DIR/logo_teco_$LOGO_ROTATION.bmp" >/dev/null 2>&1
    nandwrite -p -s 0x00400000 /dev/mtd7 $LOGO_CONFIG >/dev/null 2>&1
else
#    flash_eraseall /dev/mtd7 >/dev/null 2>&1
#    nandwrite -p /dev/mtd7 "$PRIVATE_DIR/logo_teco_$LOGO_ROTATION.bmp" >/dev/null 2>&1

    flash_eraseall /dev/mtd8 >/dev/null 2>&1
    nandwrite -p /dev/mtd8 $LOGO_CONFIG >/dev/null 2>&1
fi

### Update Root/Data filesystem

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

if [ -f /etc/resolv.conf ]; then
    rm -f $CONTENT_DIR/root/etc/resolv.conf
fi

if [ -f /etc/stunnel/stunnel.conf ]; then
    rm -f $CONTENT_DIR/root/etc/stunnel/stunnel.conf
fi

if [ -f /etc/wpa_supplicant/wpa_supplicant.conf ]; then
    rm -f $CONTENT_DIR/root/etc/wpa_supplicant/wpa_supplicant.conf
fi

if [ -f /etc/wvdial.conf ]; then
    rm -f $CONTENT_DIR/root/etc/wvdial.conf
fi

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

if [ -f /etc/default/minibrowser ]; then
    . /etc/default/minibrowser

    if [ ! "`echo $LOG_FILE | grep '/var/log'`" ]; then
	rm -f $CONTENT_DIR/root/etc/default/minibrowser
    fi
fi

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

if [ -f /etc/default/plccoms ]; then
    . /etc/default/plccoms

    if [ ! "`echo $LOG_FILE | grep '/var/log'`" ]; then
	rm -f $CONTENT_DIR/root/etc/default/plccoms
    fi
fi

if [ -f /etc/default/plcsync ]; then
    . /etc/default/plcsync

    if [ ! "`echo $LOG_FILE | grep '/var/log'`" ]; then
	rm -f $CONTENT_DIR/root/etc/default/plcsync
    fi
fi

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

if [ -f /etc/default/webserver ]; then
    rm -f $CONTENT_DIR/root/etc/default/webserver
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 $INSTALL_DIR/root/opt/MiniBrowser/MiniBrowser.ini ]; then
    if [ ! -d $INSTALL_DIR/root/opt ]; then
	mkdir $INSTALL_DIR/root/opt
    fi
    if [ ! -d $INSTALL_DIR/root/opt/MiniBrowser ]; then
	mkdir $INSTALL_DIR/root/opt/MiniBrowser
    fi

    cp -af $CONTENT_DIR/root/opt/MiniBrowser/MiniBrowser.ini $INSTALL_DIR/root/opt/MiniBrowser/.
fi

#if [ ! -f /opt/MiniBrowser/MiniBrowser-default.ini ]; then
    cp -af $CONTENT_DIR/root/opt/MiniBrowser/MiniBrowser.ini $INSTALL_DIR/root/opt/MiniBrowser/MiniBrowser-default.ini

    MINIBROWSER_CONFIG="/opt/MiniBrowser/MiniBrowser-default.ini"

    CFG=`cat $MINIBROWSER_CONFIG`

#    CFG=`echo "$CFG" | sed '/^serial_device/s/ttyO4/ttyS4/'`

    if [ "$HW_Version" = "0300" -o "$HW_Version" = "0301" ]; then
	if [ "`cat $MINIBROWSER_CONFIG | grep 'connection_list' | grep 'TCL2'`" = "" ]; then
	    CFG=`echo "$CFG" | sed '/^connection_list/s/$/|TCL2/'`
#	    CFG=`echo "$CFG" | sed '/^serial_speed/a tcl2_address                   = Rack 3, pos 8'`
	    CFG=`echo "$CFG" | sed '/^serial_device/a tcl2_device                    = /dev/ttyO1'`
	    CFG=`echo "$CFG" | sed '/^tcl2_device/a tcl2_address_list              = Rack 3, pos 8|Rack 3, pos 9|Rack 3, pos 10|Rack 3, pos 11|Rack 3, pos 12|Rack 3, pos 13'`
	fi
    fi

    CFG=`echo "$CFG" | sed '/^fullscreen/s/0/1/'`

    echo "$CFG" > $MINIBROWSER_CONFIG
#fi

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

    MINIBROWSER_CONFIG="/opt/MiniBrowser/MiniBrowser.ini"

    CFG=`cat $MINIBROWSER_CONFIG`

#    CFG=`echo "$CFG" | sed '/^serial_device/s/ttyO4/ttyS4/'`

    if [ "$HW_Version" = "0300" -o "$HW_Version" = "0301" ]; then
	if [ "`cat $MINIBROWSER_CONFIG | grep 'connection_list' | grep 'TCL2'`" = "" ]; then
	    CFG=`echo "$CFG" | sed '/^connection_list/s/$/|TCL2/'`
#	    CFG=`echo "$CFG" | sed '/^serial_speed/a tcl2_address                   = Rack 3, pos 8'`
	    CFG=`echo "$CFG" | sed '/^serial_device/a tcl2_device                    = /dev/ttyO1'`
	    CFG=`echo "$CFG" | sed '/^tcl2_device/a tcl2_address_list              = Rack 3, pos 8|Rack 3, pos 9|Rack 3, pos 10|Rack 3, pos 11|Rack 3, pos 12|Rack 3, pos 13'`
	fi
    fi

    CFG=`echo "$CFG" | sed '/^fullscreen/s/0/1/'`

    echo "$CFG" > $MINIBROWSER_CONFIG
fi

if [ -f /mnt/root/sbin/wdtd ]; then
    rm -f /mnt/root/sbin/wdtd
fi

if [ -f /mnt/root/bin/xsaverfix ]; then
    rm -f /mnt/root/bin/xsaverfix
fi

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

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

bzip2 -d -f $INSTALL_DIR/root/bin/*.bz2
bzip2 -d -f $INSTALL_DIR/root/share/fonts/truetype/*.bz2

### Naprogramujeme stm32
cd /opt/stm32
./flash_tfw >/dev/null 2>&1 || echo "Unable to program STM32 flash memory!"

sync

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

killall -USR1 rebootd

#/sbin/reboot

exit 0
