#NoTrayIcon
  
- #region - Global Variables
  ; Constants
  Global Const $IDOK = 1
  Global Const $IDYES = 6
  ; GuiConstants
  Global Const $GUI_DISABLE = 128
  Global Const $WS_POPUP = 0x80000000
  Global Const $PBS_SMOOTH = 1
  Global Const $COLOR_WHITE = 0xFFFFFF
  Global Const $GUI_BKCOLOR_TRANSPARENT = -2
  ; Used for T-00 restart from registry
  Global $pass_on_switch = ''
  ; Ini file containing custom User Settings
  Global $file_ini = @ScriptDir & '\Settings.ini'
  #endregion
  
- #region - User Settings
  ; Gui title. Should not require alteration.
  Const $GUI_TITLE = 'Deployment Script'
  ; Picture to use as background.
  $picture = _PictureSearch()
  ; Color of software installing font.(RGB format)
  $color_top = IniRead($file_ini, 'color', 'top', '0xFF8000')
  ; Color of software list font.
  $color_side = IniRead($file_ini, 'color', 'side', '0xFFFFFF')
  ; Progress Bar color.
  $color_progress = IniRead($file_ini, 'color', 'progress', '0x079E05')
  ; Where to search for au3 file types.
  $dir_t12_software = @ScriptDir & '\T-12_Software'
  ; Where to search for au3 file types to RunOnce only.
  $dir_t00_software = @ScriptDir & '\T-00_Software'
  ; Where to save the log file.
  $file_log = @WindowsDir & '\Deployment.log'
  ; Set Gui as Always On Top. Press Spacebar to toggle on or off.
  $state_on_top = 1
  ; Check all other drives for software folders to use as new default
  $drive = DriveGetDrive('ALL')
- If Not @error Then
-     For $i = 1 to $drive[0]
-         If DriveStatus($drive[$i]) = 'READY' Then
-             If FileExists($drive[$i] & '\T-12_Software') Then
                  $dir_t12_software = $drive[$i] & '\T-12_Software'
              EndIf
-             If FileExists($drive[$i] & '\T-00_Software') Then
                  $dir_t00_software = $drive[$i] & '\T-00_Software'
              EndIf
          EndIf
      Next
  EndIf
  #endregion
  
- #region - CmdlineSwitch
- If $CMDLINE[0] Then
-     For $i = 1 To $CMDLINE[0]
-         If StringLeft($CMDLINE[$i], 1) = '/' Then
-             Switch $CMDLINE[$i]
-                 Case '/?'
                      MsgBox(0x40000, StringTrimRight(@ScriptName, 4) & ' Help', _
                          'Switches are:' & @LF _
                           & @LF & '/Extract' _
                           & @LF & @TAB & 'Extract source files to current directory' _
                           & @LF & '/Execute' _
                           & @LF & @TAB & 'Installation mode which will happen immediately' _
                           & @LF & '/NoOnTop' _
                           & @LF & @TAB & 'Sets permanent OnTop status of the background to disabled' _
                           & @LF & '/Test' _
                           & @LF & @TAB & 'Display the background for a small period of time')
                      Exit
