/* Reading from the file is more convenient for actually working on
 * it. Inlining is more useful for releasing when we don't want to
 * deal with the hassle of distributing more files. */
char* findKernelSrc()
{
    char* kernelSrc = NULL;

    /* Try different alternatives */

    if (inlinedIntegralKernelSrc)
        return inlinedIntegralKernelSrc;

    kernelSrc = mwReadFileResolved("AllInOneFile.cl");
    if (kernelSrc)
        return kernelSrc;

    kernelSrc = mwReadFileResolved("integrals.cl");
    if (kernelSrc)
        return kernelSrc;

    kernelSrc = mwReadFile("../kernels/integrals.cl");
    if (kernelSrc)
        return kernelSrc;

    warn("Failed to read kernel file\n");

    return kernelSrc;
}
/* Open a lua_State, bind run information such as server arguments and
 * BOINC status, and evaluate input script. */
static lua_State* separationOpenLuaStateWithScript(const SeparationFlags* sf)
{
    char* script;
    lua_State* luaSt;

    luaSt = separationLuaOpen(FALSE);
    if (!luaSt)
        return NULL;

    script = mwReadFileResolved(sf->ap_file);
    if (!script)
    {
        perror("Opening Lua script");
        lua_close(luaSt);
        return NULL;
    }

    if (dostringWithArgs(luaSt, script, sf->forwardedArgs, sf->nForwardedArgs))
    {
        mw_lua_pcall_warn(luaSt, "Error loading Lua script '%s'", sf->ap_file);
        lua_close(luaSt);
        luaSt = NULL;
    }

    free(script);

    return luaSt;
}
/* Open a lua_State, bind run information such as server arguments and
 * BOINC status, and evaluate input script. */
static lua_State* separationOpenLuaStateWithScript(const SeparationFlags* sf)
{
    int failed;
    char* script;
    lua_State* luaSt;

    luaSt = separationLuaOpen(FALSE);
    if (!luaSt)
        return NULL;

    script = mwReadFileResolved(sf->ap_file);
    if (!script)
    {
        mwPerror("Opening Lua script '%s'", sf->ap_file);
        lua_close(luaSt);
        return NULL;
    }

    failed = tryEvaluateScript(luaSt, script, sf);
    free(script);

    if (failed)
    {
        lua_close(luaSt);
        return NULL;
    }

    return luaSt;
}