// A quick little utility to run on Windows to capture the command-line that Vide used when invoking aslink,
// so that I could accurately recreate the binaries that Vide creates on Linux when not using Vide.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

int main(void)
{
    const char *logPath = "C:\\Users\\gtoal\\Downloads\\Vide2.6_RC03.w64\\Vide.w64\\C\\Win32\\bin\\aslink.log";
    FILE *f = fopen(logPath, "a");
    if (f) {
        SYSTEMTIME st;
        GetLocalTime(&st);

        // Get full command line including program name
        LPCWSTR cmdW = GetCommandLineW();
        int len = WideCharToMultiByte(CP_UTF8, 0, cmdW, -1, NULL, 0, NULL, NULL);
        if (len > 0) {
            char *cmdUtf8 = (char *)HeapAlloc(GetProcessHeap(), 0, len);
            if (cmdUtf8) {
                WideCharToMultiByte(CP_UTF8, 0, cmdW, -1, cmdUtf8, len, NULL, NULL);

                fprintf(f,
                        "==============================\n"
                        "%04d-%02d-%02d %02d:%02d:%02d.%03d\n"
                        "Cmd: %s\n\n",
                        st.wYear, st.wMonth, st.wDay,
                        st.wHour, st.wMinute, st.wSecond, st.wMilliseconds,
                        cmdUtf8);

                HeapFree(GetProcessHeap(), 0, cmdUtf8);
            }
        }
        fclose(f);
    }

    // Build path to real exe: same dir as this exe, name "realprog_real.exe"
    char selfPath[MAX_PATH];
    GetModuleFileNameA(NULL, selfPath, MAX_PATH);

    // Replace file name with "realprog_real.exe"
    // (simple version: find last backslash and overwrite after it)
    char realExePath[MAX_PATH];
    lstrcpynA(realExePath, selfPath, MAX_PATH);
    char *lastSlash = strrchr(realExePath, '\\');
    if (!lastSlash) {
        // Fallback: just use literal path if something goes wrong
        lstrcpynA(realExePath, "C:\\Users\\gtoal\\Downloads\\Vide2.6_RC03.w64\\Vide.w64\\C\\Win32\\bin\\aslink-bin.exe", MAX_PATH);
    } else {
        ++lastSlash; // move past '\'
        lstrcpyA(lastSlash, "aslink-bin.exe");
    }

    STARTUPINFOA si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    ZeroMemory(&pi, sizeof(pi));
    si.cb = sizeof(si);

    // Get mutable copy of command line
    LPSTR cmdLine = GetCommandLineA();
    // CreateProcess requires a writable buffer for lpCommandLine
    size_t cmdLen = lstrlenA(cmdLine) + 1;
    char *cmdBuf = (char *)HeapAlloc(GetProcessHeap(), 0, cmdLen);
    if (!cmdBuf) {
        return 1;
    }
    lstrcpyA(cmdBuf, cmdLine);

    // Let CreateProcess parse cmdBuf; realExePath is the module.
    BOOL ok = CreateProcessA(
        realExePath,   // lpApplicationName
        cmdBuf,        // lpCommandLine (full original command line)
        NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi
    );                  // [web:20][web:21][web:27]

    if (!ok) {
        HeapFree(GetProcessHeap(), 0, cmdBuf);
        return 1;
    }

    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    HeapFree(GetProcessHeap(), 0, cmdBuf);

    return 0;
}
