{$MODE DELPHI}

Program testmime;

uses
  mimepart, synachar, classes;

type
  Tc = class(TObject)
  public
    class procedure ph(const Sender: TMimePart);
  end;

class procedure Tc.ph(const Sender: TMimePart);
begin
  Sender.DecodePart;
  Sender.EncodePart;
end;

var
  m: tmimepart;
  PartCount: integer;
  m1: tmimepart;
  MyPart: integer;
  MyPartCount: integer;

begin
  m := tmimepart.create;
//  try
    m.OnWalkPart:=tc.ph;
    m.Lines.LoadFromFile(paramstr(1));
    m.DecomposeParts;
    m.WalkPart;
    PartCount := m.GetSubPartCount;
    if m.Primary = 'MULTIPART' then
      begin
        writeln;
        writeln('  entering ProcessParts...');
        for MyPart := 1 to PartCount do
          begin
            if MyPart > 1 then
              writeln;
              writeln('    allocate m1 for part ',MyPart);
              m1 := tmimepart.create;
              try
                writeln('    assign m1.OnWalkPart for part ',MyPart);
                m1.OnWalkPart:=tc.ph;
                writeln('    pulling part ',MyPart,' into m1-',MyPart);
                m1:= m.GetSubPart(MyPart - 1);
                if ((m1.Primary = 'TEXT') and (m1.Secondary = 'PLAIN')) then
                  writeln('    ** WE HAVE A TEXT/PLAIN SECTION **')
                else
                  begin
                    writeln('    This part is not TEXT/PLAIN... DELETING... ');
                    writeln('      firing m.DeleteSubPart(',MyPart-1);
                    m.DeleteSubPart(MyPart - 1);
                    writeln('      back from m.DeleteSubPart...');
                    writeln('    Done!');
                  end;
              finally
                writeln('    finally');
                writeln('      free m1 for part ',MyPart,'... ');
                if m1 <> NIL then
                  begin
                    write('        m1 <> NIL... Destroying... ');
                    m1.free;
                    writeln('done!');
                  end
                else
                  writeln('        m1 = NIL... no destroy necessary...');
                writeln('    done!');
              end;
          end;
        writeln('  vacating ProcessParts...');
      end;

    writeln;
    writeln('  fire m.ComposeParts...');
    m.ComposeParts;
    writeln('  return from m.ComposeParts...');
    writeln('  fire m.Lines.SaveToFile...');
    m.Lines.SaveToFile(paramstr(1) + '.repack');
    writeln('  return from m.Lines.SaveTofile...');
//  finally
    writeln;
    writeln('finally');
    write('  free m... ');
    m.free;
    writeln('done!');
//  end;
  writeln('Process complete. Terminating...');
end.
