// The MIT License (MIT)
//
// Copyright (C) 2026 Scott Duensing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// 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 ===");
}
