{$A+,B-,D+,E+,F-,I+,L+,N-,O-,R+,S+,V-}
{$M 6000,14000,14000}

(*
 * DODINDEX.PAS
 * Copyright 1989 by Michael A. Quinlan, all rights reserved.
 * Written in Turbo Pascal v5.0.
 * Uses the Turbo Professional library from TurboPower Software.
 *
 * Build an index of the PC-SIG Directory on Disk.
 *
 *)

program DODINDEX;

uses TPDOS;

var dodf	: TEXT;						{ Diskette Descriptions }
	idxf	: file of longint;		{ Index File }
	line	: string[80];				{ Current line in Diskette Descriptions }
	lastdisk : integer;				{ Highest diskette indexed so far }
	curdisk	: integer;				{ Current diskette number }
	curpos	: longint;				{ Current position in the description file}
	nullpos	: longint;				{ Dummy variable }
	code		: integer;				{ CODE from VAL function }
	buffer	: array [1..4096] of char;		{ File I/O Buffer }

begin

	WriteLn('DODINDEX - Build an index for the PC-SIG Directory on Disk');
	WriteLn('Copyright 1989 by Michael A. Quinlan, all rights reserved.');
	WriteLn;

	if ParamCount <> 2 then begin
		WriteLn('Usage: DODINDEX d:\path\1_1000.UPP d:\path\dodindex.dat');
		halt(1);
	end;

	FileMode := 0;
	assign(dodf, ParamStr(1));
	SetTextBuf(dodf, buffer);
	{$I-} reset(dodf); {$I+}
	if (IOResult <> 0) then begin
		WriteLn('Unable to open ', ParamStr(1));
		Halt(1);
	end;

	FileMode := 1;
	assign(idxf, ParamStr(2));
	rewrite(idxf);

	lastdisk := -1;

	while not eof(dodf) do begin
		curpos := TextPos(dodf);
		ReadLn(dodf, line);
		if line <> '' then begin
			val(copy(line, 1, 4), curdisk, code);
			if (code = 0) and (curdisk > lastdisk) then begin
				WriteLn('Indexing DISK', copy(line, 1, 4), '...');
				lastdisk := lastdisk + 1;
				while lastdisk < curdisk do begin
					nullpos := -1;
					Write(idxf, nullpos);
					lastdisk := lastdisk + 1;
				end;
				Write(idxf, curpos);
			end;
		end;
	end;

	close(dodf);
	close(idxf);

	WriteLn('Done.');

end.
