Global $title_script = 'Nero Prepare Repack Automation'

If $cmdline[0] <> 1 Then
    MsgBox(0, $title_script, "Drag'n'Drop the Nero Web-Installer on me")
    Exit
EndIf
If Not FileExists($cmdline[1]) Then Exit

Opt('TrayIconDebug', 1)
Opt('WinWaitDelay', 2000)

Global $file_nero = 'nero.msi'
Global $title_nero = 'Nero 7 Demo - Installation Wizard'

If MsgBox(0x40021, $title_script, 'Do you want to start the repack: ' & _
        @LF & '" ' & $cmdline[1] & ' "') = 2 Then
    Exit
EndIf

$time = TimerInit()
ProgressOn($title_script, '', 'Extracting Temporary Files', 300, 20)
ProgressSet(10);
$pid = Run($cmdline[1])
ProcessWait('SetupX.exe', 120)
ProgressSet(30, 'Running Nero Part of Normal Install');
If WinWait($title_nero, 'Welcome to the Installation Wizard', 60) Then
    Sleep(5000)
    _CtrlIsEnabled($title_nero, 'Welcome to the Installation Wizard', 'Button1')
    While WinExists($title_nero, 'Welcome to the Installation Wizard')
        Sleep(1000)
        ControlClick($title_nero, 'Welcome to the Installation Wizard', 'Button1')
    WEnd
    ProgressSet(35);
    WinWait($title_nero, 'License Agreement')
    ControlCommand($title_nero, 'License Agreement', 'Button6', 'Check')
    ControlClick($title_nero, 'License Agreement', 'Button3')
    WinWait($title_nero, 'Customer Information')
    ControlClick($title_nero, 'Customer Information', 'Button2')
    ProgressSet(40);
    WinWait($title_nero, 'Are you sure you want to cancel')
    ControlClick($title_nero, 'Are you sure you want to cancel', 'Button2')
    WinWaitActive($title_nero, 'Installation Wizard Completed')
    ControlClick($title_nero, 'Installation Wizard Completed', 'Button1')
    ProcessWaitClose($pid)
EndIf

ProgressSet(50, 'Locating Nero.msi');
If FileChangeDir(@TempDir) Then
    $handle_search = FileFindFirstFile('*')
    If $handle_search <> - 1 Then
        While 1
            $file = FileFindNextFile($handle_search)
            If @error Then ExitLoop
            If StringInStr(FileGetAttrib($file), 'D') Then
                If FileExists($file & '\nero.msi') Then
                    FileChangeDir($file)
                    ExitLoop
                EndIf
            EndIf
        WEnd
        FileClose($handle_search)
    EndIf
EndIf

ProgressSet(60, 'Running Nero Admin Install');
If FileExists($file_nero) Then
    $pid = Run('msiexec /a "' & @WorkingDir & '\nero.msi"')
    If WinWait($title_nero, 'Welcome to the Installation Wizard ', 30) Then
        ControlClick($title_nero, 'Welcome to the Installation Wizard ', 'Button1')
        ProgressSet(70);
        WinWait($title_nero, 'Network Location')
        If DirCreate(@HomeDrive & '\Nero_Repack\Removed') Then
            If ControlFocus($title_nero, 'Network Location', 'RichEdit20W1') Then Sleep(500)
            ControlSetText($title_nero, 'Network Location', 'RichEdit20W1', @HomeDrive & '\Nero_Repack')
            Sleep(1000)
            ControlClick($title_nero, 'Network Location', 'Button1')
        EndIf
        ProgressSet(75);
        WinWait($title_nero, 'Installation Wizard Completed')
        ControlClick($title_nero, 'Installation Wizard Completed', 'Button1')
    EndIf
    ProcessWaitClose($pid)
    $count = 80
    ProgressSet($count, 'Folder Selection');
    $dir_nero = @HomeDrive & '\Nero_Repack'
    $dir_removed = @HomeDrive & '\Nero_Repack\Removed'
    If FileChangeDir($dir_nero) Then
        $prefix = '[FILELOCATION]'
        $handle_search = FileFindFirstFile('*')
        If $handle_search <> - 1 Then
            While 1
                $file = FileFindNextFile($handle_search)
                If @error Then ExitLoop
                $count = $count + 1
                ProgressSet($count);
                If StringInStr($file, '.') Then ContinueLoop
                If $file = $prefix Then ContinueLoop
                If $file = $prefix & 'Common Files' Then ContinueLoop
                If $file = $prefix & 'Core' Then ContinueLoop
                If $file = $prefix & 'Redist' Then ContinueLoop
                If $file = $prefix & 'System' Then ContinueLoop
                If $file = 'Redist' Then ContinueLoop
                If $file = 'Removed' Then ContinueLoop
                If MsgBox(0x40024, 'Folder Removal', $file) Then
                    DirMove($file, 'Removed', 1)
                    Sleep(250)
                EndIf
            WEnd
            FileClose($handle_search)
            ProgressSet(98, 'Cleanup');
        EndIf
        If FileChangeDir($prefix & 'Core') Then
            FileDelete('*.nls')
            If FileMove('nero.txt', 'nero.tmp') Then
                FileDelete('*.txt')
                FileMove('nero.tmp', 'nero.txt')
            EndIf
        EndIf
    EndIf
EndIf

Sleep(500)
ProgressOff()
MsgBox(0, $title_script, 'Task Completed in ' & Int(TimerDiff($time) / 1000) & ' seconds')
Sleep(500)
Run('Explorer "' & @WorkingDir & '\.."')

Exit

Func _CtrlIsEnabled($udf_title, $udf_text, $udf_classname)
    Do
        Sleep(1000)
    Until ControlCommand($udf_title, $udf_text, $udf_classname, 'IsEnabled')
    Sleep(1000)
EndFunc