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

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

NET_CONFIG="/etc/network/interfaces"

export PATH=$PATH:$PRIVATE_DIR

### Show update splash image (Lock screen)

if [ "$DISPLAY" = "" ]; then
    export DISPLAY=:0
fi

/sbin/killall -9 mloop >/dev/null 2&>1
/sbin/killall -9 ploop >/dev/null 2&>1
/sbin/killall -9 MiniBrowser >/dev/null 2&>1
/sbin/killall -9 PLCComS >/dev/null 2&>1

xli -fullscreen $PRIVATE_DIR/id-update.jpg >/dev/null 2&>1 &

sleep 2

xset s reset

### Update Linux Kernel

VERSION_LINKE=`cat /proc/version | grep "TECO" | awk {'print $3'}`

if [ `expr $VERSION_LINKE \< 2.0` == 1 ]; then
    /sbin/insmod $PRIVATE_DIR/mtd_rw.o minor=3 flag=0

    cat $PRIVATE_DIR/uImage-MP1x-IDx8-20.bin >/dev/mtdblock3

    if [ $? == 1 ]; then
	exit 1
    fi
fi

### Update Root filesystem

rm -f /home
rm -f /mnt
rm -f /root
rm -f /var

rm -f /etc/adjtime
rm -f /etc/hostname
rm -f /etc/motd

rm -f /usr/share/fonts/X11/75dpi/sym*
rm -f /usr/share/fonts/X11/75dpi/tim*

mkfontdir /usr/share/fonts/X11/75dpi
mkfontscale /usr/share/fonts/X11/75dpi

if [ -L /etc/resolv.conf ]; then
    TMP=`cat /etc/resolv.conf`
    rm -f /etc/resolv.conf
    echo "$TMP" > /etc/resolv.conf
fi

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

rm -f /etc/network/run
rm -f /etc/rc6.d/S25bootsplash-00

rm -f /usr/sbin/fbset

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

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

    CFG=`cat $MINIBROWSER_CONFIG`

    if [ ! "`echo "$CFG" | grep '^connection_type'`" ]; then
	APPEND="${APPEND}connection_type                = Ethernet\n"
    fi

    if [ ! "`echo "$CFG" | grep '^serial_speed'`" ]; then
	APPEND="${APPEND}serial_speed                   = 115200\n"
    fi

    if [ ! "`echo "$CFG" | grep '^tcl2_address'`" ]; then
	APPEND="${APPEND}tcl2_address                   = CH5\n"
    fi

    CFG=`echo "$CFG" |sed /'\[plc\]'/,/'^$'/s/'^$'/"$APPEND"/`

    if [ ! "`echo "$CFG" | grep '^panel_address'`" ]; then
	CFG=`echo "$CFG" | sed '/^project_scaling *=/a panel_address                  = 0'`
    fi

    if [ ! "`echo "$CFG" | grep '^setup_password'`" ]; then
	CFG=`echo "$CFG" | sed '/^panel_address *=/a setup_password                 = '`
    fi

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

    echo "$CFG" > $MINIBROWSER_CONFIG
fi

if [ -f /opt/PLCComS/PLCComS.ini ]; then
    rm -f $CONTENT_DIR/opt/PLCComS/PLCComS.ini
#    cp -a /opt/PLCComS/PLCComS.ini $CONTENT_DIR/opt/PLCComS/PLCComS.ini
fi

#rm -rf /opt/MiniBrowser
#rm -rf /opt/PLCComS

cp -af $CONTENT_DIR/* $INSTALL_DIR/

ret=$?

/sbin/depmod -a

TMP_CONFIG=`cat $NET_CONFIG | sed s/" (broadcast a gateway jsou voliteln)"/""/`

echo "$TMP_CONFIG" > $NET_CONFIG

sync

/sbin/reboot

exit $ret
