#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 2)

Global $StandarEditorName = 'Microsoft Windows notepad.exe'
Global $arRadio[3], $InCurrEditor, $LabelSuccess
Global $alternateEditorPath = ''
Global $arDest[4] = [ _
	@WindowsDir & '\servicepackfiles\i386\notepad.exe', _
	@WindowsDir & '\system32\dllcache\notepad.exe', _
	@WindowsDir & '\system32\notepad.exe', _
	@WindowsDir & '\notepad.exe']
Global $start = 0, $RoyalBlue = 0x4169E1, $Orange = 0xFFA500
If Not FileExists(@WindowsDir & '\servicepackfiles\i386\notepad.exe') Then $start = 1

$GUIAlternateEditor = GUICreate("Standard Editor wechseln (Win2k, WinXP SP2)                By BugFix  " & _
"( bugfix@autoit.de )", 563, 239, 193, 115)
GUISetOnEvent($GUI_EVENT_CLOSE, "GUIAlternateEditorClose")
GUISetBkColor($RoyalBlue, $GUIAlternateEditor)
$Group1 = GUICtrlCreateGroup("", 10, 10, 540, 209)
$arRadio[0] = GUICtrlCreateRadio("Launcher installieren, Editor festlegen", 150, 71, 206, 17)
$arRadio[1] = GUICtrlCreateRadio("Neuen Editor festlegen", 150, 98, 143, 17)
$arRadio[2] = GUICtrlCreateRadio("Windows Standard wiederherstellen", 150, 126, 196, 17)
$BtGo = GUICtrlCreateButton("Ausführen", 465, 123, 70, 20)
GUICtrlSetOnEvent(-1, '_Go')
$Label1 = GUICtrlCreateLabel("Aktueller Standard Editor:", 20, 40, 124, 17)
$InCurrEditor = GUICtrlCreateInput("", 150, 37, 385, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$LabelSuccess = GUICtrlCreateLabel("Bitte Aktion wählen", 150, 174, 385, 23)
GUICtrlSetFont(-1, 10, 800, 0, "Comic Sans MS")
GUICtrlSetColor(-1, $Orange)
GUICtrlCreateGroup("", -99, -99, 1, 1)

If StringLeft(FileGetVersion(@WindowsDir&'\notepad.exe'), 1) = 3 Then
	GUICtrlSetData($InCurrEditor, RegRead('HKEY_LOCAL_MACHINE\SOFTWARE\AlternateEditor', 'FullPath'))
	GUICtrlSetState($arRadio[1], $GUI_CHECKED)
	GUICtrlSetState($arRadio[0], $GUI_DISABLE)
Else
	GUICtrlSetData($InCurrEditor, $StandarEditorName)
	GUICtrlSetState($arRadio[1], $GUI_DISABLE)
	GUICtrlSetState($arRadio[2], $GUI_DISABLE)
	GUICtrlSetState($arRadio[0], $GUI_CHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1
	Sleep(100)
WEnd

Func GUIAlternateEditorClose()
	Exit
EndFunc

Func _Go()
	GUICtrlSetState($BtGo, $GUI_DISABLE)
	GUICtrlSetData($LabelSuccess, '')
	For $i = 0 To 2
		If BitAND(GUICtrlRead($arRadio[$i]), $GUI_CHECKED) Then ExitLoop
	Next
	Switch $i
		Case 0
			_LauncherInstall()
			Local $begin = TimerInit()
			Do
				If WinExists('Windows-Dateischutz') Then
					Send("{ESC}")
					WinWaitActive('Windows-Dateischutz', 'Sie müssen die Dateien')
					Send("!j")
					$begin = 6000
				EndIf
				Sleep(100)
			Until TimerDiff($begin) >= 6000
			GUICtrlSetData($LabelSuccess, '')
			_AlternateSelect()
			GUICtrlSetState($arRadio[0], $GUI_DISABLE)
			GUICtrlSetState($arRadio[1], $GUI_ENABLE)
			GUICtrlSetState($arRadio[2], $GUI_ENABLE)
			GUICtrlSetState($arRadio[1], $GUI_CHECKED)
		Case 1
			_AlternateSelect()
		Case 2
			_RollBack()
	EndSwitch
	GUICtrlSetState($BtGo, $GUI_ENABLE)
EndFunc ;==>_Go

Func _LauncherInstall()
	RegRead('HKEY_LOCAL_MACHINE\SOFTWARE\AlternateEditor', 'OriginalSaved')
	If @error Then
		If FileCopy(@WindowsDir & '\notepad.exe', @WindowsDir & '\notepad.exe.org') = 0 Then ; Sicherheitskopie Original
			MsgBox(262160, 'Kopierfehler', 'Die Sicherheitskopie der "notepad.exe" konnte nicht angelegt werden.')
			Return
		EndIf
		If RegWrite('HKEY_LOCAL_MACHINE\SOFTWARE\AlternateEditor', 'OriginalSaved', "REG_SZ", 1) = 0 Then
			MsgBox(262160, 'Registryfehler', 'Eintrag in die Registry fehlgeschlagen.')
			Return
		EndIf
	EndIf
	Local $fehler = 0
	For $i = $start To UBound($arDest) -1  ; ####### QUELLPFAD für FileInstall anpassen !!! ########
		If FileInstall("C:\Dokumente und Einstellungen\Standard\Desktop\SCRIPTING\AU3-Scripte\Alternate Notepad\notepad.EXE", _
			$arDest[$i], 1) = 0 Then
			$fehler += 1
		EndIf
	Next
	If $fehler > 0 Then 
		GUICtrlSetData($LabelSuccess, 'Fehler bei Installation. Wiederherstellung starten.')
		Return
	EndIf
	GUICtrlSetData($LabelSuccess, 'Installation Launcher erfolgreich. Starte Editorwahl.')
EndFunc ;==>_LauncherInstall

Func _AlternateSelect()
	GUICtrlSetData($LabelSuccess, '')
	Do
		$alternateEditorPath = FileOpenDialog("Pfad des alternativen Editors auswählen", @HomeDrive, "(*.exe)")
		If $alternateEditorPath = '' Then
			If MsgBox(262180, 'Keine Datei ausgewählt', 'Wollen Sie nochmals wählen?') = 7 Then Return 0
		EndIf
	Until $alternateEditorPath <> ''
	If RegWrite('HKEY_LOCAL_MACHINE\SOFTWARE\AlternateEditor', 'FullPath', "REG_SZ", $alternateEditorPath) = 0 Then
		GUICtrlSetData($LabelSuccess, 'Auswahl/Registrierung Editor fehlgeschlagen.')
	Else
		GUICtrlSetData($LabelSuccess, 'Alle Vorgänge wurden erfolgreich abgeschlossen.')
		GUICtrlSetData($InCurrEditor, $alternateEditorPath)
	EndIf
EndFunc ;==>_AlternateSelect

Func _RollBack()
	If Not FileExists(@WindowsDir & '\notepad.exe.org') Then
		MsgBox(262160, 'Dateifehler', 'Die Sicherung der Originaldatei:' & @LF & _
			@WindowsDir & '\notepad.exe.org' & @LF & _
			'ist nicht vorhanden!' & @LF & _ 
			'Eine Wiederherstellung ohne diese Datei ist nicht möglich!')
		Return
	EndIf
	For $i = $start To UBound($arDest) -1
		If FileCopy(@WindowsDir & '\notepad.exe.org', $arDest[$i], 1) = 0 Then
			MsgBox(262160, 'Kopierfehler', 'Die Original Datei konnte nicht in den Pfad:' & @LF & _
				$arDest[$i] & @LF & _
				'kopiert werden.' & @LF & @LF &  _ 
				'Bitte kopieren Sie die Original "notepad.exe" in GENAU dieser Reihenfolge' & @LF & _
				'in die folgenden Pfade:' & @LF & _
				@WindowsDir & '\servicepackfiles\i386\ (sofern vorhanden)' & @LF & _
				@WindowsDir & '\system32\dllcache\' & @LF & _
				@WindowsDir & '\system32\' & @LF & _
				@WindowsDir & '\')
			GUICtrlSetData($LabelSuccess, 'Fehler bei Wiederherstellung.')
			Return
		EndIf
	Next
	RegDelete('HKEY_LOCAL_MACHINE\SOFTWARE\AlternateEditor', 'FullPath')
	GUICtrlSetData($LabelSuccess, 'Wiederherstellung erfolgreich beendet.')
	GUICtrlSetData($InCurrEditor, $StandarEditorName)
	GUICtrlSetState($arRadio[0], $GUI_ENABLE)
	GUICtrlSetState($arRadio[1], $GUI_DISABLE)
	GUICtrlSetState($arRadio[2], $GUI_DISABLE)
	GUICtrlSetState($arRadio[0], $GUI_CHECKED)
EndFunc ;==>_RollBack