anchor.diff:  %% for configuration files in libraries.
   %% @end
   start() ->
  -    case config_sanity_check() of
  +    case engine_config_check() of
   	ok ->
  -	    ;
  +	    ?DEBUG("Engine config file check passed");
   	error ->
  -	    erlang:exit(config_file_error)
  -    end,
  -    %% erlastic_search_config(),
  -    %% rabbit_mq_config(),
  -    %% webmachine_config(),
  +	    ?ERROR("Engine config file check failed. Aborting config mechanism."),
  +	    erlang:exit(engine_config_file_error)
  +    end,
  +    case elastic_search_config_check() of
  +	ok ->
  +	    ?DEBUG("Elastic_search config file check passed");
  +	error ->
  +	    ?ERROR("Elastic_search config file check failed. Aborting config mechanism."),
  +	    erlang:exit(elastic_config_file_error)
  +    end,
  +    case erlastic_search_config_check() of
  +	ok ->
  +	    ?DEBUG("Erlastic_search config file check passed");
  +	error ->
  +	    ?ERROR("Erlastic_search config file check failed. Aborting config mechanism."),
  +	    erlang:exit(erlastic_config_file_error)
  +    end,
  +    case rabbitmq_config_check() of
  +	ok ->
  +	    ?DEBUG("RabbitMQ config file check passed");
  +	error ->
  +	    ?ERROR("RabbitMQ config file check failed. Aborting config mechanism."),
  +	    erlang:exit(rabbitmq_config_file_error)
  +    end,
  +    
  +    engine_and_nodejs_config(),
  +    elastic_search_config(),
  +    erlastic_search_config(),
  +    rabbit_mq_config(),
  +
  +    %% Webmachine configuration settings was altered in src/engine_sup.erl to use the the application
  +    %% environment variables which is loaded when the project is started. 
  +    %% To set webmachine config settings it is only the config/engine.config which needs to be edited.
       init:stop().
   
  -config_sanity_check() ->
  +%% @doc
  +%% Check validity of the projects config file. Make sure that all environment variables is loaded.
  +%% @end
  +engine_config_check() ->
       case file:get_cwd() of
   	{ok, CWD} ->
   	    File = CWD ++ "/config/engine.config",
  
positive.code: pair([A, B| Tl], Acc) ->
      pair(Tl, [{A,B} | Acc]).
  
  %% @doc
  %% Runs the configuration mechanism for the elastic search and erlastic libraries
  %% @end
  -spec erlastic_search_config() -> ok | error.
  erlastic_search_config() ->
      case file:get_cwd() of
  	{ok, CWD} ->
  	    File = CWD ++ "/lib/erlastic_search/include/erlastic_search.hrl",
  	    Lines = read_file_lines(File),
  	    StrippedLines = lists:map(fun string:strip/1, Lines),
  	    Fun = fun("host"++Line, Acc) ->
  			  NewLine = case application:get_env(engine, es_ip) of
  					{ok, Ip} ->				  
  					    "host = \""++ Ip ++ "\":: string(),\n";
  					_ ->
  					    ?ERROR("Environment variable 'es_ip' is not set"),
  					    "host"++Line
  				    end,
  			  [NewLine | Acc];
  		     ("port"++Line, Acc) ->
  			  NewLine = case application:get_env(engine, es_port) of
  					{ok, Port} ->			
  					    "port = "++ integer_to_list(Port) ++ ":: integer(),\n";
  					_ ->
  					    ?ERROR("Environment variable 'es_port' is not set"),
  					    "port"++Line
  				    end,
  			  [NewLine | Acc];
  		     (Line, Acc) ->
  			  [Line | Acc]
  		  end,
  	    NewLines = lists:foldr(Fun, [], StrippedLines),
  	    write_lines(File, NewLines),
  	    ?DEBUG("Finished configuring elastic_search config options");
  	_ ->
  	    ?ERROR("Unable to get current working directory, aborting config operations for elastic_search"),
  	    error
      end.
  
  %% @doc
  %% Runs the configuration mechanism for the rabbit_mq library
  %% @end
  -spec rabbit_mq_config() -> ok | error.
  rabbit_mq_config() ->
      case file:get_cwd() of
  	{ok, CWD} ->
  	    File = CWD ++ "/lib/erlastic_search/include/erlastic_search.hrl",
  	    ?DEBUG(file:read_file_info(File));
  	_ ->
  	    ?ERROR("Unable to get current working directory, aborting config operations for rabbit_mq")
      end.
  
  %% @doc
  %% Runs the configuration mechanism for the webmachine library
  %% @end
  -spec webmachine_config() -> ok | error.
  webmachine_config() ->
      case file:get_cwd() of
  	{ok, CWD} ->
  	    File = CWD ++ "/lib/erlastic_search/include/erlastic_search.hrl",
  	    ?DEBUG(file:read_file_info(File));
  	_ ->
  	    ?ERROR("Unable to get current working directory, aborting config operations for webmachine")
      end.
  
  
  
  
