process.stdout.write("."); const fs = require('fs'); const path = require('path'); const chalk = require("chalk"); global._error = require('./error'); process.stdout.write("."); global.systemRoot = path.resolve(__dirname + "/.."); global.systemLog = path.resolve(systemRoot + "/../Volumes/VM/CurrentLog.log"); global.hostname = "(none)"; global.currentStage = 0; global.uiProcess = null; global.osVersion = fs.readFileSync(systemRoot + "/MistyOSVersion").toString().trim(); process.stdout.write("."); process.on('uncaughtException', (error) => { try { log("MistyCore", "An internal exception occurred."); log("MistyCore", error.stack); log("MistyCore", "Aborting system operation"); } catch (e) {} try { fs.copyFileSync(systemLog, systemRoot + "/Logs/LastCrash.log"); } catch (e) {} try { require('child_process').execFileSync("/System/Binaries/sh", [ "-c", "echo 's' > /System/Volumes/FieldService/sysrq-trigger" ]); } catch (e) {} console.clear(); for (let i = 0; i < process.stdout.rows - 1; i++) { process.stdout.write(chalk.bgYellow.white(" ".repeat(process.stdout.columns))); } process.stdout.cursorTo(0, 0); _error(error, currentStage, osVersion); global.halt(); }) process.stdout.write("."); global.halt = () => { let v = true; while (v) {} } process.stdout.write("."); global.log = (service, text) => { let lines = text.split("\n"); for (let line of lines) { fs.appendFileSync(systemLog, "\n" + new Date().toDateString().substring(4, 10) + " " + new Date().toTimeString().split(" ")[0] + " " + hostname + " " + service + ": " + text); } } process.stdout.write("."); if (!fs.existsSync(systemRoot + "/../Volumes")) fs.mkdirSync(systemRoot + "/../Volumes"); if (!fs.existsSync(systemRoot + "/../Volumes/VM")) fs.mkdirSync(systemRoot + "/../Volumes/VM"); fs.writeFileSync(systemLog, ""); process.stdout.write("."); log("MistyCore", "Setting runtime variables..."); fs.writeFileSync(systemRoot + "/../Volumes/VM/SystemRoot", systemRoot); fs.writeFileSync(systemRoot + "/../Volumes/VM/SystemLog", systemLog); fs.writeFileSync(systemRoot + "/../Volumes/VM/Hostname", hostname); fs.writeFileSync(systemRoot + "/../Volumes/VM/Version", fs.readFileSync(systemRoot + "/MistyOSVersion").toString().trim()); fs.mkdirSync(systemRoot + "/../Volumes/VM/LaunchDaemons"); fs.mkdirSync(systemRoot + "/../Volumes/VM/LaunchDaemonsTimes"); log("MistyCore", "Done setting runtime variables"); process.stdout.write("."); global.services = require('./service'); global.socket = require('./socket'); process.stdout.write("."); log("MistyCore", "Creating launch daemons wrappers..."); let servicesList = services.list(); for (let name in servicesList) { fs.writeFileSync(systemRoot + "/../Volumes/VM/LaunchDaemons/" + name, "-1"); } log("MistyCore", "Done creating launch daemons wrappers"); log("MistyCore", "Starting MistyCore on MistyOS " + fs.readFileSync(systemRoot + "/MistyOSVersion").toString().trim()); process.stdout.write("."); console.clear(); log("MistyCore", "Showing banner"); require('./banner')(); console.log(""); console.log("MistyOS is starting up..."); process.stdout.write(" Initializing MistyCore...\n"); log("MistyCore", "Done showing banner"); log("MistyCore-Startup", "Running startup items..."); for (let unit of fs.readdirSync(systemRoot + "/StartupItems")) { let startupItemStage = parseInt(unit.substring(0, 1)); if (startupItemStage !== currentStage) { services.reachTarget(currentStage); currentStage = startupItemStage; } log("MistyCore-Startup", "Starting item: " + systemRoot + "/StartupItems/" + unit); let unitName = unit.split("--")[1]; let unitNamePretty = unitName.substring(0, unitName.length - 3); process.stdout.write(" Running " + unitNamePretty + "..."); try { log("MistyCore-Startup", "Executing file: " + systemRoot + "/StartupItems/" + unit); require(systemRoot + "/StartupItems/" + unit); log("MistyCore-Startup", "Registered success for item: " + systemRoot + "/StartupItems/" + unit); process.stdout.write(chalk.green(" Success") + "\n"); } catch (e) { log("MistyCore-Startup", "Registered failure for item: " + systemRoot + "/StartupItems/" + unit); log("MistyCore-Startup", e.stack); process.stdout.write(chalk.yellow(" Failure") + "\n"); console.log(""); console.log(chalk.red.inverse("MistyOS is unable to start up.")); console.log(""); console.log(chalk.red("A failing startup item is preventing MistyOS from starting up correctly.")); console.log(" " + chalk.red("Startup item: " + unitNamePretty)); console.log(" " + chalk.red("Error message: " + e.message)); console.log(""); console.log(chalk.red("Try starting your device in safe mode by holding Shift during start up.")); global.halt(); } } services.reachTarget(9); if (!global.uiProcess) { console.log(""); console.log(chalk.red.inverse("MistyOS does not have a user interface.")); console.log(""); console.log(chalk.red("MistyOS has started successfully, but no user interface has been started.")); console.log(" " + chalk.red("Wanted startup item: /System/Library/StartupItems/999--Shell.js")); console.log(" " + chalk.red("Try reinstall MistyOS if this is not expected.")); console.log(""); console.log(chalk.red("Try starting your device in safe mode by holding Shift during start up.")); global.halt(); } setInterval(() => {})