uses
  'UfebsConst',
  'WorkUfebsVersion.inc',
  'WorkAppVersion.inc',
  'UfebsVersionInc.inc',
  'Un_Debug';

var
  DirIni, DirArhLog: String;
  FmZipCompress: TForm;
  ZipForge: TZipForge;
  LabelFileName, LabelFileProc, LabelAllProc: TLabel;
  ProgressBarFile, ProgressBarAll: TProgressBar;
  ButtonCancel: TButton;
  

procedure OnButtonCancelClick(Sender: TObject);
begin
  ButtonCancel.Enabled := False;
end; {End OnButtonCancelClick}

procedure OnZipForgeFileProgress(Sender: TObject; FileName: String;
  Progress: Double; Operation: TZFProcessOperation;ProgressPhase: TZFProgressPhase;
  var Cancel: Boolean);
begin
//  LabelFileName.Caption := TZipForge(Sender).BaseDir + FileName;
  LabelFileName.Caption := FileName;
  ProgressBarFile.Position := Round(Progress);
  LabelFileProc.Caption := IntToStr(ProgressBarFile.Position) + '%';
  Cancel := not ButtonCancel.Enabled;
end; {End TFmZipCompress.ZipForgeFileProgress}

procedure OnZipForgeOverallProgress(Sender: TObject; Progress: Double;
  Operation: TZFProcessOperation; ProgressPhase: TZFProgressPhase; var Cancel: Boolean);
begin
  ProgressBarAll.Position := Round(Progress);
  LabelAllProc.Caption := IntToStr(ProgressBarAll.Position) + '%';
end; {End TFmZipCompress.ZipForgeOverallProgress}

procedure Main(Xml: Variant);
begin
  if CompareVersionEx(AppVersion, WorkAppVersion) < 0 then
    raise('Скрипты работают с AzkEchange.Exe начиная с версии: ' + WorkAppVersion +
      ''#13#10'Текущая версия AzkEchange.Exe: ' + AppVersion);
  DirIni := ExtractFilePath(Xml.documentElement.getAttribute('INI_FILE'));
  if not FileExists(DirIni + 'UfebsLoad.Log') and
    not FileExists(DirIni + 'UfebsUnLoad.Log') then
  begin
    MessageDlg('Нет файлов истории обработки по формату УФЭБС', mtWarning , mbCancel , 0);
    Exit;
  end;
  DirArhLog := DirIni + 'UfebsArhLog\' + FormatDateTime('yyyy-mm-dd', Date);
  if FileExists(DirArhLog + '.zip') then
  begin
    MessageDlg('Сегодня уже производилось архивирование: ' + DirArhLog + '.zip',
      mtWarning , mbCancel , 0);
    Exit;
  end;
  ForceDirectories(DirArhLog);
  if not DirectoryExists(DirArhLog) then
    raise('Ошибка создания каталога: ' + DirArhLog);
  CopyFile(DirIni + 'UfebsLoad.Log', DirArhLog + '\UfebsLoad.Log', False);
  CopyFile(DirIni + 'UfebsUnLoad.Log', DirArhLog + '\UfebsUnLoad.Log', False);
  
  FmZipCompress := TForm.Create(Application);
  ZipForge := TZipForge.Create(FmZipCompress);
  try
    LoadForm(DirIni + 'Form\Fm_UfebsZipLog.dfm', FmZipCompress);
    ZipForge.OnFileProgress := @OnZipForgeFileProgress;
    ZipForge.OnOverallProgress := @OnZipForgeOverallProgress;
    LabelFileName := TLabel(FmZipCompress.FindComponent('LabelFileName'));
    LabelFileProc := TLabel(FmZipCompress.FindComponent('LabelFileProc'));
    ProgressBarFile := TProgressBar(FmZipCompress.FindComponent('ProgressBarFile'));
    LabelAllProc := TLabel(FmZipCompress.FindComponent('LabelAllProc'));
    ProgressBarAll := TProgressBar(FmZipCompress.FindComponent('ProgressBarAll'));
    ButtonCancel := TButton(FmZipCompress.FindComponent('ButtonCancel'));
    ButtonCancel.OnClick := @OnButtonCancelClick;
    ZipForge.FileName := DirArhLog + '.zip';
    FmZipCompress.Caption := 'Создание архива ' + ExtractFileName(ZipForge.FileName);
    FmZipCompress.Show;
    ZipForge.OpenArchive(fmCreate);
    ZipForge.BaseDir := DirIni + 'UfebsArhLog';
    ZipForge.AddFiles(FormatDateTime('yyyy-mm-dd\*.*', Date));
    ZipForge.CloseArchive;
    if not ButtonCancel.Enabled then
      DeleteFile(ZipForge.FileName)
    else
    begin
      DeleteFile(DirArhLog + '\UfebsLoad.Log');
      DeleteFile(DirArhLog + '\UfebsUnLoad.Log');
      RemoveDir(DirArhLog);
      DeleteFile(DirIni + 'UfebsLoad.Log');
      DeleteFile(DirIni + 'UfebsUnLoad.Log');
      FmZipCompress.Close;
      MessageDlg('Архивирование завершено успешно, архив: ' + DirArhLog + '.zip',
        mtInformation, mbOk , 0);
    end;
  finally
    FmZipCompress.Free;
  end;
end; {End Main}

begin
end.