Corbinhol

Wrapper

Jul 22nd, 2024 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. --This is the starter wrapper. Runs the main program whenever the user types "cmauto" into their computer and also handles outside variables.
  2. local shell = require("shell");
  3. local filesystem = require("filesystem");
  4. local serialization = require("serialization");
  5.  
  6. local args = {...};
  7. local startingDir = shell.getWorkingDirectory();
  8. local resetDir = true;
  9. threadList = {};
  10.  
  11. local CFG_FILE_LOC = "/usr/CM_Automator/launcher-config.cfg";
  12. local cfg_data = {};
  13. shouldBeRunning = false;
  14.  
  15. local function save()
  16.     local file = io.open(CFG_FILE_LOC, "w");
  17.     file:write(serialization.serialize(cfg_data));
  18.     file:close();
  19. end
  20.  
  21. local function load()
  22.     local file = io.open(CFG_FILE_LOC, "r");
  23.     cfg_data = serialization.unserialize(file:read("*a"));
  24.     file:close();
  25. end
  26.  
  27. if filesystem.exists(CFG_FILE_LOC) then
  28.     load();
  29. else
  30.     cfg_data["autostart"] = true;
  31.     save();
  32. end
  33.  
  34.  
  35. local function killAllThreads()
  36.     if #threadList > 0 then
  37.         for i, t in pairs(threadList) do
  38.             t:kill();
  39.         end
  40.     end
  41. end
  42.  
  43. if args[1] == "-i" then
  44.     print("Detected installer argument. Running installer.");
  45.     os.execute("/usr/CM_Automator/installer.lua -d");
  46.     -- os.execute("/usr/CM_Automator/installer");
  47. elseif args[1] == "dir" then
  48.     shell.setWorkingDirectory("/usr/CM_Automator/");
  49.     resetDir = false;
  50. elseif args[1] == "-r" then
  51.     print("Resetting all data.");
  52.     os.execute("rm /usr/CM_Automator/data.dat");
  53. elseif args[1] == "-a" then
  54.     if args[2] == "true" then
  55.         cfg_data["autostart"] = true;
  56.         save();
  57.         print("Set autostart to true!");
  58.     elseif args[2] == "false" then
  59.         cfg_data["autostart"] = false;
  60.         save();
  61.         print("Set autostart to false!");
  62.     elseif args[2] == nil then
  63.         if cfg_data["autostart"] == false then
  64.             cfg_data["autostart"] = true;
  65.             save();
  66.             print("Set autostart to true!");
  67.         else
  68.             cfg_data["autostart"] = false;
  69.             save();
  70.             print("Set autostart to false!");
  71.         end
  72.     end
  73. else
  74.     --This needs to be in here for requires to work in the program.
  75.     shouldBeRunning = true;
  76.     shell.setWorkingDirectory("/usr/CM_Automator/");
  77.     shell.execute("/usr/CM_Automator/main.lua");
  78. end
  79.  
  80. --Resets working directory back to home
  81. killAllThreads();
  82. threadList = nil;
  83. if resetDir then
  84.     shell.setWorkingDirectory(startingDir);
  85. end
  86.  
  87. if shouldBeRunning then
  88.     print("Error. Program appears to have crashed. Disabling autostart.");
  89.     cfg_data["autostart"] = false;
  90.     save();
  91. end
  92.    
  93. package.loaded.console = nil;
Advertisement
Add Comment
Please, Sign In to add comment