uses crt;

const    bufsize=32768;

var  t,tout:text;out,f:file;
     n1,n2,n3:string;
     buffer:array[1..bufsize] of byte;
     bytes:word;
     num:word;

function upper(s:string):string;
var      c:byte;
begin
  for c:=1 to length(s) do
    s[c]:=upcase(s[c]);
  upper:=s;
end;

begin
  if paramcount<1 then
    begin
      writeln('usage: build <file.ext>');
      halt;
    end;
  n1:=paramstr(1);
  assign(t,n1); {$i-} reset(t); {$i+}
  if ioresult<>0 then
    begin
      writeln('file "',n1,'"not found.');
      halt;
    end;
  assign(tout,'index.inc'); rewrite(tout);
  num:=0;
  writeln;
  while not eof(t) do
    begin
      readln(t,n3);
      if n3[1]<>'#' then
        begin
          if (num>0) and (n3<>'') then
            begin
              if num>1 then write(tout,',') else write(tout,' ');
              assign(f,n3); {$i-} reset(f,1); {$i+}
              if ioresult<>0 then
                begin
                  writeln(tout,'(0,0)                         {',num-1,'.   ',n3,' not found }');
                  writeln('File not found: ',upper(n3));
                end
              else
                begin
                  writeln('Adding file: ',upper(n3));
                  writeln(tout,'(',filepos(out):10,',',filesize(f):10,')       {',num-1,'.   ',n3,'}');
                  while not eof(f) do
                    begin
                      blockread(f,buffer,bufsize,bytes);
                      blockwrite(out,buffer,bytes);
                    end;
                  close(f);
                end;
              inc(num);
            end;
          if (num=0) and (n3<>'') then
            begin
              writeln('Output file: ',upper(n3));
              assign(out,n3); {$i-} rewrite(out,1); {$i+}
              if ioresult<>0 then
                begin
                  writeln('Unable to create file: ',upper(n3),''^M^J,
                          'Exiting');
                  close(t);
                  close(tout);
                  halt;
                end
              else
                inc(num);
            end;
        end;
    end;
  writeln;
  writeln('Total files read: ',num-1);
  writeln('Total size of datafile: ',filepos(out));
  writeln(tout,'{total size=',filepos(out),'}');
  close(out);
  close(t);
  close(tout);
end.