#region - FreePrimoSetup install script - (Automated with WinWait functions)

Opt('TrayIconDebug', 1)
Opt('WinDetectHiddenText', 1)
Opt('WinTitleMatchMode', 4)
Opt("WinWaitDelay",100)

; Installer.
$executable = 'FreePrimoSetup.exe'
; Show progess.
$splash = 0
; Default catagory folder in startmenu.
$group = 'PrimoPDF'
; New catagory to move the default folder into.
$catagory = ''
; Installation folder in Program Files.
$directory = 'activePDF\PrimoPDF'

; Run the installer.
$pid = _Install()
If WinWait('Programa de instalación', '&Siguiente >', 60) Then
    WinMove('Programa de instalación', '&Siguiente >',@DesktopWidth, 10)
    ControlClick('Programa de instalación', '&Siguiente >', 'Button3')

    WinWait('Programa de instalación', 'Acepto los términos')
    ControlClick('Programa de instalación', 'Acepto los términos', 'Button3')
    ControlClick('Programa de instalación', 'Acepto los términos', 'Button1')

    WinWait('Programa de instalación', '&Siguiente >')
    ControlSetText('Programa de instalación', '&Siguiente >', 'Edit1', @ProgramFilesDir & '\' & $directory)
    ControlClick('Programa de instalación', '&Siguiente >', 'Button1')

    WinWait('PrimoPDF Setup', '')
    ControlClick('PrimoPDF Setup', '', 'Button1')

    WinWait('Programa de instalación', '&Siguiente >')
    ControlClick('Programa de instalación', '&Siguiente >', 'Button4')
    ControlClick('Programa de instalación', '&Siguiente >', 'Button1')
Else
    _Abort()
EndIf
ProcessWaitClose($pid)

; Remove shortcuts.
If _MainShortcut('PrimoPDF_2.0 Manual.lnk') Then
    FileDelete('Uninstall PrimoPDF.lnk')
EndIf

#endregion

Exit

Func _Install($path = 'Default')
    ; Run the installer in Default Script directory.
    Dim $splash, $processblock
    If $path = 'Default' Then $path = @ScriptDir
    If StringRight($path, 1) <> '\' Then $path = $path & '\'
    If StringInStr($executable, '\') Then $path = ''
    If Not FileExists($path & $executable) Then Exit(1)
    If $processblock <> '' Then Call('_' & 'ProcessBlock')
    If $splash Then _Splash('Installing:' & StringTrimRight(StringReplace(@ScriptName, '_', ' '), 4))
    If StringRight($executable, 3) = 'msi' Then
        Return Run(@SystemDir & '\msiexec /i "' & $path & $executable & '"')
    Else
        Return Run($path & $executable)
    EndIf
EndFunc

Func _Abort()
    ; close process if exists then exit.
    Dim $pid
    If ProcessExists($pid) Then
        ProcessClose($pid)
        Exit(2)
    Else
        Exit(3)
    EndIf
EndFunc

Func _Desktop($shortcut)
    ; Delete a Desktop shortcut.
    If FileExists(@DesktopDir & '\' & $shortcut) Then
        Return FileChangeDir(@DesktopDir) And FileDelete($shortcut)
    ElseIf FileExists(@DesktopCommonDir & '\' & $shortcut) Then
        Return FileChangeDir(@DesktopCommonDir) And FileDelete($shortcut)
    EndIf
EndFunc

Func _MainShortcut($shortcut, $rename = '')
    ; Change working directory to correct StartMenu\Group directory.
    Dim $group, $catagory, $splash
    If $group = '' Then Return 0
    If FileExists(@ProgramsDir & '\' & $group) Then
        FileChangeDir(@ProgramsDir & '\' & $group)
    ElseIf FileExists(@ProgramsCommonDir & '\' & $group) Then
        FileChangeDir(@ProgramsCommonDir & '\' & $group)
    Else
        Return 0
    EndIf
    ; Wait for main shortcut.
    If $splash Then _Splash('Waiting for shortcuts')
    For $i = 1 To 20
        If FileExists($shortcut) Then ExitLoop
        Sleep(500)
    Next
    If $splash Then _Splash('Cleaning up:' & StringTrimRight(StringReplace(@ScriptName, '_', ' '), 4))
    ; If catagory not assigned anything, then return.
    If $catagory = '' Then Return 1
    ; Move the group folder into the catagory folder.
    If FileChangeDir('..') And DirCopy($group, $catagory & '\' & $group, 1) Then
        If DirRemove($group, 1) Then
            ; If optional rename parameter is used, then rename the group folder.
            If $rename <> '' And FileChangeDir($catagory) Then
                If DirCopy($group, $rename, 1) And DirRemove($group, 1) Then
                    Return FileChangeDir($rename)
                EndIf
            Else
                Return FileChangeDir($catagory & '\' & $group)
            EndIf
        EndIf
    EndIf
EndFunc

Func _QuickLaunch($shortcut)
    ; Delete a Quicklaunch shortcut.
    Local $subdirs = '\Microsoft\Internet Explorer\Quick Launch'
    If FileExists(@AppDataDir & $subdirs & '\' & $shortcut) Then
        Return FileChangeDir(@AppDataDir & $subdirs) And FileDelete($shortcut)
    ElseIf FileExists(@AppDataCommonDir & $subdirs & '\' & $shortcut) Then
        Return FileChangeDir(@AppDataCommonDir & $subdirs) And FileDelete($shortcut)
    EndIf
EndFunc

Func _Splash($text = '')
    ; Shows a small borderless splash message.
    Dim $splash
    If $splash Then
        If $text Then
            SplashTextOn('', $text, 500, 25, -1, 5, 1, '', 14)
        Else
            SplashOff()
        EndIf
    EndIf
EndFunc

Func _WinClose($title, $text = '')
    ; Close a window with further attempts.
    For $i = 1 To 10
        WinClose($title, $text)
        If Not WinExists($title) Then Return 1
        Sleep(500)
    Next
EndFunc

Func OnAutoItStart()
    ; A 2nd script instance will exit.
    Local $interpreter
    If StringInStr($cmdlineraw, '/dummy') Then Exit
    $interpreter = StringTrimRight(@ScriptName, 4) & ' Script Interpreter'
    If WinExists($interpreter) Then Exit
    AutoItWinSetTitle($interpreter)
EndFunc