negative.code: 			    Fun2 = fun({ConfOpt, ConfVal}) ->
  					   case application:get_env(engine, ConfOpt) of
  					       {ok, ConfVal} ->
  						   ok;
  					       {ok, _} ->
  						   ?ERROR(lists:concat(["Config option '", ConfOpt, 
  									"' is defined, but has wrong value "
  									"in relation to the config file"])),
  						   error;
  					       undefined ->
  						   ?ERROR(lists:concat(["Config option '", ConfOpt, "' is not defined"])),
  						   error
  					   end
  				   end,
  			    CheckConfig = lists:map(Fun2, FilterAndPair),
  			    case lists:all(fun(X) -> X =/= error end, CheckConfig) of
  				true ->
  				    ok;
  				false ->
  				    error
  			    end;
  			_ ->
  			    ?ERROR("Unable to read from config file"),
  			    error
  		    end;
  		_ ->
  		    ?ERROR("Unable to get current working directory"),
  		    error
  	    end;
  	_ ->
  	    ?ERROR("Unable to open config file"),
  	    error
      end.
  
  %% @doc
  %% Check validity of elastic_search config file
  %% @end
  -spec elastic_search_config_check() -> ok | error.
  elastic_search_config_check() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/lib/elasticsearch/config/elasticsearch.yml",
      case file:read_file_info(File) of
  	{ok, _FileInfo}->
  	    ok;
  	{error, _Error} ->
  	    ?ERROR("Elastic search config file NOT FOUND"),
  	    error
      end.
  
  
  %% @doc
  %% Check validity of erlastic_search config file
  %% @end
  -spec erlastic_search_config_check() -> ok | error.
  erlastic_search_config_check() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/lib/erlastic_search/include/erlastic_search.hrl",
      case file:read_file_info(File) of
  	{ok, _FileInfo}->
  	    ok;
  	{error, _Error} ->
  	    ?ERROR("Erlastic search config file NOT FOUND"),
  	    error
      end.
  
  %% @doc
  %% Check validity of RabbitMQ config file
  %% @end
  -spec rabbitmq_config_check() -> ok | error.
  rabbitmq_config_check() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/lib/rabbitmq-server/scripts/rabbitmq-defaults",
      case file:read_file_info(File) of
  	{ok, _FileInfo} ->
  	    ok;
  	{error, _Error} ->	    
  	    ?ERROR("RabbitMQ config file NOT FOUND"),
  	    error
      end.
  
  %% @doc
  %% Runs the configuration mechanism for the application
  %% @end
  -spec engine_and_nodejs_config() -> ok.
  engine_and_nodejs_config() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/sensec.sh",
      Lines = read_file_lines(File),
      NewLines = [case X of
  		    OldLine = "LOG_DIR"++_Line ->
  			case application:get_env(engine, engine_log_dir) of
  			    undefined ->
  				?DEBUG("Application log dir not defined. Using default value."),
  				OldLine;
  			    {ok, Value} ->
  				ensure_dir(Value),
  				"LOG_DIR=" ++ Value ++ "\n"
  			end;
  		    OldLine = "LOG_JS_DIR"++_Line ->
  			case application:get_env(engine, nodejs_log_dir) of
  			    undefined ->
  				?DEBUG("NodeJS log dir not defined. Using default value."),
  				OldLine;
  			    {ok, Value} ->
  				ensure_dir(Value),
  				"LOG_JS_DIR=" ++ Value ++ "\n"
  			end;
  		    X ->
  			X
  		end || X <- Lines],  
       case NewLines of
  	Lines ->
  	    ok; %% Nothing in the configuration file was changed so we don't write to the file
  	_ ->
  	    write_lines(File, NewLines)
      end,
      ?DEBUG("Finished configuring application config options").
  
  
  %% @doc
  %% Runs the configuration mechanism for the elastic search
  %% @end
  -spec elastic_search_config() -> ok.
  elastic_search_config() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/lib/elasticsearch/config/elasticsearch.yml",
      Lines = read_file_lines(File),
      FunStrip = fun(X) -> NoComment = string:strip(X, left, $#),
  			 string:strip(NoComment, left) 
  	       end, 
      %% Take away comment characters & spaces from the beginning of the line
      StrippedLines = [FunStrip(X) || X <- Lines],
      Fun = fun(OldLine = "network.host"++_Line, Acc) ->
  		  case application:get_env(engine, es_ip) of
  		      undefined ->
  			  ?DEBUG("Elastic search ip option not defined. Using default value."),
  			  [OldLine|Acc];
  		      {ok, Value} ->
  			  ["network.host: " ++ Value ++ "\n"|Acc]
  		  end;
  	     (OldLine = "http.port"++_Line, Acc) ->
  		  case application:get_env(engine, es_port) of
  		      undefined ->
  			  ?DEBUG("Elastic search port option not defined. Using default value."),
  			  [OldLine|Acc];
  		      {ok, Value} ->
  			  ["http.port: " ++ integer_to_list(Value) ++ "\n"|Acc]
  		  end;
  	     (OldLine = "path.data"++_Line, Acc) ->
  		  case application:get_env(engine, es_db_dir) of
  		      undefined ->
  			  ?DEBUG("Elastic search data directory option not defined. Using default value."),
  			  [OldLine|Acc];
  		      {ok, Value} ->
  			  DataLine1 = "path.data: " ++ Value ++ "\n",
  			  case lists:member(DataLine1, Acc) of
  			      true ->
  				  case application:get_env(engine, es_db_extra_dir) of
  				      undefined ->
  					  ?DEBUG("Elastic search extra data directory option not defined. "
  						 "Not using extra data directory"),
  					  [OldLine|Acc];
  				      {ok, ""} ->
  					  ?DEBUG("Elastic search extra data directory option defined as empty string. "
  						 "Not using extra data directory"),
  					  ["# "++OldLine|Acc];
  				      {ok, Value2} ->
  					  ["path.data: " ++ Value2 ++ "\n"|Acc]
  				  end;
  			      false ->
  				  ensure_dir(Value),
  				  [DataLine1|Acc]
  			  end
  		  end;
  	     (OldLine = "path.logs"++_Line, Acc) ->
  		  case application:get_env(engine, es_log_dir) of
  		      undefined ->
  			  ?DEBUG("Elastic search log directory option not defined. Using default value."),
  			  [OldLine|Acc];
  		      {ok, Value} ->
  			  ensure_dir(Value),
  			  ["path.logs: " ++ Value ++ "\n"|Acc]
  		  end;
  	     (OldLine = "cluster.name"++_Line, Acc) ->
  		  case application:get_env(engine, es_cluster_name) of
  		      undefined ->
  			  ?DEBUG("Elastic search cluster name option not defined. Using default value."),
  			  [OldLine|Acc];
  		      {ok, Value} ->
  			  ["cluster.name: " ++ Value ++ "\n"|Acc]
  		  end;
  	     (Line, Acc) ->
  		  case lists:member(Line, Lines) of
  		      true ->
  			  [Line|Acc];
  		      false ->
  			  NewLine = "# "++ Line,
  			  [NewLine|Acc]
  		  end
  	  end,
      NewLines = lists:reverse(lists:foldl(Fun, [], StrippedLines)),
      case NewLines of
  	Lines ->
  	    ok; %% Nothing in the configuration file was changed so we don't write to the file
  	_ ->
  	    write_lines(File, NewLines)
      end,
      ?DEBUG("Finished configuring elastic_search config options").
  
  
  %% @doc
  %% Runs the configuration mechanism for the erlastic libraries
  %% @end
  -spec erlastic_search_config() -> ok.
  erlastic_search_config() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/lib/erlastic_search/include/erlastic_search.hrl",
      Lines = read_file_lines(File),
      FunStrip = fun(X) -> string:strip(X, left) end, 
      %% Take away comment characters & spaces from the beginning of the line
      StrippedLines = [FunStrip(X) || X <- Lines],
      NewLines = [case X of
  		    OldLine = "host"++_Line ->
  			case application:get_env(engine, es_ip) of
  			    undefined ->
  				?DEBUG("Elastic search ip option not defined. Using default value."),
  				OldLine;
  			    {ok, Value} ->
  				"host = \""++ Value ++ "\":: string(),\n"
  			end;
  		    OldLine = "port"++_Line ->
  			case application:get_env(engine, es_port) of
  			    undefined ->
  				?DEBUG("Elastic search port option not defined. Using default value."),
  				OldLine;
  			    {ok, Value} ->
  				"port = \""++ integer_to_list(Value) ++ "\":: integer(),\n"
  			end;
  		    X ->
  			X
  		end || X <- StrippedLines], 
      case NewLines of
  	Lines ->
  	    ok; %% Nothing in the configuration file was changed so we don't write to the file
  	_ ->
  	    write_lines(File, NewLines)
      end,
      ?DEBUG("Finished configuring erlastic_search config options").
  
  
  %% @doc
  %% Runs the configuration mechanism for the rabbit_mq library
  %% @end
  -spec rabbit_mq_config() -> ok.
  rabbit_mq_config() ->
      {ok, CWD} = file:get_cwd(),
      File = CWD ++ "/lib/rabbitmq-server/scripts/rabbitmq-defaults",
      Lines = read_file_lines(File),
      FunStrip = fun(X) -> string:strip(X, left) 
  	       end, 
      %% Take away comment characters & spaces from the beginning of the line
      StrippedLines = [FunStrip(X) || X <- Lines],
      NewLines = [case X of
  		    OldLine = "LOG_BASE"++_Line ->
  			case application:get_env(engine, rabbit_mq_log_dir) of
  			    undefined ->
  				?DEBUG("RabbitMQ log directory option not defined. Using default value."),
  				OldLine;
  			    {ok, Value} ->
  				ensure_dir(Value),
  				"LOG_BASE=" ++ Value ++ "\n"
  			end;
  		    X ->
  			X
  		end || X <- StrippedLines],
      case NewLines of
  	Lines ->
  	    ok; %% Nothing in the configuration file was changed so we don't write to the file
  	_ ->
  	    write_lines(File, NewLines)
      end, 
      ?DEBUG("Finished configuring rabbit_mq config options").
  
  
  
  pair(List) ->
      pair(List, []).
  