// shellInfo.c  -- System information wrapper for DVX Shell
//
// Delegates hardware detection to the platform layer via
// platformGetSystemInfo(), then logs the result line-by-line
// to DVX.LOG.  The result pointer is cached so subsequent calls
// to shellGetSystemInfo() return instantly without re-probing.

#include "shellInf.h"
#include "shellApp.h"
#include "dvxPlat.h"

#include <string.h>
#include "dvxMem.h"

// ============================================================
// Module state
// ============================================================

static const char *sCachedInfo = NULL;

// ============================================================
// Prototypes
// ============================================================

const char *shellGetSystemInfo(void);
void        shellInfoInit(AppContextT *ctx);


const char *shellGetSystemInfo(void) {
    return sCachedInfo ? sCachedInfo : "";
}


void shellInfoInit(AppContextT *ctx) {
    sCachedInfo = platformGetSystemInfo(&ctx->display);

    // Log each line individually so the log file is readable
    dvxLog("=== System Information ===");

    const char *line = sCachedInfo;

    while (*line) {
        const char *eol = strchr(line, '\n');

        if (!eol) {
            dvxLog("%s", line);
            break;
        }

        int32_t len = (int32_t)(eol - line);
        char tmp[256];

        if (len >= (int32_t)sizeof(tmp)) {
            len = sizeof(tmp) - 1;
        }

        memcpy(tmp, line, len);
        tmp[len] = '\0';
        dvxLog("%s", tmp);
        line = eol + 1;
    }

    dvxLog("=== End System Information ===");
}
