MODULE InfoCollect;

	(*
	Collect all files used in the link document
	Helmut Zinn - 

	 InfoCollect.Run
	*)

	IMPORT
		DosFileCmds, DosKeyboard, HostFiles, InfoDialog, StdLinks, StdLog, Strings,
		TextControllers, TextMappers, TextModels, TextViews, Views;

	CONST
		pathLen = HostFiles.pathLen;

	VAR
		dst: ARRAY pathLen OF CHAR;
		cnt: INTEGER;

	PROCEDURE InitFilename (IN filespec: ARRAY OF CHAR);
	BEGIN
		cnt := 0;
		dst := filespec$;
	END InitFilename;

	PROCEDURE CreateFilename (OUT filespec: ARRAY OF CHAR);
		VAR
			sequentialNumber: ARRAY 32 OF CHAR;
	BEGIN
		INC(cnt);
		Strings.IntToStringForm(cnt, 10, 3, '0', FALSE, sequentialNumber);
		filespec := dst + sequentialNumber$;
	END CreateFilename;

	PROCEDURE ExtractFilename (IN cmd: ARRAY OF CHAR; OUT filespec: ARRAY OF CHAR);
		VAR a, b, n: INTEGER; s1, s2, s3: ARRAY 256 OF CHAR;
	BEGIN
		a := 0;
		WHILE (cmd[a] # 0X) & (cmd[a] # "'") DO INC(a) END;
		b := a + 1;
		WHILE (cmd[b] # 0X) & (cmd[b] # "'") DO INC(b) END;
		n := LEN(cmd$);
		IF b > n THEN b := a + 1 END;
		IF b > n THEN n := b + 1 END;
		Strings.Extract(cmd, 0, a + 1, s1); (* in front of a - including a *)
		Strings.Extract(cmd, a + 1, b - a - 1, s2); (* between a and b *)
		Strings.Extract(cmd, b, n - b, s3); (* behind b - including b *)
		(* check cmd *)
		IF (s1 = "InfoCmds.OpenDoc('") & (s3[0] = "'") & (s3[1] = ")") THEN
			filespec := s2$;
		ELSE
			filespec := "";
			StdLog.String('Link not insert into the file list: '); StdLog.String(cmd); StdLog.Ln;
			(*
			StdLog.String(s1); StdLog.Ln;
			StdLog.String(s2); StdLog.Ln;
			StdLog.String(s3); StdLog.Ln;
			*)
		END;
	END ExtractFilename;

	PROCEDURE TruncateTitle (IN str: ARRAY OF CHAR; OUT title: Views.Title);
		VAR s1, s2: Views.Title; n: INTEGER;
	BEGIN
		n := LEN(str$);
		IF n > 63 THEN
			Strings.Extract(str, 0, 29, s1);
			Strings.Extract(str, n - 29, 29, s2);
			title := s1$ + " ... " + s2$
		ELSE
			title := str$;
		END;
	END TruncateTitle;

	PROCEDURE FileList* (destination: ARRAY OF CHAR);
		VAR
			t: TextModels.Model; f: TextMappers.Formatter; tv: TextViews.View; title: Views.Title;
			cn: TextControllers.Controller;
			r: TextModels.Reader;
			v: Views.View;
			cmd, srcFilespec, dstFilespec: ARRAY pathLen OF CHAR;
	BEGIN
		InitFilename(destination);
		t := TextModels.dir.New();
		f.ConnectTo(t); 
		f.WriteString("auto"); f.WriteLn;
		cn := TextControllers.Focus();
		IF cn # NIL THEN
			r := cn.text.NewReader(NIL);
			r.ReadView(v);
			WHILE ~DosKeyboard.Break() & (v # NIL) DO
				WITH v: StdLinks.Link DO
					IF v.leftSide THEN
						v.GetCmd(cmd);
						ExtractFilename(cmd, srcFilespec);
						IF srcFilespec # "" THEN (* if not directory  then *)
							f.WriteString(srcFilespec); f.WriteTab;
							CreateFilename(dstFilespec);
							f.WriteString(dstFilespec); f.WriteLn;
						END;
					END
				ELSE
				END;
				r.ReadView(v)
			END;
			IF DosKeyboard.Break() THEN DosKeyboard.ResetBreak END;
		END;
		tv := TextViews.dir.New(t);
		TruncateTitle(destination, title);
		(* set Browser mode: *)
		Views.OpenAux(tv, title)
	END FileList;

	PROCEDURE CreateFileList*;
	BEGIN
		InfoDialog.ProofParam;
		FileList(InfoDialog.param.source);
	END CreateFileList;

	PROCEDURE ReadField (VAR sc: TextMappers.Scanner; OUT s: ARRAY OF CHAR);
		VAR ch: CHAR; i: INTEGER;
	BEGIN
		i := 0;
		ch := sc.rider.char;
		WHILE (ch = TextModels.line) OR (ch = TextModels.tab) OR (ch = TextModels.para) DO
			sc.rider.ReadChar(ch)
		END;
		WHILE ~(sc.rider.eot
			OR (ch = TextModels.line) OR (ch = TextModels.tab) OR (ch = TextModels.para) OR (i > 254)) DO
			s[i] := ch; INC(i);
			sc.rider.ReadChar(ch);
		END;
		s[i] := 0X;
	END ReadField;

	PROCEDURE MoveFiles*;
		VAR
			cn: TextControllers.Controller;
			sc: TextMappers.Scanner;
			ch: CHAR;
			srcFilespec, dstFilespec: ARRAY pathLen OF CHAR;
	BEGIN
		StdLog.Ln; StdLog.String("Move Files started ..."); StdLog.Ln;
		cn := TextControllers.Focus();
		IF cn # NIL THEN
			sc.ConnectTo(cn.text);
			sc.Scan; (* skip auto *)
			sc.rider.ReadChar(ch);
			WHILE ~DosKeyboard.Break() & ~sc.rider.eot DO
				ReadField(sc, srcFilespec);
				ReadField(sc, dstFilespec);
				DosFileCmds.Copy(srcFilespec + ".odc", dstFilespec + ".odc");
				DosFileCmds.Delete(srcFilespec + ".odc");
				sc.rider.ReadChar(ch);
			END;
			IF DosKeyboard.Break() THEN DosKeyboard.ResetBreak END;
		END;
		StdLog.String("Move Files done."); StdLog.Ln;
	END MoveFiles;

END InfoCollect.
