{
  Author: H van Zwietering
  Version: 1.1

  Changes:
  1.1: Added support for PSU v4
  1.0: Initial version
}

  procedure UpdateCollection(AColl: TImageCollection);
  var
    i: Integer;
  begin
    for i := 0 to AColl.SubFolders.Count - 1 do
    begin
      AColl.SubFolders.Items[i].ParentOrder := i;
      PublicCatalog.UpdateCollection(AColl.SubFolders.Items[i]);
      // recursive call
      UpdateCollection(AColl.SubFolders.Items[i]);
    end;
  end;

  procedure UpdateGallery(AGall: TImageGallery);
  var
    i: Integer;
  begin
    for i := 0 to AGall.Collections.Count - 1 do
    begin
      AGall.Collections.Items[i].ParentOrder := i;
      PublicCatalog.UpdateCollection(AGall.Collections.Items[i]);

      UpdateCollection(AGall.Collections.Items[i]);
    end;
  end;

  procedure UpdatePortfolio(AModel: TImageModel);
  var
    i: Integer;
  begin
    if AModel.Galleries.Count = 0 then exit;

    for i := 0 to AModel.Galleries.Count - 1 do
    begin
      UpdateGallery(AModel.Galleries.Items[i]);

    end;
  end;

  procedure ReorderPortfolios;
  var
    APortfolios: TImageModelItems;
    i: Integer;
  begin
    APortfolios := TImageModelItems.Create(TImageModelItem, '');
    PublicCatalog.EnumModels(APortfolios, True, False, False);
    for i := 0 to APortfolios.Count - 1 do
      UpdatePortfolio(APortfolios.Items[i].Model);
    APortfolios.Free;
  end;

begin
  if not Ask2('Are you sure you want to continue?', 'This script will convert an IDimager database to Photo Supreme. This can take a while to complete, depending on the size of your Catalog.' + CrLf2 + 'PLEASE DO NOT KILL THE APPLICATION WHILE THIS SCRIPT RUNS, EVEN WHEN THE OPERATING SYSTEM INDICATES THAT THE APPLICATION IS NOT RESPONDING.' + CrLf2) then exit;

  ReorderPortfolios;
  PublicCatalog.BuildSearchData;

  Say('Done');
end;

