#region - Autoit install script - (Nullsoft)
#include <Common_Functions.au3>
Opt('TrayIconDebug', 1)
Break(False)

; Installer.
$executable = 'Autoit 3.2.4.8.exe'
; Default group folder in startmenu.
$group = 'AutoIt v3'
; Installation folder in Program Files.
$directory = 'AutoIt3'

; Run the installer.
$pid = _Nullsoft()
ProcessWaitClose($pid)

; Copy extra files to program folder.
$source = @ScriptDir & '\files\autoit'
$destination = @ProgramFilesDir & '\' & $directory
If FileExists(@ScriptDir & '\files\autoit') Then
    DirCopy($source, $destination, 1)
    DirCreate($destination & '\Beta')
    FileCopy($source & '\AutoIt3Help2.au3', $destination & '\Beta')
    FileCopy($source & '\AutoIt3Help2.exe', $destination & '\Beta')
EndIf

; Remove shortcuts.
If _Programs('AutoIt Help File.lnk') Then
    FileMove($group & '\AutoIt Help File.lnk', 'Development\', 9)
    FileMove($group & '\AutoIt Window Info.lnk', 'Development\', 9)
    FileMove($group & '\Scite Script Editor.lnk', 'Development\Scite4AutoIt3.lnk', 9)
    DirRemove($group, 1)
EndIf

If FileChangeDir(@ProgramFilesDir & '\' & $directory) Then
    ; Installed psapi.dll already exists in SystemDir, so delete it.
    If FileExists(@SystemDir & '\psapi.dll') Then
        FileDelete('psapi.dll')
    EndIf
    If FileExists('AutoIt v3 Website.url') Then
        FileDelete('AutoIt v3 Website.url')
    EndIf
    If FileExists(@WorkingDir & '\AutoIt3.exe') Then
        ConsoleWrite('AutoIt Version Installed: ' & FileGetVersion(@WorkingDir & '\AutoIt3.exe') & @CRLF)
    EndIf
EndIf

If @WorkingDir <> @ScriptDir Then
    FileChangeDir(@ScriptDir)
EndIf

; Install AutoIt Beta if exist
$beta_script = @ScriptDir & '\__AutoIt_Beta.au3'
$beta_installer = @ScriptDir & '\AutoIt_Beta.exe'
If FileExists($beta_installer) And FileExists($beta_script) Then
    RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $beta_script & '"')
EndIf

; Remove Edit and use Open as default for Edit.
If RegDelete('HKCR\AutoIt3Script\Shell\Edit') And Not @error Then
    RegWrite('HKCR\AutoIt3Script\Shell\Open', '', 'Reg_sz', 'Edit Script')
EndIf
If RegRead('HKCR\AutoIt3Script\Shell', '') <> 'Open' Then
    RegWrite('HKCR\AutoIt3Script\Shell', '', 'Reg_sz', 'Open')
EndIf
; Removes edit from Contextmenu
RegDelete('HKCR\.au3', 'PerceivedType')
; Enable Drag n Drop support for a3x filetype
RegWrite('HKCR\AutoIt3XScript\Shellex\DropHandler', '', 'Reg_sz', '{86C86720-42A0-1069-A2E8-08002B30309D}')
; Add Include list of directories separated by semicolon's.
If Not RegRead('HKLM\SOFTWARE\AutoIt v3\AutoIt', 'Include') Then
    RegWrite('HKLM\SOFTWARE\AutoIt v3\AutoIt', 'Include', 'Reg_sz', '')
EndIf

Exit
#endregion

Func _AutoItVersion($scriptver)
    Local $au3ver, $expectver, $exitcode
    If Not @Compiled Then
        $au3ver = StringSplit(@AutoItVersion, '.')
        $expectver = StringSplit($scriptver, '.')
        If @error Then
            Return ConsoleWrite('_AutoItVersion() error with using StringSplit()' & @CRLF)
            Exit -2
        EndIf
        For $i = 1 To $expectver[0]
            If $au3ver[$i] = $expectver[$i] Then
                ContinueLoop
            ElseIf $au3ver[$i] > $expectver[$i] Then
                Return
            Else
                MsgBox(0x40010, @ScriptName, 'Need atleast v' & $scriptver & ' to execute this script', 10)
                $exitcode = StringReplace($scriptver, '.', '')
                Exit $exitcode
            EndIf
        Next
    EndIf
EndFunc