;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
; AutoIt Version: 3.1.0
; Author:Andrew Calcutt 	Date:08/11/2005
; Script Function: Auto-Sysprep Utility
;-----------------------------------------------------------------------------
AutoItSetOption("RunErrorsFatal", 0) ;Do not show errors if run error
#include <GuiConstants.au3>
#include <string.au3>
#include <PrepSysFunc.au3> ;Include PrepSys Functions
;-----------------------------------------------------------------------------
Dim $settings = @HomeDrive & "\settings.ini"
;If user defined settings.ini is not on C:\ , use default in working dir
If FileExists($settings) = 0 Then
	FileCopy(StringTrimRight(@ScriptDir, 8) & "\settings.ini", $settings, 0)
	FileSetAttrib($settings, "-R")
EndIf
;if working dir is not set in ini, write working dir to ini
If IniRead($settings, "Script_Settings", "Working_Dir", "") = "" Then
	$workingdir = StringTrimRight(@ScriptDir, 8)
	If StringLen($workingdir) <> 3 Then
		$workingdir = $workingdir & "\"
	EndIf
	IniWrite($settings, "Script_Settings", "Working_Dir", $workingdir)
EndIf
;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
;Decalair Variables
Dim $shutdown = "RUN"
Dim $workingdir = IniRead($settings, "Script_Settings", "Working_Dir", "")
Dim $progressref = IniRead($settings, "Program_Use_Only", "Progress_Ref", "")
Dim $progressref2 = IniRead($settings, "Program_Use_Only", "Progress_Ref2", "")
Dim $run_todo = IniRead($settings, "Script_Settings", "Delete_Start_User", "0") + IniRead($settings, "Script_Settings", "Install_McAfee", "0") + IniRead($settings, "Script_Settings", "Install_Sophos", "0") + IniRead($settings, "Script_Settings", "Install_VPN", "0") + IniRead($settings, "Script_Settings", "Install_Office", "0") + IniRead($settings, "Script_Settings", "Set_Visual_Settings", "0") + IniRead($settings, "Script_Settings", "Copy_Profile", "0") + IniRead($settings, "Script_Settings", "Wait_Updates", "0") + IniRead($settings, "Script_Settings", "Clean_SMS", "0") + IniRead($settings, "Script_Settings", "Remove_McAfee_GUID", "0") + IniRead($settings, "Script_Settings", "Defrag", "0")
Dim $logonuser1 = IniRead($settings, "Script_Settings", "Admin_Username", "")
Dim $logonpass1 = _StringEncrypt(0, IniRead($settings, "Script_Settings", "Admin_Password", ""), "Prepsys", 2)
Dim $logonuser2 = IniRead($settings, "Script_Settings", "Sysprep_Username", "")
Dim $logonpass2 = _StringEncrypt(0, IniRead($settings, "Script_Settings", "Sysprep_Password", ""), "Prepsys", 2)
Dim $scriptlocation = @ScriptDir & "\" & @ScriptName

Dim $mcafee_exe = IniRead($settings, "Program_Use_Only", "mcafee_exe", $workingdir & "McAfee\AutoInstall_McAfee.exe")
Dim $mcafee_patch_exe = IniRead($settings, "Program_Use_Only", "mcafee_patch_exe", $workingdir & "McAfee\Patch\AutoInstall_McAfee_Patch.exe")
Dim $sophos_exe = IniRead($settings, "Program_Use_Only", "sophos_exe", $workingdir & "Sophos\AutoInstall_Sophos.exe")
Dim $vpn_exe = IniRead($settings, "Program_Use_Only", "vpn_exe", $workingdir & "VPN\AutoInstall_VPN.exe")
Dim $office_exe = IniRead($settings, "Program_Use_Only", "office_exe", $workingdir & "OFFICE\AutoInstall_Office.exe")
Dim $datatel_exe = IniRead($settings, "Program_Use_Only", "datatel_exe", $workingdir & "DATATEL\AutoInstall_Datatel.exe")
Dim $deepfreeze_exe = IniRead($settings, "Program_Use_Only", "deepfreeze_exe", $workingdir & "Deepfreeze\DFSeed.bat")
Dim $cleansms_exe = IniRead($settings, "Program_Use_Only", "cleansms_exe", $workingdir & "CleanSMS\AutoRun_cleansms.exe")

