MODULE WinIntDialog;

	IMPORT Dialog, Console, WinApi, Env := WinEnv;

	TYPE
		ShowHook = POINTER TO RECORD (Dialog.ShowHook) END;

	PROCEDURE WriteParamMsg (IN str, p0, p1, p2: ARRAY OF CHAR);
	VAR msg: ARRAY 1024 OF CHAR; i: INTEGER; ch: CHAR;
	BEGIN
		Dialog.MapParamString(str, p0, p1, p2, msg);
		i := 0; ch := msg[0];
		WHILE ch # 0X DO
			IF ch = 0DX THEN Console.WriteLn
			ELSIF ch = 0EX THEN Console.WriteLn
			ELSIF ch = 09X THEN Console.WriteChar(09X)
			ELSIF ch >= " " THEN Console.WriteChar(ch)
			END;
			INC(i); ch := msg[i];
		END;
		Console.WriteLn
	END WriteParamMsg;

	PROCEDURE (h: ShowHook) ShowParamMsg (IN str, p0, p1, p2: ARRAY OF CHAR);
	BEGIN
		WriteParamMsg(str, p0, p1, p2)
	END ShowParamMsg;

	PROCEDURE (h: ShowHook) ShowParamStatus (IN str, p0, p1, p2: ARRAY OF CHAR);
	BEGIN
		IF Dialog.showsStatus THEN
			WriteParamMsg(str, p0, p1, p2)
		END
	END ShowParamStatus;

	PROCEDURE IsWine(): BOOLEAN;
		VAR h: WinApi.HMODULE; adr: WinApi.FARPROC; res: INTEGER;
	BEGIN
		(* http://habrahabr.ru/blogs/development/76131/ *)
		h := WinApi.LoadLibraryW("ntdll.dll");
		IF h # WinApi.NULL THEN
			adr := WinApi.GetProcAddress(h, "wine_get_version");
			res := WinApi.FreeLibrary(h);
			IF adr # NIL THEN RETURN TRUE END
		END ;
		RETURN FALSE
	END IsWine;

	PROCEDURE Init;
	VAR showHook: ShowHook; res: INTEGER;
	BEGIN
		Dialog.platform := Dialog.windows;
		Dialog.isWine := IsWine();
		Env.GetPar("PAR", Dialog.commandLinePars, res);
		NEW(showHook); Dialog.SetShowHook(showHook)
	END Init;

BEGIN
	Init
END WinIntDialog.
