////////////////////////////////////////////////////////////////////////////////
// CMUUpd
//
// Version 1.0
// * Added elevation to allow for excution beyond WinXP
//
// Copyright 2005 - 2019, The Coolmon Project
////////////////////////////////////////////////////////////////////////////////
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, lplDownload, DFUnRar, Windows10;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    ProgressBar1: TProgressBar;
    Label1: TLabel;
    download: TlplDownload;
    Unrar: TDFUnRar;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure downloadError(Sender: TObject; ErrorCode: Integer);
    procedure downloadProgress(Sender: TObject; Readed, TotalSize: Int64);
    procedure downloadComplete(Sender: TObject);
    procedure UnrarProgress(Sender: TObject; FilesProcessed, FileCount,
      SizeProcessed, SizeCount: Cardinal);
    procedure UnrarError(Sender: TObject; Message: String;
      MessageID: Integer);
  private
    { Private declarations }
  public
    CMUFile: string;
  end;

var
  Form1: TForm1;

implementation

uses filter, registry, lpl_sysutils, Win32Dlg;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
Var
 URL: String;
 I: Integer;
begin
 // Ensure elevation is active (will fail on newer versions of windows otherwise)
 If not HasAdminAccess then begin
  RestartApplicationWithElevatedPriviliges;
  Application.Terminate;
  Application.ProcessMessages;
 end;

 URL := '';
 For I := 1 to paramcount do begin
  If uppercase(GetFilt(paramstr(I), 1, '=')) = '/URL' then begin
   URL := GetFilt(ParamStr(I), 2, '=');
  end;
  If uppercase(GetFilt(paramstr(I), 1, '=')) = '/CMU' then begin
   CMUFile := GetFilt(ParamStr(I), 2, '=');
  end;
 end;
 If URL = '' then begin
  MessageDlg('No URL was passed to the updater, thus no update can be performed.', mtError, [mbAbort], 0);
  halt;
 end;
 If CMUFile = '' then begin
  MessageDlg('No Extractpath was passed thus extraction cannot be performed', mtError, [mbAbort], 0);
  halt;
 end;
 download.URL := URL;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 download.Filename := extractfilepath(CMUFile) + '\' + GetlastFilt(download.URL, '/');
 application.ProcessMessages;
 download.DownloadFile;
end;

procedure TForm1.downloadError(Sender: TObject; ErrorCode: Integer);
begin
 MessageDlg('Download Error (' + IntToStr(ErrorCode) + ')' + #13#10 + 'Description: ' + download.GetErrorCodeDes(ErrorCode), mtError, [mbAbort], 0);
 application.Terminate;
end;

procedure TForm1.downloadProgress(Sender: TObject; Readed,
  TotalSize: Int64);
begin
 progressbar1.Position := round((Readed/totalsize)*100);
 application.ProcessMessages;
end;

procedure TForm1.downloadComplete(Sender: TObject);
begin
 Label1.Caption := 'Status : Extracting update ...';
 Progressbar1.Position := 0;
 update;
 Unrar.Filename := download.filename;
 Unrar.Directory := extractfilepath(download.filename);
 UnRar.OverrideEvent := OR_ALWAYS;
 UnRar.Mode := DFRAR_EXTRACT;
 Unrar.Extract;
 Execapp(CMUFile);
 application.Terminate;
end;

procedure TForm1.UnrarProgress(Sender: TObject; FilesProcessed, FileCount,
  SizeProcessed, SizeCount: Cardinal);
begin
 application.ProcessMessages;
end;

procedure TForm1.UnrarError(Sender: TObject; Message: String;
  MessageID: Integer);
begin
 MessageDlg('Rar Error: ' + Message, mtWarning, [mbOK], 0);
 application.ProcessMessages;
end;

end.
