program pluck;

{$I stdins}

type
datblo=record
       a:array[1..512] of byte;
       end;

var
dat:datblo;
fil:file of datblo;
filename:string[30];
last_error:byte;



procedure help_screen;

begin
writeln('Syntax:',cr,lf,'      PLUCK  [options]  filename',cr,lf);
writeln('Plucks a sector from a hard or floppy disk into a file');
writeln('Options:',cr,lf,'     /?              -             Produces this screen',cr,lf);
end;



procedure switch_sus(switch:general_string);

begin
case switch of
     '?':help_screen;
     end;
end;


function param_ok:boolean;

var
l:integer;

begin
filename:='';
param_ok:=false;
last_error:=1;
if paramcount>0 then
   for l:=1 to paramcount do
   if copy(paramstr(l),1,1)='/' then switch_sus(copy(paramstr(l),2,1))
      else begin
           filename:=paramstr(l);
           writeln('File:',filename);
           param_ok:=true;
           end
   else last_error:=3;
end;




procedure get_sector(many,cyl,sec,drive,head:integer);

begin
recpack.ax:=$0200+many;
recpack.cx:=(cyl*256)+sec;
recpack.dx:=(head*256)+drive;
recpack.es:=seg(dat);
recpack.bx:=ofs(dat);
intr($13,recpack);
if ((recpack.ax) and 255) <> many then last_error:=1 else last_error:=0;
end;


procedure report_error(num:integer);

begin
case num of
   1:writeln('What about a filename then ?');
   2:writeln('Unable to open file!');
   3:writeln('No parameters!');
   else writeln('No go!');
   end;
end;


begin
writeln('PLUCK - MS-DOS Sector Plucking Tool (ooh eer)  V0.1  (C)Veghead 1993');
if param_ok then
   begin
   {$I-}
   assign(fil,filename);
   rewrite(fil);
   {$I+}
   if ioresult=0 then
      begin
      get_sector(1,0,1,$80,0);
      write(fil,dat);
      writeln('Sector plucked!');
      close(fil);
      end
      else report_error(2);
   end
   else report_error(last_error);
end.
