let fs = require('fs');
function ceval(cmd, channelid = "", guildid = "", author = "", callback) {
    if(cmd.includes("#include <string.h>")) cmd = cmd.replace("#include <string>", "");
    if(cmd.includes("#include <iostream>")) cmd = cmd.replace("#include <iostream>", "");
    if(cmd.includes("#include <fstream>")) cmd = cmd.replace("#include <fstream>", "");
    if(cmd.includes("#include <cstdio>")) cmd = cmd.replace("#include <cstdio>", "");
    var hasMain = false;
    cmd = cmd.split("\n");
    var headers = "";
    var code = "";
    for(i in cmd) {
        if(cmd[i].startsWith("#")) headers += cmd[i] + "\n";
        else {
            if(cmd[i].startsWith("int main(")) hasMain = true;
            code += cmd[i] + "\n";
        }
    }
    headers += "#include <string>\n#include <iostream>\n#include <fstream>\n#include <cstdio>";
    if (hasMain == false) {
        code = "int main(int argc, char *argv[]){\n" + code;
        if (code.includes("return")) {
            code = code + "\nreturn 0;";
        }
        code += "\n}";
    }
    if(headers.toLowerCase().includes("#include <sdl")) {
        callback("SDL is not allowed!");
        return;
    }
    code = "std::string channel_id = \"" + channelid + "\";\nstd::string guild_id = \"" + guildid + "\";\nstd::string author_id = \"" + author + "\";\n" + code;
    cmd = headers + "\n\n" + code;
    fs.writeFileSync("./temp/WRITEFILE.cpp", cmd);
    return require("./popen.node").popen("g++ -std=c++17 temp/WRITEFILE.cpp -o ./temp/WRITE 2>&1 && ./temp/WRITE");
}
module.exports = ceval;