#!/usr/bin/env escript main([]) -> try {ok,DefaultDirName}=file:get_cwd(), Id = input_int("Please enter a node number", 1,99), NodeName = input("Please enter a node name", make_nodename(Id) ), Cookie = input("Please enter a network cookie", "oreo" ), DirName = input("Please enter a directory name", DefaultDirName ), file_substitute(filename:join(["priv","templates","simnode.config.template"]), filename:join(["config","sys.config"]), [{"$$PROJECT_HOME$$",DirName}, {"$$NODE_ID$$",integer_to_list(Id)}]), file_substitute(filename:join(["priv","templates","simnode.args.template"]), filename:join(["config","vm.args"]), [{"$$NODE_NAME$$",NodeName},{"$$COOKIE$$",Cookie}]) catch _:_ -> usage() end; main(_) -> usage(). usage() -> io:format("usage: mqtt_config ~n"), halt(1). find_hostname()-> {ok,Hostname}=inet:gethostname(), {ok,{hostent,_RealHostname,_,inet,4,[A|_]}}=inet_res:gethostbyname(Hostname), {ok,{hostent,VerifiedHostName,_,inet,4,_}}=inet_res:gethostbyaddr(A), VerifiedHostName. make_nodename(Id)-> "simnode" ++ integer_to_list(Id) ++ "@" ++ find_hostname(). replace([],FinalBlob)-> FinalBlob; replace([{Variable,Value}|T],Blob) when is_integer(Value)-> replace(T,string:replace(Blob,Variable,integer_to_list(Value),all)); replace([{Variable,Value}|T],Blob) when is_list(Value)-> replace(T,string:replace(Blob,Variable,Value,all)). file_substitute(FileNameIn,FileNameOut,VariableList)-> {ok,FileBin}=file:read_file(FileNameIn), FileBlob=binary_to_list(FileBin), NewBlob=replace(VariableList,FileBlob), file:write_file(FileNameOut,list_to_binary(NewBlob)). input_int(Prompt,Min,Max)-> InputData=string:trim(io:get_line( Prompt ++ "(" ++ integer_to_list(Min) ++ ".." ++ integer_to_list(Max) ++ ") [" ++ integer_to_list(Min) ++ "] :")), case InputData=="" of true -> Min; false -> D = string:trim(InputData), try I = list_to_integer(D), case ((I >= Min) and (I =< Max)) of true -> I; false -> io:format("Must be a number between ~p and ~p.~n",[Min,Max]), input_int(Prompt,Min,Max) end catch _:_ -> io:format("Must be a number between ~p and ~p.~n",[Min,Max]), input_int(Prompt,Min,Max) end end. input(Prompt,Default)-> InputData=string:trim(io:get_line( Prompt ++ " [" ++ Default ++ "] :")), case InputData=="" of true -> Default; false -> InputData end.