#!/bin/ash /etc/rc.common
USE_PROCD=1
START=99
STOP=15

# Define variables for bundled packages and status file
STATUS_FILE="/usr/lib/opkg/status"

# Bundled package information to be added
BUNDLED_PACKAGES_INFO="
Package: libinotifytools
Version: 3.20.11.0-1
Depends: libc
Status: install user installed
Architecture: aarch64_cortex-a53
Installed-Time: $(date +%s)

Package: inotifywait
Version: 3.20.11.0-1
Depends: libc, libinotifytools
Status: install user installed
Architecture: aarch64_cortex-a53
Installed-Time: $(date +%s)
"

start_service() {
    echo "Starting add_opkg_status_bundled service in the background..."

    # Run the service in the background using procd
    procd_open_instance
    procd_set_param command /bin/sh -c "(
        echo 'Waiting for 4 seconds...'
        sleep 4

        # Append the package information to the status file
        echo 'Adding bundled package entries to $STATUS_FILE...'
        echo \"$BUNDLED_PACKAGES_INFO\" >> \"$STATUS_FILE\"
        
        # Sync filesystem to ensure changes are saved
        sync

        # Verify if the entries were added correctly
        if grep -q 'Package: libinotifytools' \"$STATUS_FILE\" && grep -q 'Package: inotifywait' \"$STATUS_FILE\"; then
            echo 'Successfully added bundled package entries to $STATUS_FILE'
        else
            echo 'Error: Failed to add bundled package entries to $STATUS_FILE'
        fi

        # Self-remove the init script after execution
        echo 'Cleaning up: Removing /etc/init.d/add_opkg_status_bundled'
        rm -f /etc/init.d/add_opkg_status_bundled
        echo 'add_opkg_status_bundled service completed and removed.'
        ) &"
    procd_close_instance
}

stop_service() {
    echo "Stopping add_opkg_status_bundled service (if running)..."
}

