program SRLUpdater;

{.INCLUDE ..\Extensions\Updater.sei}

function CheckSRL(Name: string; I: integer): boolean;
var
  X: integer;
begin
  Result := True;
  for X := 0 to Length(UpdaterArr) do
    if (UpdaterArr[X].Name = 'Plugins') then
    begin
      if (Name = 'Check') then
        Tick(UpdaterArr[X].CheckMenuItem)
      else
        Tick(UpdaterArr[X].Timer);
      Exit;
    end;
end;

procedure OpenDefScript(Sender: TObject);
var
  Script: TStringArray;
begin
  SetLength(Script, 6);
  Script[0] := 'program Untitled;';
  Script[1] := '//{$DEFINE SMART}  //Uncomment for SMART';
  Script[2] := '{.include SRL/SRL.scar}';
  if (TMenuItem(Sender).Parent.Caption = 'Reflection') then
  begin
    SetLength(Script, 7);
    Script[1] := '{$DEFINE SMART}';
    Script[3] := '{.include Reflection/Reflection.scar}';
  end;
  Script[Length(Script) - 3] := '';
  Script[Length(Script) - 2] := 'begin';
  Script[Length(Script) - 1] := 'end.';

  WriteLn('Opening ' + TMenuItem(Sender).Parent.Caption + ' Default Script...');
  OpenScript('', Implode({$IFDEF WIN}#13+{$ENDIF}#10, Script), False);
end;

function OpenDefMenu(const Blank: string; const ID: integer): boolean;
var
  OpenMenuItem: TMenuItem;
begin
  OpenMenuItem := MoveMenuItem(UpdaterArr[ID].MainMenu, GetIndex(UpdaterArr[ID].MainMenu, GetMenuByName('Open', UpdaterArr[ID].MainMenu)), GetIndex(UpdaterArr[ID].MainMenu, GetMenuByName('Update', UpdaterArr[ID].MainMenu)));
  OpenMenuItem.OnClick := @OpenDefScript;
  OpenMenuItem.Visible := True;
end;

function BeforePlugins(NotNeeded: string; I: integer): boolean;
begin
  Result := ForceDirectories(UpdaterArr[I].Folder);
end;

function SuccessPlugins(Cont: string; I: integer): boolean;
var
  X: integer;
  Files: TStringArray;
  Overwrite: boolean;
begin
  Files := GetFiles(UpdaterArr[I].Folder, {$IFDEF WIN}'dll'{$ELSE}'so'{$ENDIF});
  Overwrite := True;
  for X := 0 to High(Files) do
    if (FileExists(PluginPath + Files[X])) then
    begin
      Overwrite := (MessageDlg('SRL Updater', 'Do you want to overwrite the plugins?', mtConfirmation, [mbNo, mbYes], 0) = mrYes);
      Break;
    end;

  Result := UnTarEx(Cont, PluginPath, Overwrite);
end;

procedure Init;
var
  SRL, Plugins, Refl: integer;
begin
  if (AddUpdater('SRL', 'http://wizzup.org/static/srl/srl.tar.bz2', 'http://wizzup.org/static/srl/srl_version', IncludePath, True, True, SRL)) then
  begin
    UpdaterArr[SRL].Hooks[CHECK_FOR_UPDATE] := @CheckSRL;
    UpdaterArr[SRL].Hooks[ATTACH_HOOK] := @OpenDefMenu;
  end;

  if (AddUpdater('Plugins', 'http://wizzup.org/static/srl/simba_plugins.tar.bz2', 'http://wizzup.org/static/srl/plugins_version', IncludePath + 'SRL/SimbaPlugins/', False, False, Plugins)) then
  begin
    UpdaterArr[Plugins].Hooks[BEFORE_UPDATE] := @BeforePlugins;
    UpdaterArr[Plugins].Hooks[SUCCESS_UPDATE] := @SuccessPlugins;
  end;

  if (AddUpdater('Reflection', 'http://wizzup.org/static/srl/srlref.tar.bz2', 'http://wizzup.org/static/srl/srl_refl_version', IncludePath, True, True, Refl)) then
  begin
    UpdaterArr[Refl].Hooks[ATTACH_HOOK] := @OpenDefMenu;
  end;
end;

function GetName: string;
begin
  Result := 'SRL Updater';
end;

function GetVersion: string;
begin
  Result := '0.1';
end;

begin
end.
