#include "__include.h"
#include "Exception.h"
#include "File.h"

namespace jet {
   
   __include::__include(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) {
      if(!keywordDefined("file"))
	throw coreutils::Exception("file keyword must be specified.");
      if(hasContainer)
	throw coreutils::Exception("include tag should not have a container.");
      hasContainer = true;
      coreutils::File file(resolveKeyword("file"));
      file.read();
      container = file.asZString();
      try {	   
	 processContainer(container);
      }
      catch(coreutils::Exception e) {
	 container.setCursor(global.errorCursor);
	 container.moveBackToLineStart();
	 std::cout << "-----------------------------" << std::endl;
	 std::cout << "Error in included script '" << variables["file"] << "' at line " << container.getLineNumberAtCursor() << std::endl;
	 std::cout << "Error text: " << e.text << std::endl;
	 std::cout << "-----------------------------" << std::endl;
	 std::cout << container.parsed() << std::endl;
	 std::cout << "******** Error caught: " << e.text << std::endl;
	 std::cout << container.unparsed() << std::endl;
	 global.errorCursor = in.getCursor();
	 throw coreutils::Exception("error in included file.");
      }
      
   }

}
