program UpdateIni;
uses
  dos;

function UpCaseStr(S:String):String;
var
  t:integer;
begin
  for t:=1 to length(S) do S[t]:=upcase(S[t]);
  UpCaseStr:=S;
  end;

function DelLeft(S:String):String;
begin
  While (S<>'') and (S[1]=' ') do delete(S,1,1);
  DelLeft:=S;
  end;

function DelRight(S:String):String;
begin
  While (S<>'') and (S[Length(S)]=' ') do delete(S,length(S),1);
  DelRight:=S;
  end;

function XGroup(Var S:String;Orig:String):Boolean;
begin
  S:=UpCaseStr(DelRight(DelLeft(Orig)));
  XGroup:=(S[1]='[');
  end;

procedure UpIni(TargetFile,tmpFile,Group,Name,Value:String;Var errorcode:Integer);
var
  Handle,Hout:Text;
  CurrGroup,CurrInst,CurrName,S:String;
  Done:Boolean;
  t:Integer;
begin
  Assign(Handle,TargetFile);
  Reset(Handle);
  Assign(Hout,tmpFile);
  Rewrite(Hout);
  CurrGroup:='!';
  CurrInst:='!';
  Done:=False;

  While not eof(Handle) do begin
    Repeat
      ReadLn(Handle,CurrInst);
      until eof(Handle) or (DelRight(CurrInst)<>'');
    if not eof(Handle) or (DelRight(CurrInst)<>'') then begin
      if CurrGroup=Group then begin
        if XGroup(S,CurrInst) then begin
          if not Done then begin
            if Value<>'!' then WriteLn(Hout,Name,'=',Value);
            Done:=True;
            end;
          WriteLn(Hout);
          end
        else begin
          t:=pos('=',CurrInst);
          CurrName:=copy(CurrInst,1,t-1);
          if UpCaseStr(DelLeft(DelRight(CurrName)))=Name then begin
            if Value='!' then CurrInst:='!'
            else CurrInst:=CurrName+'='+Value;
            Done:=True;
            end;
          end;
        end
      else begin
        if XGroup(S,CurrInst) then begin
          if CurrGroup<>'!' then WriteLn(Hout);
          CurrGroup:=S;
          S:=S;
          end
        end;
      if CurrInst<>'!' then WriteLn(Hout,CurrInst)
      end;
    end;{big while not eof}
  if (Value<>'!') and not Done then begin
    if CurrGroup<>Group then begin
      WriteLn(Hout);
      WriteLn(Hout,Group);
      end;
    WriteLn(Hout,Name,'=',Value);
    end;
  Close(Hout);
  Close(Handle);
  Rewrite(Handle);
  Write(Handle,'z');
  close(Handle);
  {rename(Handle,'z');  }
  erase(Handle);
  rename(Hout,TargetFile);
  errorcode:=0;
  end;


{MAIN}
var
  Handle,Htmp,Hout:Text;
  CurrFile,CurrGroup,CurrInst:String;
  t,errorcode:Integer;
begin
  if ParamCount<>2 then begin
    WriteLn('Error: Bad Number of parameters,');
    WriteLn(' Usage: UpIni TargetFile(skip the .ini) (with) InstructionFile(skip the .upd)');
    Exit;
    end;



  Assign(Handle,ParamStr(2)+'.upd');
  Reset(Handle);
  while not eof(Handle) and (errorcode=0) do begin
    ReadLn(Handle,CurrFile);
    Assign(Htmp,ParamStr(1)+CurrFile+'.ini');
    Reset(Htmp);
    Assign(Hout,'c:\upini.ini');
    Rewrite(Hout);
    while not eof(Htmp) do begin
      ReadLn(Htmp,CurrInst);
      WriteLn(Hout,CurrInst);
      end;
    Close(Hout);
    Close(Htmp);
    errorcode:=0;
    CurrGroup:='!';
    while not eof(Handle) and (errorcode=0) and (CurrGroup<>'*') do begin
      ReadLn(Handle,CurrGroup);
      if CurrGroup<>'*' then begin
        while (CurrGroup<>'') and (CurrGroup[1]=' ') do delete(CurrGroup,1,1);
        while (CurrGroup<>'') and (CurrGroup[length(CurrGroup)]=' ') do
          delete(CurrGroup,length(CurrGroup),1);
        CurrGroup:=UpCaseStr(CurrGroup);
        repeat
          ReadLn(Handle,CurrInst);
          if CurrInst<>'.' then begin
            t:=pos('=',CurrInst);
            UpIni('c:\upini.ini','c:\upini.tmp',CurrGroup,UpCaseStr(copy(CurrInst,1,t-1))
              ,copy(CurrInst,t+1,length(CurrInst)-t),errorcode);
            end;
          until (CurrInst='.') or (errorcode<>0);
        end;
      end;
    Assign(Htmp,'c:\upini.ini');
    Reset(Htmp);
    Assign(Hout,ParamStr(1)+CurrFile+'.ini');
    Rewrite(Hout);
    while not eof(Htmp) do begin
      ReadLn(Htmp,CurrInst);
      WriteLn(Hout,CurrInst);
      end;
    Close(Hout);
    Close(Htmp);
    Rewrite(Htmp);
    close(Htmp);
    {rename(Handle,'z');}
    erase(Htmp);

    end;
  close(Handle);
  if errorcode<>0 then writeln('errorcode ',errorcode)
  end.
