#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;

int
System(const string & cmd) 
{
   int ret;
   cout << cmd << endl;
   ret = system(cmd.c_str());
   if(ret != 0) throw "compiler driver error...";
   return ret;
}

int
main(int argc, char ** argv)
{
   string input,output = "a.out";
   bool is_kernel = false;
   for(int i = 1; i < argc; i++) {
      if(strcmp(argv[i], "-o") == 0) {
         i++;
         if(i < argc) {
            output = argv[i];
         }
      }
      else if(strcmp(argv[i], "-kernel") == 0) {
         is_kernel = true;
      }
      else {
         input = argv[i];
      }
   }
   string c;
#ifdef __WIN32__
   c = "c";
#else
   c = "./c";
#endif
   try {
      System("mcpp " + input + " -o " + input + ".tmp");
      if(is_kernel) {
         System(c+" " + input + ".tmp " + output + "2.asm -kernel");
      }
      else {
         System(c+" " + input + ".tmp " + output + "2.asm");
      }
      System("nasm -f bin " + output + "2.asm -o " + output);
   }
   catch(const char * message) {
      cerr << "exception: " << message << endl;
      exit(1);
   }
   return 0;
}
