#!/bin/bash

Install_dir="/media/sda1/deb/"
Trace_File="_update_usb.log"

wait_keypress()
{
        echo "Press any key to finish USB Update ..."
        while [ true ] ; do
          read -n 1
          if [ $? = 0 ] ; then
                exit 0;
        fi
        done
}

if [ -d "$Install_dir" ]; then
        count=`ls -1 $Install_dir*.deb 2>/dev/null | wc -l`
        if [ $count != 0 ]; then
        	/lib/matelex/screensaver stop
                /usr/bin/oled_screen -d1 -c -f2 -m "* UPGRADE *~Start USB Upd"
                socuid=$(cat /sys/devices/soc0/soc_uid)
                echo "Check/Start USB update..."
                date +"Year: %Y, Month: %m, Day: %d" >> $Install_dir$socuid$Trace_File

                for FILE in $Install_dir*.deb; do
                        echo "Install update $FILE" >> $Install_dir$socuid$Trace_File;
                        /usr/bin/dpkg --install $FILE >> $Install_dir$socuid$Trace_File;
                done

                sync
                echo "USB update finished !"
                /usr/bin/oled_screen -d1 -c -f2 -m "Press Key~End USB Upd"
                wait_keypress
        fi
#else echo "no update on USB"
fi

exit 0