Dim $netuser = IniRead($settings, "Program_Use_Only", "NetAdmin", "")
Dim $netpass = _StringEncrypt(0, IniRead($settings, "Program_Use_Only", "NetPass", ""), "Prepsys", 2)
Dim $netdom = IniRead($settings, "Program_Use_Only", "NetDom", "")
;-----------------------------------------------------------------------------
;Auto Logon Remove
_Logon ("REMOVE")
;-----------------------------------------------------------------------------
GUICreate("PrepSys Progress", 200, 50, (@DesktopWidth - 200) , (0))

$action_progress = GUICtrlCreateProgress(10, 2, 180, 12)
$current_action = GUICtrlCreateLabel("Current Action", 13, 28, 315, 23)

GUISetState()
While 1
	$msg = GUIGetMsg()
	Select
		Case $msg = $GUI_EVENT_CLOSE
			ExitLoop
	EndSelect
	
	;*** COPY USERNAME OF INITIAL XP USER(IF NOT DEFAULT ADMIN ACCOUNT) ***
	If IniRead($settings, "Script_Settings", "Delete_Start_User", "0") = 1 And IniRead($settings, "Program_Use_Only", "Deleted_Start_User", "0") = 0 Then
		If @UserName <> $logonuser1 Then
			GUICtrlSetData($current_action, "Deleting Initial User")
			IniWrite($settings, "Script_Settings", "Delete_User", @UserName)
			IniWrite($settings, "Program_Use_Only", "Delete_Start_User_Step1", "1")
			_Shutdown ("LOGOFF", "ADD", $logonuser1, $logonpass1, $scriptlocation)
		Else
			IniWrite($settings, "Program_Use_Only", "Deleted_Start_User", "1")
		EndIf
	EndIf
	
	
	;*** DELETE INITIAL USER ***
	If IniRead($settings, "Program_Use_Only", "Delete_Start_User_Step1", "0") = 1 And IniRead($settings, "Program_Use_Only", "Deleted_Start_User", "0") = 0 Then
		GUICtrlSetData($current_action, "Deleting Initial User")
		_Profile ("REMOVE", IniRead($settings, "Script_Settings", "Delete_User", ""))
		_User ("REMOVE", IniRead($settings, "Script_Settings", "Delete_User", ""), "", "")
		IniWrite($settings, "Program_Use_Only", "Deleted_Start_User", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** INSTALL MCAFEE ***
	If IniRead($settings, "Script_Settings", "Install_McAfee", "0") = 1 And IniRead($settings, "Program_Use_Only", "Installed_McAfee", "0") = 0 Then
		GUICtrlSetData($current_action, "Installing McAfee")
		_RunNetSupport ($mcafee_exe, $netuser, $netpass, $netdom)
		_RunNetSupport ($mcafee_patch_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Installed_McAfee", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;Install Sophos Remote Update
	If IniRead($settings, "Script_Settings", "Install_Sophos", "0") = 1 And IniRead($settings, "Program_Use_Only", "Installed_Sophos", "0") = 0 Then
		GUICtrlSetData($current_action, "Installing Sophos")
		_RunNetSupport ($sophos_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Installed_Sophos", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** INSTALL VPN SOFTWARE ***
	If IniRead($settings, "Script_Settings", "Install_VPN", "0") = 1 And IniRead($settings, "Program_Use_Only", "Installed_VPN", "0") = 0 Then
		GUICtrlSetData($current_action, "Installing VPN")
		_RunNetSupport ($vpn_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Installed_VPN", "1")
		_Progress_Ref ("ADD")
		$shutdown = "REBOOT"
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** INSTALL OFFICE ***
	If IniRead($settings, "Script_Settings", "Install_Office", "0") = 1 And IniRead($settings, "Program_Use_Only", "Installed_Office", "0") = 0 Then
		GUICtrlSetData($current_action, "Installing Office")
		_RunNetSupport ($office_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Installed_Office", "1")
		If IniRead($settings, "OFFICE", "Add_Office_Toolbar", "0") = 1 And IniRead($settings, "Program_Use_Only", "Added_Office_Toolbar", "0") = 0 Then
			_TaskToolbar ($toolbardir)
			IniWrite($settings, "Program_Use_Only", "Added_Office_Toolbar", "1")
		EndIf
		_Progress_Ref ("ADD")
		$shutdown = "REBOOT"
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** INSTALL DATATEL ***
	If IniRead($settings, "Script_Settings", "Install_Datatel", "0") = 1 And IniRead($settings, "Program_Use_Only", "Installed_Datatel", "0") = 0 Then
		GUICtrlSetData($current_action, "Installing Datatel")
		_RunNetSupport ($datatel_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Installed_Datatel", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;Install Deepfreeze seed
	If IniRead($settings, "Script_Settings", "Install_DeepFreeze", "0") = 1 And IniRead($settings, "Program_Use_Only", "Installed_DeepFreeze", "0") = 0 Then
		GUICtrlSetData($current_action, "Installing DeepFreeze Seed")
		_RunNetSupport ($deepfreeze_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Installed_DeepFreeze", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	If $shutdown <> "" Then _Shutdown ($shutdown, "ADD", $logonuser1, $logonpass1, $scriptlocation)
	
	;Set Start Menu
	If IniRead($settings, "Script_Settings", "Set_Start_Menu", "0") = 1 And IniRead($settings, "Program_Use_Only", "Set_Start_Menu", "0") = 0 Then
		GUICtrlSetData($current_action, "Setting Start Menu/TaskBar Settings")
		$HKUS1521 = "HKEY_USERS\" & _FindReg ("HKEY_USERS", "S-1-5-21")
		_StartMenu ($settings, $HKUS1521)
		_Taskbar ($settings, $HKUS1521)
		IniWrite($settings, "Program_Use_Only", "Set_Start_Menu", "1")
		_Progress_Ref ("ADD")
		$shutdown = "LOGOFF"
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** SET VISUAL SETTINGS ***
	If IniRead($settings, "Script_Settings", "Set_Visual_Settings", "0") = 1 And IniRead($settings, "Program_Use_Only", "Set_Visual_Settings", "0") = 0 Then
		GUICtrlSetData($current_action, "Setting Visual Settings")
		_VisualSettings ()
		GUICtrlSetData($current_action, "Setting Desktop Settings")
		_Desktop ()
		IniWrite($settings, "Program_Use_Only", "Set_Visual_Settings", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** Set Folder Options ***
	If IniRead($settings, "Script_Settings", "Set_Folder_Options", "0") = 1 And IniRead($settings, "Program_Use_Only", "Set_Folder_Options", "0") = 0 Then
		GUICtrlSetData($current_action, "Setting Folder Options")
		$HKUS1521 = "HKEY_USERS\" & _FindReg ("HKEY_USERS", "S-1-5-21")
		_FolderOptions ($settings, $HKUS1521)
		IniWrite($settings, "Program_Use_Only", "Set_Folder_Options", "1")
		_Progress_Ref ("ADD")
		$shutdown = "LOGOFF"
	EndIf
	
	_Progress_Ref ("SHOW")
	
	If $shutdown <> "" Then _Shutdown ($shutdown, "ADD", $logonuser1, $logonpass1, $scriptlocation)
	
	;*** MAKE SYSPREP USER - RESTART FOR COPY PROFILE ***
	If IniRead($settings, "Script_Settings", "Copy_Profile", "0") = 1 And IniRead($settings, "Program_Use_Only", "Copy_Profile_Step1", "0") = 0 Then
		GUICtrlSetData($current_action, "Creating Copy Profile User")
		_User ("ADD", $logonuser2, $logonpass2, "Administrators")
		IniWrite($settings, "Program_Use_Only", "Copy_Profile_Step1", "1")
		_Shutdown ("LOGOFF", "ADD", $logonuser2, $logonpass2, $scriptlocation)
	EndIf
	
	;*** COPY PROFILE ***
	If IniRead($settings, "Script_Settings", "Copy_Profile", "0") = 1 And IniRead($settings, "Program_Use_Only", "Copy_Profile_Step2", "0") = 0 Then
		GUICtrlSetData($current_action, "Copying Profile")
		_Profile ("COPY", $logonuser1)
		IniWrite($settings, "Program_Use_Only", "Copy_Profile_Step2", "1")
		_Shutdown ("LOGOFF", "ADD", $logonuser1, $logonpass1, $scriptlocation)
	EndIf
	
	;*** Remove sysprep user/profile ***
	If IniRead($settings, "Script_Settings", "Copy_Profile", "0") = 1 And IniRead($settings, "Program_Use_Only", "Copy_Profile_Step3", "0") = 0 Then
		GUICtrlSetData($current_action, "Removing Copy Profile User")
		_Profile ("REMOVE", $logonuser2)
		_User ("REMOVE", $logonuser2, "", "")
		FileDelete(@UserProfileDir & "\" & $logonuser2)
		IniWrite($settings, "Program_Use_Only", "Copy_Profile_Step3", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** WAIT FOR UPDATES/INSTALLS ***
	If IniRead($settings, "Script_Settings", "Wait_Updates", "0") = 1 And IniRead($settings, "Program_Use_Only", "Waited_For_Updates", "0") = 0 Then
		GUICtrlSetData($current_action, "Waiting for Updates")
		_Logon ("ADD", $logonuser1, $logonpass1, $scriptlocation)
		MsgBox(0, "Waiting", "Install updates or extra software - HIT OK WHEN DONE - This window will start on next boot if you need to restart")
		_Logon ("REMOVE")
		IniWrite($settings, "Program_Use_Only", "Waited_For_Updates", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** CLEAN SMS ***
	If IniRead($settings, "Script_Settings", "Clean_SMS", "0") = 1 And IniRead($settings, "Program_Use_Only", "Cleaned_SMS", "0") = 0 Then
		GUICtrlSetData($current_action, "Cleaning SMS")
		_RunNetSupport ($cleansms_exe, $netuser, $netpass, $netdom)
		IniWrite($settings, "Program_Use_Only", "Cleaned_SMS", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** REMOVE McAfee GUID ***
	If IniRead($settings, "Script_Settings", "Remove_McAfee_GUID", "0") = 1 And IniRead($settings, "Program_Use_Only", "Removed_McAfee_GUID", "0") = 0 Then
		GUICtrlSetData($current_action, "Removing McAfee GUID")
		RunWait("net stop McAfeeFramework")
		RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent", "AgentGUID")
		IniWrite($settings, "Program_Use_Only", "Removed_McAfee_GUID", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** DEFRAGMENT ***
	If IniRead($settings, "Script_Settings", "Defrag", "0") = 1 And IniRead($settings, "Program_Use_Only", "Defraged", "0") = 0 Then
		GUICtrlSetData($current_action, "Defraging Local Disk")
		_Defrag ()
		IniWrite($settings, "Program_Use_Only", "Defraged", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	;*** SYSPREP ***
	If IniRead($settings, "Script_Settings", "Run_Sysprep", "0") = 1 And IniRead($settings, "Program_Use_Only", "Ran_Sysprep", "0") = 0 Then
		GUICtrlSetData($current_action, "Running Sysprep")
		If @OSVersion = "WIN_XP" Then
			DirCopy($workingdir & "sysprep\WinXP\sysprep", "c:\sysprep", 1)
			Run("c:\sysprep\sysprep.exe")
		EndIf
		If @OSVersion = "WIN_2000" Then
			DirCopy($workingdir & "sysprep\Win2K\sysprep", "c:\sysprep", 1)
			Run("c:\sysprep\sysprep.exe")
		EndIf
		IniWrite($settings, "Program_Use_Only", "Ran_Sysprep", "1")
		_Progress_Ref ("ADD")
	EndIf
	
	_Progress_Ref ("SHOW")
	
	ExitLoop
WEnd
_DeleteINI ()
_Progress_Ref ("SHOW")
Sleep(3000)
GUIDelete()
;Delete settings.ini if done




Exit