-                 Case '/extract'
                      FileInstall('Deployment.au3', @ScriptDir & '\')
                      FileInstall('Default.gif', @ScriptDir & '\')
                      Exit
-                 Case '/execute', '/test'
                      AdlibEnable('_Adlib', 1000)
-                     If StringInStr($CMDLINERAW, '/NoOnTop') Then
                          $pass_on_switch = ' /NoOnTop'
                          $state_on_top = 0
                      EndIf
                      _Deployment($dir_t12_software)
                      AdlibDisable()
                      Exit
-                 Case '/runonce'
                      AdlibEnable('_Adlib', 1000)
-                     If StringInStr($CMDLINERAW, '/NoOnTop') Then
                          $state_on_top = 0
                      EndIf
                      _Deployment($dir_t00_software)
                      AdlibDisable()
                      Exit
-                 Case '/NoOnTop'
                      ContinueLoop
-                 Case Else
                      MsgBox(0x40000, 'Incorrect switch used', _
                           'Command used:' & @CRLF & $CMDLINERAW & @CRLF & _
                           @CRLF & 'Use /? for the switches available.', 60)
                      Exit
              EndSwitch
          EndIf
      Next
- Else
-     If Not FileExists(@ScriptDir & '\..\$OEM$') Then
          MsgBox(0x40010, @ScriptName, 'I belong in the $OEM$ folder', 60)
          Exit
      EndIf
-     If MsgBox(0x40021, @ScriptName, 'Do you want to setup the folder and file layout', 60) = $IDOK Then
-         If Not FileExists($dir_t12_software) Then
              DirCreate($dir_t12_software)
          EndIf
-         If Not FileExists($dir_t00_software) Then
              DirCreate($dir_t00_software)
          EndIf
-         If Not FileExists(@ScriptDir & '\_Cleanup.au3') Then
              FileWriteLine(@ScriptDir & '\_Cleanup.au3', '; This script will execute after the software installs at T-12')
          EndIf
-         If Not FileExists(@ScriptDir & '\_Prepare.au3') Then
              FileWriteLine(@ScriptDir & '\_Prepare.au3', '; This script will execute before the software installs at T-12')
          EndIf
-         If Not FileExists(@ScriptDir & '\_Startup.au3') Then
              FileWriteLine(@ScriptDir & '\_Startup.au3', '; This script will execute when the desktop is active at T-00')
          EndIf
-         If Not FileExists(@ScriptDir & '\Cmdlines.txt') Then
              FileWrite(@ScriptDir & '\Cmdlines.txt', '[COMMANDS]' & @CRLF & '"' & @ScriptName & ' /Execute"')
          EndIf
-         If Not FileExists($file_ini) Then
              IniWrite($file_ini, 'color', 'top', '0xFF8000')
              IniWrite($file_ini, 'color', 'side', '0xFFFFFF') 
              IniWrite($file_ini, 'color', 'progress', '0x079E05')
          EndIf
          Sleep(2000)
          MsgBox(0x40040, @ScriptName, 'Basic layout is now setup.' & @CRLF _
               & 'Add your installers and Au3 scripts' & @CRLF _
               & 'into the T-12_Software folder or' & @CRLF _
               & 'into the T-00_Software folder')
      EndIf
      Exit
  EndIf
  #endregion
  
  Exit
  
- Func _Deployment($dir_software)
-     ; Execute _Prepare.au3, execute all scripts in the .\T-12_Software folder, execute _Cleanup.au3.
      ; Execute all scripts in the .\T-00_Software folder and copy _Startup.au3 to Windows directory,
      ; add RunOnce entry into HKLM\...\RunOnce key and 
      ; add _Startup.au3 information into HKCU\...\RunOnce key.
      Opt('RunErrorsFatal', 0)
      Opt('GUIResizeMode', 1)
-     If $state_on_top Then
          HotKeySet('{SPACE}', '_ToggleOnTop')
      EndIf
      Local $file_count, $item_side, $item_top, $script, $font_size
      Local $time_total, $time_install
      Local $handle_search, $handle_write
      Local Const $AUTOITNAME = _AutoItName()
      Dim $state_on_top, $picture, $color_top, $color_side, $color_progress
      Dim $dir_log, $error = 0
      ; If install directory not found, then return from function
      If Not FileExists($dir_software) Then Return False
      ; Get screen size for setting font size
-     Switch @DesktopWidth
-         Case 640 To 1200
              $font_size = 8
-         Case 1201 To 1601
              $font_size = 12
-         Case 1602 To 5000
              $font_size = 14
-         Case Else
              $font_size = 8
      EndSwitch
      ; Setup the Gui based background
      GUICreate($GUI_TITLE, 640, 480, 0, 0, $WS_POPUP)
-     If FileExists($picture) Then
          GUICtrlCreatePic($picture, 0, 0, 640, 480)
          GUICtrlSetState(Default, $GUI_DISABLE)
      EndIf
      $label_top = GUICtrlCreateLabel($item_top, 10, 50, 170, 17)
      $label_side = GUICtrlCreateLabel($item_side, 10, 65, 170, 325)
      $ctrl_progress = GUICtrlCreateProgress(25, 410, 125, 15, $PBS_SMOOTH)
      GUICtrlSetColor($label_top, $color_top)
      GUICtrlSetColor($label_side, $color_side)
      GUICtrlSetColor($ctrl_progress, $color_progress)
      GUICtrlSetBkColor($label_top, $GUI_BKCOLOR_TRANSPARENT)
      GUICtrlSetBkColor($label_side, $GUI_BKCOLOR_TRANSPARENT)
      GUICtrlSetBkColor($ctrl_progress, $COLOR_WHITE)
      GUICtrlSetFont($label_top, $font_size + 2, 600)
      GUICtrlSetFont($label_side, $font_size)
      GUISetState(@SW_HIDE)
      WinWait($GUI_TITLE)
      WinSetOnTop($GUI_TITLE, '', $state_on_top)
      WinMove($GUI_TITLE, '', 0, 0, @DesktopWidth, @DesktopHeight)
      GUISetState(@SW_SHOW)
      GUISetBkColor(PixelGetColor(5, @DesktopHeight / 2))
      ; Fill the side list with available Software script names.
      $handle_search = FileFindFirstFile($dir_software & '\*.au3')
-     If $handle_search <> - 1 Then
-         For $file_count = 1 To 500
              $data = FileFindNextFile($handle_search)
              If @error Then ExitLoop
              If StringRight($data, 3) <> 'au3' Then ContinueLoop
              If StringLeft($data, 2) = '__' Then ContinueLoop
              $item_side &= @LF & StringTrimRight(StringReplace($data, '_', ' '), 4)
          Next
          GUICtrlSetData($label_side, $item_side)
          FileClose($handle_search)
      EndIf
-     If StringInStr($CMDLINERAW, '/test') Then
          ; Test run only
          $time_total = TimerInit()
          GUICtrlSetData($ctrl_progress, 66)
          Sleep(5000)
          GUICtrlSetData($ctrl_progress, 100)
-     Else
          ; Start logging.
          $handle_write = FileOpen($file_log, 2)
          FileWrite($handle_write, 'Install Directory: "' & $dir_software & '"' & @CRLF)
          $time_total = TimerInit()
-         ;
          ; T-12. Run the _Prepare.au3 script
-         If StringInStr($CMDLINERAW, '/Execute') Then
-             If FileExists(@ScriptDir & '\_Prepare.au3') Then
                  GUICtrlSetData($label_top, 'Preparing for Software')
                  $exitcode = RunWait(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptDir & '\_Prepare.au3"')
-                 If @error Or $exitcode Then
                      If FileWrite($handle_write, 'Error(' & $exitcode & '): _Prepare.au3' & @CRLF) Then $error += 1
                  EndIf
-             Else
                  If FileWrite($handle_write, 'Error: _Prepare.au3 not found in "' & @ScriptDir & '"' & @CRLF) Then $error += 1
              EndIf
              GUICtrlSetData($label_top, '')
          EndIf
-         ;
          ; Run all the Software scripts available.
          $handle_search = FileFindFirstFile($dir_software & '\*.au3')
-         If $handle_search <> - 1 Then
-             For $i = 1 To 500
                  $script = FileFindNextFile($handle_search)
                  If @error Then ExitLoop
                  If StringRight($script, 3) <> 'au3' Then ContinueLoop
                  If StringLeft($script, 2) = '__' Then ContinueLoop
                  $item_side = StringReplace($item_side, StringTrimRight(StringReplace($script, '_', ' '), 4) & @LF, '')
                  GUICtrlSetData($label_top, StringTrimRight(StringReplace($script, '_', ' '), 4))
                  GUICtrlSetData($label_side, $item_side)
                  $time_install = TimerInit()
                  Sleep(1000)
                  $exitcode = RunWait(@AutoItExe & ' /AutoIt3ExecuteScript "' & $dir_software & '\' & $script & '"')
-                 If @error Or $exitcode Then
                      If FileWrite($handle_write, $script & @CRLF & 'Error(' & $exitcode & ')' & @CRLF & @CRLF) Then $error += 1
                      ContinueLoop
                  EndIf
                  GUICtrlSetData($ctrl_progress, 100 / $file_count * $i)
                  FileWrite($handle_write, $script & @CRLF & Round(TimerDiff($time_install) / 1000) & ' seconds' & @CRLF & @CRLF)
              Next
              GUICtrlSetData($item_side, '')
              FileClose($handle_search)
          EndIf
-         ;
          ; T-12. Run Cleanup, Copy Startup.au3 to Windows folder and add to registry and add restart for RunOnce
-         If StringInStr($CMDLINERAW, '/execute') Then
              ; T-12. Run the _Cleanup.au3
-             If FileExists(@ScriptDir & '\_Cleanup.au3') Then
                  GUICtrlSetData($label_top, 'Cleanup in Progress')
                  $exitcode = RunWait(@AutoItExe & ' "' & @ScriptDir & '\_Cleanup.au3"')
-                 If @error Or $exitcode Then
                      If FileWrite($handle_write, 'Error(' & $exitcode & '): _Cleanup.au3' & @CRLF) Then $error += 1
                  EndIf
-             Else
                  If FileWrite($handle_write, 'Error: _Cleanup.au3 not found in "' & @ScriptDir & '"' & @CRLF) Then $error += 1
              EndIf
              GUICtrlSetData($label_top, '')
              ; T-12. Copy _Startup.au3 to Windows directory and hide it, then add it to HKCU\...\Runonce key in registry
-             If FileExists(@ScriptDir & '\_Startup.au3') Then
-                 If FileCopy(@ScriptDir & '\_Startup.au3', @WindowsDir & '\', 1) Then
-                     If Not FileSetAttrib(@WindowsDir & '\_Startup.au3', '+SHR') Then
                          If FileWrite($handle_write, 'Error: _Startup.au3 FileSetAttrib' & @CRLF) Then $error += 1
                      EndIf
-                     If Not FileCopy(@AutoItExe, @WindowsDir & '\', 1) Then
                          If FileWrite($handle_write, 'Error: "' & $AUTOITNAME & '" FileCopy' & @CRLF) Then $error += 1
-                     ElseIf Not FileSetAttrib(@WindowsDir & '\' & $AUTOITNAME, '+SHR') Then
                          If FileWrite($handle_write, 'Error: "' & $AUTOITNAME & '" FileSetAttrib' & @CRLF ) Then $error += 1
                      EndIf
                      RegWrite('HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce', 'zFirstLogon', 'Reg_sz', _
                           '"' & @WindowsDir & '\' & @ScriptName & '" /AutoIt3ExecuteScript "' & @WindowsDir & '\_Startup.au3"')
-                     If @error Then
                          If FileWrite($handle_write, 'Error: _Startup.au3 RegWrite' & @CRLF) Then $error += 1
                      EndIf
-                 Else
                      If FileWrite($handle_write, 'Error: _Startup.au3 FileCopy' & @CRLF) Then $error += 1
                  EndIf
-             Else
                  If FileWrite($handle_write, 'Error: _Startup.au3 not found in "' & @ScriptDir & '"' & @CRLF) Then $error += 1
              EndIf
              ; T-12. Prepare HKLM\...\RunOnce entry
-             If StringInStr($CMDLINERAW, '/Execute') Then
-                 If FileExists(@ScriptDir & '\' & $dir_t00_software) Then
                      RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce', $script, 'Reg_sz', '"' & @ScriptFullPath & '" /RunOnce' & $pass_on_switch)
-                 Else
                      If FileWrite($handle_write, 'Error: T-12_Software folder not found in "' & @ScriptDir & '"' & @CRLF) Then $error += 1
                  EndIf
              EndIf           
          EndIf
-         ;
          ; Finalize Software Deployment  
          FileWrite($handle_write, _total($time_total, $error))
          FileClose($handle_write)
      EndIf
      Sleep(1000)
      GUICtrlSetData($label_top, '     Final Result     ')
      GUICtrlSetData($label_side, _total($time_total, $error))
      Sleep(10000)
      GUIDelete()
-     If FileExists(@TempDir & '\background.gif') Then
          FileDelete(@TempDir & '\background.gif')
      EndIf
  EndFunc
  
- Func _ToggleOnTop()
      ; Toggle for setting Gui as always on top or not.
      Dim $state_on_top
      $state_on_top = Not $state_on_top
      Return WinSetOnTop($GUI_TITLE, '', $state_on_top)
  EndFunc
  
- Func _PictureSearch()
      Local $picture = '', $file, $handle_search
      Local $handle_search = FileFindFirstFile(@ScriptDir & '\background.*')
-     If $handle_search <> -1 Then
-         While 1
              $file = FileFindNextFile($handle_search)
              If @error Then ExitLoop
-             Switch $file
-                 Case 'background.bmp'
                      $picture = @ScriptDir & '\background.bmp'
                      ExitLoop
-                 Case 'background.gif'
                      $picture = @ScriptDir & '\background.gif'
                      ExitLoop
-                 Case 'background.jpg'
                      $picture = @ScriptDir & '\background.jpg'
                      ExitLoop
              EndSwitch
          WEnd
          FileClose($handle_search)
      EndIf
-     Switch $picture
-         Case ''
-             If FileInstall('Default.gif', @TempDir & '\background.gif', 1) Then
                  $picture = @TempDir & '\background.gif'
                  Sleep(500)
              EndIf
      EndSwitch
      Return $picture
  EndFunc
  
- Func _Total($time_total = 0, $error = 0)
      Return  @CRLF & 'Deployment Date = ' & @MDAY & '/' & @MON & '/' & @YEAR & _
              @CRLF & 'AutoIt Version = ' & @AutoItVersion & _
              @CRLF & 'Total time = ' & Round(TimerDiff($time_total) / 1000 / 60) & ' minutes' & _
              @CRLF & 'Error Count = ' & $error & @CRLF & @CRLF & @CRLF
  EndFunc
  
- Func _Adlib()
-     If WinExists('AutoIt Error') Then
          Local $handle_write, $text
          $handle_write = FileOpen(@WindowsDir & '\AutoIt_Error.log', 1)
-         If $handle_write <> -1 Then
              $text = WinGetText('AutoIt Error')
              $text = StringReplace($text, @LF, @CRLF)
              FileWrite($handle_write, $text & @CRLF & '-#-#-#-#-#-#-' & @CRLF)
              FileClose($handle_write)
          EndIf
-         While WinExists('AutoIt Error')
              WinClose('AutoIt Error')
              Sleep(1000)
          WEnd
      EndIf
  EndFunc
  
- Func _AutoItName()
      ; Return AutoItName from AutoItExe path
-     If @Compiled Then
          Return @ScriptName
-     Else
          Return StringTrimLeft(@AutoItExe, StringInStr(@AutoItExe, '\', 0, -1))
      EndIf
  EndFunc
  
- Func _AutoItDir()
      ; Return AutoItDir from AutoItExe path
-     If @Compiled Then
          Return @ScriptDir
-     Else
          Return StringLeft(@AutoItExe, StringInStr(@AutoItExe, '\', 0, -1) -1)
      EndIf
  EndFunc
  
- Func _AutoItFullPath()
      ; Return AutoItFullPath from AutoItExe
      Return @AutoItExe
  EndFunc
  
- Func OnAutoItStart()
      ; Only one instance
      If WinExists(@ScriptName & '_Interpreter') Then Exit
      AutoItWinSetTitle(@ScriptName & '_Interpreter')
  EndFunc