{temporary rescue after I cocked up and deleted all the source...}
program Rescue;


uses
	minilib,
	windos, dosutils;
{runs through all files in \temp\undel directory, checking to see if first
line is ASCII, if so, runs through first 100 lines looking for "unit" or
"program", and renames file appropriately if found}


var
	SearchRec : TSearchRec;
	F : text;
	Path, ToPath, S, UnitFName : string;
	TestFName : string;
	Pascal, Sure, ASCII : boolean;
	LineNUm,I : integer;
	FCount : word;
	LogFile : text;

procedure Write(S : string);
begin
	System.Write(S);
	System.Write(LogFile, S);
end;


begin
	Path := '\TEMP\UNDEL\'; ToPath := '\temp\';
	FindFirst('\TEMP\UNDEL\*.*', faArchive, SearchRec);
	FCount := 0;
	Assign(LogFile, 'RESCUE.LOG');
	Rewrite(LogFile);

	while DosError = 0 do begin
		TestFName := Path+DelNulR(SearchRec.Name);
		Write('Checking '+TestFName);
		UnitFName := '';
		inc(FCount);

		{Open file}
		Assign(F, TestFName);
		Reset(F);

		LineNUm := 1; Sure := False; ASCII := True; Pascal := False; UnitFname := '';
		while (LineNum<99) and not eof(F) and not Sure and ASCII do begin
			Readln(F, S);

			{check for ASCII}
			for I := 1 to length(S) do if (S[I]>#128) or (S[I]=#0) or (S[I]=#7) then ASCII := False;

			if (Copy(ucase(S),1,3)='TPU') or (Copy(Ucase(S),1,3)='TPP') then ASCII := False;

			if ASCII then begin
				if S[1]='{' then Pascal := True;

				if pos('UNIT', ucase(S))>0 		then UnitFName := delspace(copy(S,pos('UNIT', ucase(S))+4,99));
				if pos('PROGRAM', ucase(S))>0 then UnitFName := delspace(copy(S,pos('PROGRAM', ucase(S))+7,99));

				if UnitFName<>'' then begin
					if (pos(' ',UnitFName)>0) or (UnitFname[1]='}') or (UnitFname[1]='*') then
						UnitFname := ''
					else begin
						Pascal := True;
						Sure := True;
					end;
				end;

				inc(LineNum);
			end;
		end;

		close(F);

		if not ASCII then begin
			write(' Not ASCII');
		end else begin
			if UnitFName='' then begin
				write('  no unit name');
				UnitFName := 'ASCII'+N2Str(FCount);
			end else
				if UnitFName[length(UnitFName)]=';' then UnitFName := Copy(UnitFName, 1, length(UnitFName)-1);

			if Pascal then UnitFName := UnitFName+'.PAS';
			if FileExists(ToPath+UnitFName) then begin
				if FileExists(ToPath+'DUPL\'+UnitFName) then
					write(' duplicate, not stored')
				else
					UnitFname := 'DUPL\'+UnitFname;
			end;
			write('  '+UnitFName);
			CopyFile(TestFName, ToPath+ucase(UnitFName));
		end;

		writeln;
		FindNext(SearchREc);
	end;

	Close(LogFile);
end.


