#include "__header.h"
#include "Exception.h"
#include "Operand.h"
#include <iostream>

namespace jet {
   
   __header::__header(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) {
      output = false;
      if(!variableDefined("name"))
	throw coreutils::Exception("header tag must have name defined.");
      if(!variableDefined("expr") && variableDefined("value") && hasContainer)
	throw coreutils::Exception("header tag cannot have both value and a container.");
      if(variableDefined("expr") && !variableDefined("value") && hasContainer)
	throw coreutils::Exception("header tag cannot have both expr and a container.");
      if(variableDefined("expr") && variableDefined("value") && !hasContainer)
	throw coreutils::Exception("header tag cannot have both expr and value.");
      if(!variableDefined("expr") && !variableDefined("value") && !hasContainer)
	throw coreutils::Exception("header tag must have a value, expr or a container."); 
      if(variableDefined("expr")) {
	 if(variableDefined("eval"))
	   throw coreutils::Exception("Cannot use eval with expr.");	 
	 global.headers[variables["name"]] = Operand(variables["expr"]).string;	    
      } else if(hasContainer) {
	 processContainer(container);
	 if(evaluate) {
	    global.headers[variables["name"]] = out;	       
	 } else {	   
	    global.headers[variables["name"]] = container;
	 }	    
      } else
	global.headers[variables["name"]] = variables["value"];
     
   }      
}
