// SPDX-License-Identifier: MPL-2.0
#include "vybuild.hpp"
#include <string>
#include <filesystem>
#include <iostream>
using namespace std;
namespace fs=std::filesystem;
int main(int argc,char** argv) {
  if (argc<2) {
    cout<<"[VyBuild] Error: not enough arguments."<<endl;
    return -1;
  }
  string root_module_path=string(argv[1]);
  if (!fs::exists(root_module_path)) {
    cout<<"[VyBuild] Error: provided file doesn't exist."<<endl;
    return -1;
  }
  bool smart_cache=true;
  int bumb=1;
  if (argc>2) {
    for (int i=2;i<argc;i++) {
      if (string(argv[i])=="--purge-cache") {
        fs::remove_all(".vybuild_cache");
        fs::remove_all(".vybuild_scache");
        continue;
      }
      if (string(argv[i])=="--disable-smart-cache") {
        smart_cache=false;
        continue;
      }
      if (string(argv[i])!="") {
        vybuild::vars::set_global_variable("%"+to_string(bumb)+"%",string(argv[i]));
        bumb++;
      }
    }
  }
  auto root_module=vybuild::module::Module(root_module_path,true);
  vybuild::vars::set_global_variable("%rootcallerfolder%",fs::absolute(fs::path(root_module_path)).parent_path().string());
  vybuild::cache::init_cache(smart_cache);
  cout<<"[VyBuild] Running first module: "+root_module.name<<endl;
  auto status=root_module.run_module(true,false);
  if (status==vybuild::actions::ActionStatus::Success) vybuild::cache::clean_cache();
  return 0;
}
