#region - Daemon Tools v4.00 install script - (Automated with WinExists functions)

Opt('TrayIconDebug', 1)
Opt('WinDetectHiddenText', 1)
Opt('WinTitleMatchMode', 4)

; Installer.
$executable = 'Daemon Tools v4.00.exe'
; Show progess.
$splash = 0
; Default catagory folder in startmenu.
$group = 'DAEMON Tools'
; New catagory to move the default folder into.
$catagory = ''
; Installation folder in Program Files.
$directory = 'DAEMON Tools'
; Allowed time for installation.
$allowed = 300 * 1000
; Auto or manual reboot. (either choice will runonce the installer at next reboot)
$auto_reboot = 0

; ** README ""
; Check values above and "Remove shortcuts" section below are correct.
; This is a 2 part install. The 2nd part automatically happens on reboot at HKCU\RunOnce
;   which is when the Startup folder is processed and the Desktop is active. This script
;   is set to manage both parts of installation automagically. An AutoIt Shutdown() request
;   can be added to the end of this script if it is the last item with a Unattended Windows Setup.
; Spyware is unchecked for installation. ;)

; Run the installer.
If $auto_reboot And StringInStr($cmdlineraw, '/postrun') Then
    If WinWait('DAEMON Tools 4.00HE', 'Welcome to the DAEMON Tools', 30) Then
        $pid = $executable
    Else
        $pid = _Install()
    EndIf
Else
    $pid = _Install()
EndIf
$time = TimerInit()
Do
    Select ; 1st 3 windows are messageboxes.
        Case WinExists('SPTD setup V1.21', 'This program will install SCSI Pass Through Direct')
            ControlClick('SPTD setup V1.21', 'This program will install SCSI Pass Through Direct', 'Button1')
            Sleep(1000)

        Case WinExists('DAEMON Tools 4.00HE', 'Setup must restart Windows')
            _RunOnce()
            If $auto_reboot Then
                $choice = 'Button1'
            Else
                $choice = 'Button2'
            EndIf
            ControlClick('DAEMON Tools 4.00HE', 'Setup must restart Windows', $choice)
            WinWaitClose('DAEMON Tools 4.00HE', 'Setup must restart Windows', 5)
            Exit

        Case WinExists('DAEMON Tools 4.00HE', 'You must reboot after previous operation')
            _RunOnce()
            ControlClick('DAEMON Tools 4.00HE', 'You must reboot after previous operation', 'Button1')
            ; Desktop Reboot after 20 seconds.
            Sleep(15*1000)
            MsgBox(262144, 'DAEMON Tools 4.00HE', 'System restart commencing now', 5)
            Shutdown(2)
            Exit

        Case WinExists('DAEMON Tools 4.00HE', 'Welcome to the DAEMON Tools')
            ControlClick('DAEMON Tools 4.00HE', 'Welcome to the DAEMON Tools', 'Button2')

        Case WinExists('DAEMON Tools 4.00HE', 'License Agreement')
            ControlClick('DAEMON Tools 4.00HE', 'License Agreement', 'Button2')
           
        Case WinExists('DAEMON Tools 4.00HE', 'Already Installed')
            $text = 'Update the automated installation script for uninstall or upgrade support, If required'
            MsgBox(262144, 'DAEMON Tools 4.00HE', $text, 5)
            _Abort()

        Case WinExists('DAEMON Tools 4.00HE', 'Choose Components')
            ControlFocus('DAEMON Tools 4.00HE', 'Choose Components', 'SysTreeView321')
            ControlSend('DAEMON Tools 4.00HE', 'Choose Components', 'SysTreeView321', '{DOWN}{SPACE}')
            ControlClick('DAEMON Tools 4.00HE', 'Choose Components', 'Button2')

        Case WinExists('DAEMON Tools 4.00HE', 'Choose the folder')
            ControlSend('DAEMON Tools 4.00HE', 'Choose the folder', 'Edit1', @ProgramFilesDir & '\' & $directory)
            ControlClick('DAEMON Tools 4.00HE', 'Choose the folder', 'Button2')

        Case WinExists('DAEMON Tools 4.00HE', 'Completing the DAEMON Tools')
            ControlCommand('DAEMON Tools 4.00HE', 'Completing the DAEMON Tools', 'Button4', 'UnCheck', '')
            ControlClick('DAEMON Tools 4.00HE', 'Completing the DAEMON Tools', 'Button2')
            ExitLoop
    EndSelect
    Sleep(1000)
    If TimerDiff($time) > $allowed Then _Abort()
Until Not ProcessExists($pid)

; Remove shortcuts.
If _MainShortcut('DAEMON Tools.lnk') Then
    FileDelete('Uninstall.lnk')
    _Desktop('DAEMON Tools.lnk')
EndIf

; Delete spyware installer.
If FileExists(@ProgramFilesDir & '\' & $directory & '\SetupDTSB.exe') Then
    FileDelete(@ProgramFilesDir & '\' & $directory & '\SetupDTSB.exe')
EndIf

; Remove Autorun entry.
;~ RegDelete('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 'DAEMON Tools')

#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

Func _RunOnce()
    ; Add 2nd part to installation.
    Local $key = 'HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce'
    If @Compiled Then
        RegWrite($key, '_DTools', 'Reg_sz', '"' & @ScriptFullPath & '" /postrun')
    Else
        RegWrite($key, '_DTools', 'Reg_sz', '"' & @AutoItExe & '" "' & @ScriptFullPath & '" /postrun')
    EndIf
EndFunc