#!/bin/sh

# Author: Reis Ciaramitaro
# Company: Inspectron Inc. 2025
# Description: 
#     Script that checks for the application partition located in /mnt/userdata to compelete a SW update.
#     Will execute a recovery check if it exists after the update
# Usage:
#     name this script S100<name> and put it in the /etc/init.d folder
#     ensure the permissions are set to 0755
#
# Licensed under terms of GPLv2

# console colors
RED='\033[0;31m'
ORNG='\033[0;33m'
NC='\033[0;0m'

case "$1" in
        start)

                if [ -f "/mnt/app/scripts/launchWeston.sh" ]; then
                        # launch weston with custom config file if it exists
                        /mnt/app/scripts/launchWeston.sh
                        # sleep to allow weston to launch
                        sleep 1
                fi


                # finish the updates if the application partition is present on the system
                if [ -f "/mnt/userdata/application.ext4" ]; then
                        echo -e "${ORNG} Finishing updates ${NC}"
                        umount -f /dev/mmcblk1p7
                        dd if=/mnt/userdata/application.ext4 of=/dev/mmcblk1p7
                        rm -rf /mnt/userdata/application.ext4
                        mount /dev/mmcblk1p7 /mnt/app
                fi

                # execute recovery for update cleanup or file recovery
                if [ -d "/mnt/recovery" ]; then
                        echo -e "${RED} Launching inspectron recovery check ${NC}"
                        killall InspRecovery
                        /mnt/recovery/InspRecovery &
                fi
                ;;
        stop)
                ;;
        restart|reload)
                $0 stop
                $0 start
                ;;
        *)
                echo -e "${RED} Usage: $0 {start|stop|restart} ${NC}"
                exit 1
esac

exit 0
