#!/bin/sh
# System Control for Openplus 
# GNU GPL2+

if [ ! -f /etc/.opinit ]; then
  sleep 20
  /usr/bin/passwd -d root
  echo "." > /etc/.opinit
fi

COUNTERSET=0
COUNTERIMG=0

while [ 1 ]; do
 ((COUNTER++))
 
 if [ -d /picon ]; then
	mv /picon /usr/share/enigma2
 fi 
 if [ $COUNTERSET = 86400 ]; then
        echo "testsettings \n" > /tmp/systemop00000001
        COUNTERSET=0
 elif [ $COUNTERIMG = 604800 ]; then
        echo "testupdate \n" > /tmp/systemop00000002
        COUNTERIMG=0
 fi

 for I in `ls /tmp/systemop*`; do
        F=$(cat $I | cut -d " " -f1) ;
        FU=$(cat $I | cut -d " " -f2-) ;

        rm -f $I
        case $F in
        # Test updates for Openplus and Openplus Settings

        testupdate)
               sync ; echo 3 > /proc/sys/vm/drop_caches
               opkg update
               opkg upgrade
               rm -f /tmp/list-upgradable
               opkg clean
                ;;

        testsettings)
                sync ; echo 3 > /proc/sys/vm/drop_caches
                opkg update
                opkg list | grep '/enigma2-plugin-settings-'  > /tmp/.list-upgradable
                for arg in `awk '/enigma2-plugin-settings/{print $1}' /tmp/.list-upgradable` ; do
                       echo "Installing $arg";
                       opkg install $arg  >/dev/null 2>&1 &
                done;
                opkg list | grep '/enigma2-plugin-picons-'  > /tmp/.list-upgradable
                for arg in `awk '/enigma2-plugin-picons/{print $1}' /tmp/.list-upgradable` ; do
                       echo "Installing $arg";
                       opkg install $arg  >/dev/null 2>&1 &
                       echo "." > /tmp/.systemop.update_settings
                done;

                rm -f /tmp/.list-upgradable
                ;;

        update)
                sync ; echo 3 > /proc/sys/vm/drop_caches
                opkg update  >/dev/null 2>&1 &
               ;;

        reset_root_passwd)
                /usr/bin/passwd -d root  >/dev/null 2>&1 &
                ;;

        run)
                $FU  >/dev/null 2>&1 &
                if [ $? -eq 0 ]; then
                echo $FU " command executed" 
                else
                echo "Error in command: " $FU > /tmp/.error
                fi
                ;;

        *)
                echo "systemop - Invalid Option: $F $FU" ;
                ;;      
        esac
 done
sleep 1s
done

