// 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.

// basBuild.h -- shared "compile a project into an .app" back end
//
// Both the DVX BASIC IDE (ideMain.c, Make Executable) and the standalone
// command-line compiler (bascomp.c) turn a compiled module plus a set of
// .frm texts into a finished .app.  The whole back half of that job --
// release stripping, form/control name obfuscation, bytecode compaction,
// serialization, stub extraction, resource emission, help-file copy and
// the "noicon" fallback -- lives here so both front ends produce
// byte-identical artifacts.
//
// Callers own every buffer they pass in via BasBuildSpecT; the module is
// never modified (release builds work on a private copy).

#ifndef BAS_BUILD_H
#define BAS_BUILD_H

#include "runtime/vm.h"

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

// Progress callback (may be NULL); receives one line per pipeline stage.
typedef void (*BasBuildLogFnT)(const char *msg);

typedef struct {
    // Project metadata (any NULL/empty is skipped; projName falls back
    // to a generic label).
    const char *projName;
    const char *author;
    const char *publisher;
    const char *version;
    const char *copyright;
    const char *description;

    // Project directory; iconPath and helpFile are relative to it.
    const char *projectDir;
    const char *iconPath;       // NULL/empty: embed the NOICON fallback
    const char *helpFile;       // NULL/empty: no help file

    // Path of the running executable; it carries the STUB and NOICON
    // resources.
    const char *selfPath;

    // Compiled module.  Never modified.
    const BasModuleT *module;

    // Raw .frm texts (NUL-terminated), one per form.  Comments are
    // stripped here; release builds additionally obfuscate them.
    const char *const *frmSources;
    int32_t            frmCount;

    bool           release;
    BasBuildLogFnT log;         // may be NULL
} BasBuildSpecT;

// Build outPath from spec.  Returns NULL on success, otherwise a static
// error message.  On failure outPath may be left partially written.
const char *basBuildApp(const char *outPath, const BasBuildSpecT *spec);

#ifdef __cplusplus
}
#endif

#endif // BAS_BUILD_H
