#include <Array.au3>
#include <ComboConstants.au3>
#include <Crypt.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <String.au3>
#include <WindowsConstants.au3>

OnAutoItExitRegister("beenden")

If Not FileExists(@AppDataDir & "\opsipackagecreator\config.ini") Then
	#region ### START Koda GUI section ### Form=
	$Form1 = GUICreate("opsi-package-creator / First start Wizard", 385, 340, 192, 124)
	GUISetFont(11, 400, 0, "Arial")

	$LabelServer = GUICtrlCreateLabel("Server", 16, 20, 79, 22)
	GUICtrlSetFont(-1, 11, 800, 4, "Arial")

	$LabelServername = GUICtrlCreateLabel("Servername:", 16, 50, 87, 21)
	$InputServername = GUICtrlCreateInput("", 136, 50, 233, 25)

	$LabelUser = GUICtrlCreateLabel("User:", 16, 80, 87, 21)
	$InputUser = GUICtrlCreateInput("", 136, 80, 233, 25)

	$LabelPassword = GUICtrlCreateLabel("Password:", 16, 110, 87, 21)
	$InputPassword = GUICtrlCreateInput("", 136, 110, 233, 25, $ES_PASSWORD)


	$LabelMaintainer = GUICtrlCreateLabel("Maintainer", 16, 150, 79, 22)
	GUICtrlSetFont(-1, 11, 800, 4, "Arial")

	$LabelMaintainerName = GUICtrlCreateLabel("Name:", 16, 180, 47, 21)
	$InputMaintainerName = GUICtrlCreateInput("", 136, 180, 233, 25)

	$LabelMaintainerEmail = GUICtrlCreateLabel("Email:", 16, 210, 45, 21)
	$InputMaintainerEmail = GUICtrlCreateInput("", 136, 210, 233, 25)

	$LabelDeveloperPath = GUICtrlCreateLabel("Developer-Folder:", 16, 240, 124, 21)
	$InputDeveloperPath = GUICtrlCreateInput("", 136, 240, 233, 25)
	$LabellimitDeveloperPath = GUICtrlCreateLabel("(must exist in opsi-workbench, all files in this path are moved to CLIENT_DATA)", 16, 270, 321, 30)
	GUICtrlSetFont(-1, 8, 800, 0, "Arial")


	$ButtonSave = GUICtrlCreateButton("Save", 16, 300, 75, 25)
	GUISetState(@SW_SHOW)
	#endregion ### END Koda GUI section ###

	While 1
		$nMsg = GUIGetMsg()
		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit
			Case $ButtonSave
				$opsiserver = GUICtrlRead($InputServername)
				$opsiuser = GUICtrlRead($InputUser)
				$opsipass = _Crypt_EncryptData(GUICtrlRead($InputPassword), @UserName, $CALG_AES_256)

				$maintainername = GUICtrlRead($InputMaintainerName)
				$maintaineremail = GUICtrlRead($InputMaintainerEmail)
				$developerpath = GUICtrlRead($InputDeveloperPath)

				If $opsiserver = "" Or $opsiuser = "" Or $opsipass = "" Or $maintainername = "" Or $maintaineremail = "" Or $developerpath = "" Then
					MsgBox(16, "opsi-package-creator", "Please fill in all fields")
				Else
					DirCreate(@AppDataDir & "\opsipackagecreator")
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "server", "name", $opsiserver)
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "server", "user", $opsiuser)
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "server", "pass", $opsipass)

					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "name", $maintainername)
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "email", $maintaineremail)
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "developerpath", $developerpath)

					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "create", "folder", "1")
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "create", "move", "1")
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "create", "create", "1")
					IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "create", "install", "1")

					GUISetState(@SW_HIDE)
					ExitLoop
				EndIf
		EndSwitch
	WEnd

Else
	$opsiserver = IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "server", "name", "")
	$opsiuser = IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "server", "user", "")
	$opsipass = BinaryToString(_Crypt_DecryptData(IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "server", "pass", ""), @UserName, $CALG_AES_256))

	$maintainername = IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "name", "")
	$maintaineremail = IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "email", "")
	$developerpath = IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "developerpath", "")
EndIf


$workbenchdrive = DriveMapAdd("*", "\\" & $opsiserver & "\opsi_workbench", 0, $opsiuser, $opsipass)

#region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("opsi-package-creator", 623, 585, 192, 124, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
GUISetFont(11, 400, 0, "Arial")
$Tab1 = GUICtrlCreateTab(0, 0, 626, 585)
$TabSheetPackage = GUICtrlCreateTabItem("Package")

$LabelProductname = GUICtrlCreateLabel("Product name:", 24, 48, 100, 21)
$InputProductname = GUICtrlCreateInput("", 152, 48, 300, 22)

$LabelProductid = GUICtrlCreateLabel("Product id:", 24, 78, 74, 21)
$InputProductid = GUICtrlCreateInput("", 152, 78, 300, 22)
$LabellimitProductid = GUICtrlCreateLabel("only alphanumeric and -", 456, 78, 130, 21)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$LabelDescription = GUICtrlCreateLabel("Description:", 24, 108, 82, 21)
$InputDescription = GUICtrlCreateInput("", 152, 108, 300, 22)

$LabelAdvice = GUICtrlCreateLabel("Advice:", 24, 138, 51, 21)
$InputAdvice = GUICtrlCreateInput("", 152, 138, 300, 22)

$LabelProductversion = GUICtrlCreateLabel("Product version:", 24, 168, 110, 21)
$InputProductversion = GUICtrlCreateInput("1.0", 152, 168, 300, 22)
$LabellimitProductversion = GUICtrlCreateLabel("max. 32 chars", 456, 168, 97, 21)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$LabelPackageversion = GUICtrlCreateLabel("Package version:", 24, 198, 116, 21)
$InputPackageversion = GUICtrlCreateInput("1", 152, 198, 300, 22)
$LabellimitPackageversion = GUICtrlCreateLabel("max. 32 chars", 456, 198, 97, 21)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$LabelLicenserequired = GUICtrlCreateLabel("License required:", 24, 228, 116, 21)
$ComboLicenserequired = GUICtrlCreateCombo("False", 152, 228, 97, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "True")

$LabelPrirority = GUICtrlCreateLabel("Prirority:", 24, 258, 58, 21)
$InputPrirority = GUICtrlCreateInput("0", 152, 258, 50, 22)
$LabellimitPrirority = GUICtrlCreateLabel("from 100 to -100", 212, 258, 111, 21)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$LableScripts = GUICtrlCreateLabel("Scripts (you can drag an drop the files)", 24, 310, 280, 22)
GUICtrlSetFont(-1, 11, 800, 0, "Arial")
$ButtonOpenDeveloperFolder = GUICtrlCreateButton("Open Developer-Folder", 304, 310, 130, 25)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$LabelSetup = GUICtrlCreateLabel("Setup:", 24, 338, 46, 21)
$InputSetup = GUICtrlCreateInput("", 152, 336, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$LabelUninstall = GUICtrlCreateLabel("Uninstall:", 24, 368, 63, 21)
$InputUninstall = GUICtrlCreateInput("", 152, 364, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$LabelUpdate = GUICtrlCreateLabel("Update:", 24, 398, 54, 21)
$InputUpdate = GUICtrlCreateInput("", 152, 393, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$LabelAlways = GUICtrlCreateLabel("Always:", 24, 428, 54, 21)
$InputAlways = GUICtrlCreateInput("", 152, 426, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$LabelOnce = GUICtrlCreateLabel("Once:", 24, 458, 44, 21)
$InputOnce = GUICtrlCreateInput("", 152, 453, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$LabelCustom = GUICtrlCreateLabel("Custom:", 24, 488, 60, 21)
$InputCustom = GUICtrlCreateInput("", 152, 483, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$LabelUserlogin = GUICtrlCreateLabel("User login:", 24, 518, 73, 21)
$InputUserlogin = GUICtrlCreateInput("", 152, 514, 150, 22)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$TabSheetDependency = GUICtrlCreateTabItem("Dependency")

$ListDependencyListView = GUICtrlCreateListView("action|product id|required action|installation status|type", 24, 48, 580, 280, $LVS_NOSORTHEADER)
_GUICtrlListView_SetColumnWidth($ListDependencyListView, 2, $LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_SetColumnWidth($ListDependencyListView, 3, $LVSCW_AUTOSIZE_USEHEADER)

$ButtonDependencyDelete = GUICtrlCreateButton("Delete", 24, 330, 75, 25)

$LabelDependencyAction = GUICtrlCreateLabel("Dependency for action:", 24, 370, 154, 21)
$ComboDependencyAction = GUICtrlCreateCombo("setup", 208, 370, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "uninstall|update")

$LabelDependencyProductid = GUICtrlCreateLabel("Rquired product id:", 24, 400, 127, 21)
$InputDependencyProductid = GUICtrlCreateInput("", 360, 400, 121, 25)
$ComboDependencyProductid = GUICtrlCreateCombo("", 208, 400, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))

$LabelDependencyRequiredaction = GUICtrlCreateLabel("Rquired action:", 24, 430, 102, 21)
$ComboDependencyRequiredaction = GUICtrlCreateCombo("none", 208, 430, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "setup|uninstall|update")

$LabelDependencyRequiredstatus = GUICtrlCreateLabel("Required installation status:", 24, 460, 187, 21)
$ComboDependencyRequiredstatus = GUICtrlCreateCombo("none", 208, 460, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "installed")

$LabelDependencyRequirementtype = GUICtrlCreateLabel("Requirement type:", 24, 490, 123, 21)
$ComboDependencyRequirementtype = GUICtrlCreateCombo("before", 208, 490, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "after")

$ButtonDependencyAdd = GUICtrlCreateButton("Add", 24, 520, 75, 25)

$TabSheetProperty = GUICtrlCreateTabItem("Property")

$ListPropertyListView = GUICtrlCreateListView("name|type|multivalue|editable|description|values|default", 24, 48, 580, 280, $LVS_NOSORTHEADER)

$LabelPropertyName = GUICtrlCreateLabel("Name:", 24, 340, 154, 21)

$LabelPropertyType = GUICtrlCreateLabel("Type: ", 24, 370, 127, 21)
$ComboPropertyType = GUICtrlCreateCombo("unicode", 208, 370, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "bool")

$LabelPropertyMultivalue = GUICtrlCreateLabel("Mutivalue: ", 24, 400, 127, 21)
$ComboPropertyMultivalue = GUICtrlCreateCombo("False", 208, 400, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "True")

$LabelPropertyEditable = GUICtrlCreateLabel("Editable:", 24, 430, 102, 21)
$ComboPropertyEditable = GUICtrlCreateCombo("False", 208, 430, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "True")

$LabelPropertyDescription = GUICtrlCreateLabel("Description:", 24, 460, 187, 21)
$InputPropertyDescription = GUICtrlCreateInput("", 208, 460, 145, 25)

$LabelPropertyValues = GUICtrlCreateLabel("Values:", 24, 490, 187, 21)
$InputPropertyValues = GUICtrlCreateInput("", 208, 490, 145, 25)
$LabellimitPropertyValues = GUICtrlCreateLabel("separate them with comma", 355, 490, 170, 21)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$LabelPropertyDefault = GUICtrlCreateLabel("Default:", 24, 520, 187, 21)
$InputPropertyDefault = GUICtrlCreateInput("", 208, 520, 145, 25)
$ComboPropertyDefault = GUICtrlCreateCombo("False", 355, 520, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlSetData(-1, "True")

$ButtonPropertyUpdate = GUICtrlCreateButton("Update", 24, 550, 75, 25)



$TabSheetProperty = GUICtrlCreateTabItem("Create")

$checkCreatePackagefolder = GUICtrlCreateCheckbox("Create Packagefolder (CLIENT_DATA, control...)", 24, 48, 700, 20)
$checkCreateMovaData = GUICtrlCreateCheckbox("Move Data from Developerfolder to CLIENT_DATA", 24, 78, 700, 20)
$checkCreateMakePackage = GUICtrlCreateCheckbox("Create OPSI-Package", 24, 108, 700, 20)
$checkCreateInstall = GUICtrlCreateCheckbox("Install Package", 24, 138, 700, 20)

If IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "create", "folder", "") = 1 Then
	GUICtrlSetState($checkCreatePackagefolder, $GUI_CHECKED)
EndIf

If IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "create", "move", "") = 1 Then
	GUICtrlSetState($checkCreateMovaData, $GUI_CHECKED)
EndIf

If IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "create", "create", "") = 1 Then
	GUICtrlSetState($checkCreateMakePackage, $GUI_CHECKED)
EndIf

If IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "create", "install", "") = 1 Then
	GUICtrlSetState($checkCreateInstall, $GUI_CHECKED)
EndIf

$ButtonCreateCreate = GUICtrlCreateButton("Create", 24, 550, 75, 25)


$TabSheetProperty = GUICtrlCreateTabItem("Settings")

$LabelSettingServer = GUICtrlCreateLabel("Server", 16, 40, 79, 22)
GUICtrlSetFont(-1, 11, 800, 4, "Arial")

$LabelSettingServername = GUICtrlCreateLabel("Servername:", 16, 70, 87, 21)
$InputSettingServername = GUICtrlCreateInput($opsiserver, 136, 70, 233, 25)

$LabelSettingUser = GUICtrlCreateLabel("User:", 16, 100, 87, 21)
$InputSettingUser = GUICtrlCreateInput($opsiuser, 136, 100, 233, 25)

$LabelSettingPassword = GUICtrlCreateLabel("Password:", 16, 130, 87, 21)
$InputSettingPassword = GUICtrlCreateInput(BinaryToString(_Crypt_DecryptData(IniRead(@AppDataDir & "\opsipackagecreator\config.ini", "server", "pass", ""), @UserName, $CALG_AES_256)), 136, 130, 233, 25, $ES_PASSWORD)


$LabelSettingMaintainer = GUICtrlCreateLabel("Maintainer", 16, 170, 79, 22)
GUICtrlSetFont(-1, 11, 800, 4, "Arial")

$LabelSettingMaintainerName = GUICtrlCreateLabel("Name:", 16, 200, 47, 21)
$InputSettingMaintainerName = GUICtrlCreateInput($maintainername, 136, 200, 233, 25)

$LabelSettingMaintainerEmail = GUICtrlCreateLabel("Email:", 16, 230, 45, 21)
$InputSettingMaintainerEmail = GUICtrlCreateInput($maintaineremail, 136, 230, 233, 25)

$LabelSettingDeveloperPath = GUICtrlCreateLabel("Developer-Folder:", 16, 260, 124, 21)
$InputSettingDeveloperPath = GUICtrlCreateInput($developerpath, 136, 260, 233, 25)
$LabelSettinglimitDeveloperPath = GUICtrlCreateLabel("(must exist in opsi-workbench, all files in this path are moved to CLIENT_DATA)", 16, 290, 321, 30)
GUICtrlSetFont(-1, 8, 800, 0, "Arial")

$ButtonSettingSave = GUICtrlCreateButton("Save", 16, 330, 75, 25)

$LabelImprite = GUICtrlCreateLabel("created by chip (autoit.de poweruser)", 16, 500, 300, 21)

GUICtrlCreateTabItem("")


GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

$ListPropertyListViewselectalt = -1

PropertyScann()
ComboDependencyProductidfill()

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $InputPrirority
			$checkPrirority = GUICtrlRead($InputPrirority)
			If $checkPrirority > 100 Then
				GUICtrlSetData($InputPrirority, 100)
			EndIf

			If $checkPrirority < -100 Then
				GUICtrlSetData($InputPrirority, -100)
			EndIf
		Case $InputProductname
			$checkProductname = GUICtrlRead($InputProductname)
			$checkProductname = StringLower($checkProductname)
			$checkProductname = StringRegExpReplace($checkProductname, "[^a-zA-Z0-9\-]", "")
			GUICtrlSetData($InputProductid, $checkProductname)
		Case $InputProductid
			$checkProductid = GUICtrlRead($InputProductid)
			$checkProductid = StringLower($checkProductid)
			$checkProductid = StringRegExpReplace($checkProductid, "[^a-zA-Z0-9\-]", "")
			GUICtrlSetData($InputProductid, $checkProductid)
		Case $GUI_EVENT_DROPPED
			$checkSetup = GUICtrlRead(@GUI_DropId)
			$checkSetup = _StringReverse($checkSetup)
			$checkSetup = _StringBetween($checkSetup, "sni.", "\")
			$checkSetup = _StringReverse($checkSetup[0])
			GUICtrlSetData(@GUI_DropId, $checkSetup & ".ins")
		Case $ButtonOpenDeveloperFolder
			ShellExecute("\\" & $opsiserver & "\opsi_workbench\" & $developerpath)
		Case $ButtonCreateCreate
			Create()
		Case $ComboDependencyRequiredaction
			If GUICtrlRead($ComboDependencyRequiredaction) = "none" Then
				GUICtrlSetState($ComboDependencyRequiredstatus, $GUI_ENABLE)
			Else
				GUICtrlSetState($ComboDependencyRequiredstatus, $GUI_DISABLE)
			EndIf
		Case $ComboDependencyRequiredstatus
			If GUICtrlRead($ComboDependencyRequiredstatus) = "none" Then
				GUICtrlSetState($ComboDependencyRequiredaction, $GUI_ENABLE)
			Else
				GUICtrlSetState($ComboDependencyRequiredaction, $GUI_DISABLE)
			EndIf
		Case $ButtonDependencyAdd
			ListDependencyListAdd()
		Case $ButtonDependencyDelete
			_GUICtrlListView_DeleteItem($ListDependencyListView, _GUICtrlListView_GetSelectionMark($ListDependencyListView))
		Case $ButtonPropertyUpdate
			PropertyUpdate()
		Case $ComboPropertyType
			PropertyStateType(GUICtrlRead($ComboPropertyType))
		Case $ButtonSettingSave
			$opsiserver = GUICtrlRead($InputSettingServername)
			$opsiuser = GUICtrlRead($InputSettingUser)
			$opsipass = _Crypt_EncryptData(GUICtrlRead($InputSettingPassword), @UserName, $CALG_AES_256)

			$maintainername = GUICtrlRead($InputSettingMaintainerName)
			$maintaineremail = GUICtrlRead($InputSettingMaintainerEmail)
			$developerpath = GUICtrlRead($InputSettingDeveloperPath)

			IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "server", "name", $opsiserver)
			IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "server", "user", $opsiuser)
			IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "server", "pass", $opsipass)

			IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "name", $maintainername)
			IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "email", $maintaineremail)
			IniWrite(@AppDataDir & "\opsipackagecreator\config.ini", "maintainer", "developerpath", $developerpath)
	EndSwitch

	If _GUICtrlListView_GetSelectionMark($ListPropertyListView) > -1 Then
		If _GUICtrlListView_GetSelectionMark($ListPropertyListView) <> $ListPropertyListViewselectalt Then
			$ListPropertyListViewselectalt = _GUICtrlListView_GetSelectionMark($ListPropertyListView)
			Propertyfill($ListPropertyListViewselectalt)
		EndIf
	EndIf
WEnd


Func Create()
	$ListPropertyListViewallset = 1

	$ListPropertyListViewCount = _GUICtrlListView_GetItemCount($ListPropertyListView)
	For $i = 0 To $ListPropertyListViewCount - 1 Step 1
		$ListPropertyListViewItems = _GUICtrlListView_GetItemTextArray($ListPropertyListView, $i)
		If $ListPropertyListViewItems[2] = "" Then
			$ListPropertyListViewallset = 0
			ExitLoop
		EndIf
	Next

	If GUICtrlRead($InputProductname) = "" Or GUICtrlRead($InputProductid) = "" Or GUICtrlRead($InputProductversion) = "" Or GUICtrlRead($InputPackageversion) = "" Or GUICtrlRead($InputPrirority) = "" Or GUICtrlRead($InputSetup) = "" Then
		MsgBox(16, "opsi-package-creator", "Not all required fields are filled.")
	ElseIf $ListPropertyListViewallset = 0 Then
		MsgBox(16, "opsi-package-creator", "Not all Property are set.")
	Else
		If GUICtrlRead($checkCreatePackagefolder) = $GUI_CHECKED Then
			createWorkbench()
		EndIf

		PuttyLogin()

		If GUICtrlRead($checkCreateMovaData) = $GUI_CHECKED Then
			Puttymovedeveloperdata()
		EndIf

		If GUICtrlRead($checkCreateMakePackage) = $GUI_CHECKED Then
			Puttymakeproductfile()
		EndIf

		If GUICtrlRead($checkCreateInstall) = $GUI_CHECKED Then
			Puttypackagemanagerinstall()
		EndIf

		PuttyLogout()
	EndIf
EndFunc   ;==>Create

Func PropertyStateType($typ)
	If $typ = "bool" Then
		GUICtrlSetState($ComboPropertyMultivalue, $GUI_DISABLE)
		GUICtrlSetState($ComboPropertyEditable, $GUI_DISABLE)
		GUICtrlSetState($InputPropertyValues, $GUI_DISABLE)
		GUICtrlSetState($InputPropertyDefault, $GUI_DISABLE)

		GUICtrlSetState($ComboPropertyDefault, $GUI_ENABLE)
	Else
		GUICtrlSetState($ComboPropertyMultivalue, $GUI_ENABLE)
		GUICtrlSetState($ComboPropertyEditable, $GUI_ENABLE)
		GUICtrlSetState($InputPropertyValues, $GUI_ENABLE)
		GUICtrlSetState($InputPropertyDefault, $GUI_ENABLE)

		GUICtrlSetState($ComboPropertyDefault, $GUI_DISABLE)
	EndIf
EndFunc   ;==>PropertyStateType


Func PropertyUpdate()
	_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($ComboPropertyType), $ListPropertyListViewselectalt, 1)
	_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($ComboPropertyMultivalue), $ListPropertyListViewselectalt, 2)
	_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($ComboPropertyEditable), $ListPropertyListViewselectalt, 3)
	_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($InputPropertyDescription), $ListPropertyListViewselectalt, 4)
	_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($InputPropertyValues), $ListPropertyListViewselectalt, 5)

	If GUICtrlRead($ComboPropertyType) = "bool" Then
		_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($ComboPropertyDefault), $ListPropertyListViewselectalt, 6)
	Else
		_GUICtrlListView_SetItem($ListPropertyListView, GUICtrlRead($InputPropertyDefault), $ListPropertyListViewselectalt, 6)
	EndIf

	_GUICtrlListView_SetColumnWidth($ListPropertyListView, 1, $LVSCW_AUTOSIZE)
	_GUICtrlListView_SetColumnWidth($ListPropertyListView, 4, $LVSCW_AUTOSIZE)
	_GUICtrlListView_SetColumnWidth($ListPropertyListView, 5, $LVSCW_AUTOSIZE)
EndFunc   ;==>PropertyUpdate

Func Propertyfill($id)
	$ListPropertyListViewItems = _GUICtrlListView_GetItemTextArray($ListPropertyListView, $id)

	GUICtrlSetData($LabelPropertyName, "Name: " & $ListPropertyListViewItems[1])

	If $ListPropertyListViewItems[2] <> "" Then
		GUICtrlSetData($ComboPropertyType, $ListPropertyListViewItems[2])
		PropertyStateType($ListPropertyListViewItems[2])
	EndIf

	If $ListPropertyListViewItems[3] <> "" Then
		GUICtrlSetData($ComboPropertyMultivalue, $ListPropertyListViewItems[3])
	EndIf

	If $ListPropertyListViewItems[4] <> "" Then
		GUICtrlSetData($ComboPropertyEditable, $ListPropertyListViewItems[4])
	EndIf

	GUICtrlSetData($InputPropertyDescription, $ListPropertyListViewItems[5])
	GUICtrlSetData($InputPropertyValues, $ListPropertyListViewItems[6])
	GUICtrlSetData($InputPropertyDefault, $ListPropertyListViewItems[7])
EndFunc   ;==>Propertyfill

Func PropertyScann()
	$PropertyScannFiles = FileFindFirstFile("\\" & $opsiserver & "\opsi_workbench\" & $developerpath & "\*.ins")
	While 1
		$PropertyScannFile = FileFindNextFile($PropertyScannFiles)
		If @error Then ExitLoop

		$PropertyScannFileOpen = FileOpen("\\" & $opsiserver & "\opsi_workbench\" & $developerpath & "\" & $PropertyScannFile, 0)
		$PropertyScannFileInhalt = FileRead($PropertyScannFileOpen)
		FileClose($PropertyScannFileOpen)

		$PropertyScannFileArray = _StringBetween($PropertyScannFileInhalt, "GetProductProperty", ")")

		If IsArray($PropertyScannFileArray) Then
			For $Propertyelement In $PropertyScannFileArray
				$Propertyelementsplit = StringSplit($Propertyelement, '"')

				$ListPropertySearch = _GUICtrlListView_FindText($ListPropertyListView, $Propertyelementsplit[2])

				If $ListPropertySearch < 0 Then
					GUICtrlCreateListViewItem($Propertyelementsplit[2] & "||||||" & $Propertyelementsplit[4], $ListPropertyListView)
					_GUICtrlListView_SetColumnWidth($ListPropertyListView, 0, $LVSCW_AUTOSIZE)
					_GUICtrlListView_SetColumnWidth($ListPropertyListView, 6, $LVSCW_AUTOSIZE)
				EndIf
			Next
		EndIf
	WEnd
EndFunc   ;==>PropertyScann

Func ListDependencyListAdd()
	If GUICtrlRead($InputDependencyProductid) = "" And GUICtrlRead($ComboDependencyProductid) = "" Then
		MsgBox(16, "opsi-package-creator", "Product id required")
	ElseIf GUICtrlRead($ComboDependencyRequiredaction) = "none" And GUICtrlRead($ComboDependencyRequiredstatus) = "none" Then
		MsgBox(16, "opsi-package-creator", 'Set "Required action" or "Required installations status')
	Else
		If GUICtrlRead($InputDependencyProductid) = "" Then
			$InputDependencyProductid = GUICtrlRead($ComboDependencyProductid)
		Else
			$InputDependencyProductid = GUICtrlRead($InputDependencyProductid)
		EndIf

		GUICtrlCreateListViewItem(GUICtrlRead($ComboDependencyAction) & "|" & $InputDependencyProductid & "|" & GUICtrlRead($ComboDependencyRequiredaction) & "|" & GUICtrlRead($ComboDependencyRequiredstatus) & "|" & GUICtrlRead($ComboDependencyRequirementtype), $ListDependencyListView)
	EndIf
EndFunc   ;==>ListDependencyListAdd

Func ComboDependencyProductidfill()
	$ProductIDs = _FileListToArray("\\" & $opsiserver & "\opt_pcbin\install", "*", 2)
	_ArrayDelete($ProductIDs, 0)
	_ArraySort($ProductIDs)
	$ProductIDs = _ArrayToString($ProductIDs)
	GUICtrlSetData($ComboDependencyProductid, $ProductIDs)
EndFunc   ;==>ComboDependencyProductidfill

Func createWorkbench()
	DirCreate("\\" & $opsiserver & "\opsi_workbench\" & GUICtrlRead($InputProductid) & "\CLIENT_DATA")
	DirCreate("\\" & $opsiserver & "\opsi_workbench\" & GUICtrlRead($InputProductid) & "\OPSI")
	DirCreate("\\" & $opsiserver & "\opsi_workbench\" & GUICtrlRead($InputProductid) & "\SERVER_DATA")
	$file = FileOpen("\\" & $opsiserver & "\opsi_workbench\" & GUICtrlRead($InputProductid) & "\OPSI\control", 2)

	FileWriteLine($file, "[Package]")
	FileWriteLine($file, "version: " & GUICtrlRead($InputPackageversion))
	FileWriteLine($file, "depends:")
	FileWriteLine($file, "incremental: False")

	FileWriteLine($file, "")

	FileWriteLine($file, "[Product]")
	FileWriteLine($file, "type: localboot")
	FileWriteLine($file, "id: " & GUICtrlRead($InputProductid))
	FileWriteLine($file, "name: " & GUICtrlRead($InputProductname))
	FileWriteLine($file, "description: " & GUICtrlRead($InputDescription))
	FileWriteLine($file, "advice: " & GUICtrlRead($InputAdvice))
	FileWriteLine($file, "version: " & GUICtrlRead($InputProductversion))
	FileWriteLine($file, "priority: " & GUICtrlRead($InputPrirority))
	FileWriteLine($file, "licenseRequired: " & GUICtrlRead($ComboLicenserequired))
	FileWriteLine($file, "productClasses:")
	FileWriteLine($file, "setupScript: " & GUICtrlRead($InputSetup))
	FileWriteLine($file, "uninstallScript: " & GUICtrlRead($InputUninstall))
	FileWriteLine($file, "updateScript: " & GUICtrlRead($InputUpdate))
	FileWriteLine($file, "alwaysScript: " & GUICtrlRead($InputAlways))
	FileWriteLine($file, "onceScript: " & GUICtrlRead($InputOnce))
	FileWriteLine($file, "customScript: " & GUICtrlRead($InputCustom))
	FileWriteLine($file, "userLoginScript: " & GUICtrlRead($InputUserlogin))

	$ListDependencyListViewCount = _GUICtrlListView_GetItemCount($ListDependencyListView)

	For $i = 0 To $ListDependencyListViewCount - 1 Step 1
		$ListDependencyListViewItems = _GUICtrlListView_GetItemTextArray($ListDependencyListView, $i)

		FileWriteLine($file, "")

		FileWriteLine($file, "[ProductDependency]")
		FileWriteLine($file, "action: " & $ListDependencyListViewItems[1])
		FileWriteLine($file, "requiredProduct: " & $ListDependencyListViewItems[2])

		If $ListDependencyListViewItems[3] <> "none" Then
			FileWriteLine($file, "requiredAction: " & $ListDependencyListViewItems[3])
		Else
			FileWriteLine($file, "requiredStatus: " & $ListDependencyListViewItems[4])
		EndIf

		FileWriteLine($file, "requirementType: " & $ListDependencyListViewItems[5])

	Next

	$ListPropertyListViewCount = _GUICtrlListView_GetItemCount($ListPropertyListView)

	For $i = 0 To $ListPropertyListViewCount - 1 Step 1
		$ListPropertyListViewItems = _GUICtrlListView_GetItemTextArray($ListPropertyListView, $i)

		FileWriteLine($file, "")

		FileWriteLine($file, "[ProductProperty]")
		FileWriteLine($file, "type: " & $ListPropertyListViewItems[2])
		FileWriteLine($file, "name: " & $ListPropertyListViewItems[1])

		If $ListPropertyListViewItems[2] = "unicode" Then
			FileWriteLine($file, "multivalue: " & $ListPropertyListViewItems[3])
			FileWriteLine($file, "editable: " & $ListPropertyListViewItems[4])
		EndIf

		FileWriteLine($file, "description: " & $ListPropertyListViewItems[5])

		If $ListPropertyListViewItems[2] = "unicode" Then

			$ListPropertyListValuesSplit = StringSplit($ListPropertyListViewItems[6], ",", 2)

			$ListPropertyListValuesfinal = '["'
			For $element In $ListPropertyListValuesSplit
				$ListPropertyListValuesfinal = $ListPropertyListValuesfinal & StringStripWS(StringStripWS($element, 1), 2) & '", "'
			Next

			$ListPropertyListValuesfinal = StringTrimRight($ListPropertyListValuesfinal, 3)
			$ListPropertyListValuesfinal = $ListPropertyListValuesfinal & "]"

			FileWriteLine($file, "values: " & $ListPropertyListValuesfinal)

			FileWriteLine($file, 'values: ["' & $ListPropertyListViewItems[7] & '"]')
		Else
			FileWriteLine($file, 'values: ' & $ListPropertyListViewItems[7])
		EndIf
	Next



	FileWriteLine($file, "")

	FileWriteLine($file, "[Changelog]")
	FileWriteLine($file, GUICtrlRead($InputProductid) & "(" & GUICtrlRead($InputProductversion) & "-" & GUICtrlRead($InputProductversion) & ") testing; urgency=low")

	FileWriteLine($file, "")

	FileWriteLine($file, "  * Initial package")

	FileWriteLine($file, "")

	Switch @MON
		Case "01"
			$mon = "Jan"
		Case "02"
			$mon = "Feb"
		Case "03"
			$mon = "Mar"
		Case "04"
			$mon = "Apr"
		Case "05"
			$mon = "May"
		Case "06"
			$mon = "June"
		Case "07"
			$mon = "July"
		Case "08"
			$mon = "Aug"
		Case "09"
			$mon = "Sept"
		Case "10"
			$mon = "Oct"
		Case "11"
			$mon = "Nov"
		Case "12"
			$mon = "Dec"
	EndSwitch

	Switch @WDAY
		Case "1"
			$tag = "Sun"
		Case "2"
			$tag = "Mon"
		Case "3"
			$tag = "Tue"
		Case "4"
			$tag = "Wed"
		Case "5"
			$tag = "Thu"
		Case "6"
			$tag = "Fri"
		Case "7"
			$tag = "Sat"
	EndSwitch

	FileWriteLine($file, " -- " & $maintainername & " <" & $maintaineremail & "> " & $tag & ", " & @MDAY & " " & $mon & " " & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " + 0000")

	FileClose($file)
EndFunc   ;==>createWorkbench

Func PuttyLogin()
	PuttystartSsh()

	Sleep(1000)

	While Not PuttyparseLogFileSingleKeyword("login as:")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", $opsiuser & @CR)

	While Not PuttyparseLogFileSingleKeyword("password:")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", $opsipass & @CR)
EndFunc   ;==>PuttyLogin


Func PuttyLogout()
	While Not PuttyparseLogFileSingleKeyword("@g-opsi")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", "exit" & @CR)
EndFunc   ;==>PuttyLogout

Func Puttymovedeveloperdata()
	While Not PuttyparseLogFileSingleKeyword("g-opsi:~$")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", "mv {ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}home{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}opsiproducts{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}" & $developerpath & "{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}{ALTDOWN}{NUMPAD4}{NUMPAD2}{ALTUP} {ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}home{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}opsiproducts{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}" & GUICtrlRead($InputProductid) & "{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}{ALTDOWN}{NUMPAD6}{NUMPAD7}{ALTUP}{ALTDOWN}{NUMPAD7}{NUMPAD6}{ALTUP}{ALTDOWN}{NUMPAD7}{NUMPAD3}{ALTUP}{ALTDOWN}{NUMPAD6}{NUMPAD9}{ALTUP}{ALTDOWN}{NUMPAD7}{NUMPAD8}{ALTUP}{ALTDOWN}{NUMPAD8}{NUMPAD4}{ALTUP}{ALTDOWN}{NUMPAD9}{NUMPAD5}{ALTUP}{ALTDOWN}{NUMPAD6}{NUMPAD8}{ALTUP}{ALTDOWN}{NUMPAD6}{NUMPAD5}{ALTUP}{ALTDOWN}{NUMPAD8}{NUMPAD4}{ALTUP}{ALTDOWN}{NUMPAD6}{NUMPAD5}{ALTUP}" & @CR)
EndFunc   ;==>Puttymovedeveloperdata

Func Puttymakeproductfile()
	While Not PuttyparseLogFileSingleKeyword("g-opsi:~$")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", "cd {ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}home{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}opsiproducts{ALTDOWN}{NUMPAD0}{NUMPAD4}{NUMPAD7}{ALTUP}" & GUICtrlRead($InputProductid) & @CR)

	While Not PuttyparseLogFileSingleKeyword(GUICtrlRead($InputProductid) & "$")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", "opsi-makeproductfile" & @CR)
EndFunc   ;==>Puttymakeproductfile


Func Puttypackagemanagerinstall()
	While Not PuttyparseLogFileSingleKeyword(GUICtrlRead($InputProductid) & "$")
		Sleep(100)
	WEnd

	ControlSend("[CLASS:PuTTY]", "", "", "opsi-package-manager -i " & GUICtrlRead($InputProductid) & "{ALTDOWN}{NUMPAD9}{NUMPAD5}{ALTUP}" & GUICtrlRead($InputProductversion) & "-" & GUICtrlRead($InputPackageversion) & ".opsi" & @CR)

EndFunc   ;==>Puttypackagemanagerinstall

Func PuttystartSsh()
	Run(@ScriptDir & "\putty.exe", "", @SW_SHOW)
	WinWait("PuTTY Configuration")

	ControlSetText("PuTTY Configuration", "Host &Name (or IP address)", "Edit1", $opsiserver)


	ControlSend("PuTTY Configuration", "", "", "!s")
	ControlSend("PuTTY Configuration", "", "", "+{TAB}")
	ControlSend("PuTTY Configuration", "", "", "+{TAB}")
	ControlSend("PuTTY Configuration", "", "", "+{TAB}")
	Sleep(250)
	ControlSend("PuTTY Configuration", "", "", "{DOWN}")


	ControlSend("PuTTY Configuration", "Log &file name:", "Edit1", "{DEL 10}")
	ControlSetText("PuTTY Configuration", "Log &file name:", "Edit1", @ScriptDir & "\Puttytemp.log")
	ControlSend("PuTTY Configuration", "", "", "!p")
	ControlSend("PuTTY Configuration", "", "", "!e")
	ControlSend("PuTTY Configuration", "", "", "{UP}")
	ControlSend("PuTTY Configuration", "", "", "{UP}")
	ControlSend("PuTTY Configuration", "", "", "{UP}")

	;delete existing logfile to prevent handling leftover logfile from previous sessions masking problems
	If FileExists(@ScriptDir & "\Putty.log") Then
		FileDelete(@ScriptDir & "\Putty.log")
		FileDelete(@ScriptDir & "\Puttytemp.log") ;temporary workaround
	EndIf

	ControlClick("PuTTY Configuration", "&Open", "Button1")
	If WinWait("PuTTY Security Alert", "", 5) Then
		ControlClick("PuTTY Security Alert", "", 6)
	EndIf
EndFunc   ;==>PuttystartSsh



Func PuttyparseLogFileSingleKeyword($sKeyword)
	Local $bReturnFlag = False
	FileCopy(@ScriptDir & "\Puttytemp.log", @ScriptDir & "\Putty.log", 1) ;workaround
	Sleep(200)
	;analyze logfile created to parse for single keyword
	$logfile = FileOpen(@ScriptDir & "\Putty.log", 0)
	; Check if file opened for reading OK
	If $logfile = -1 Then
		MsgBox(48, "Error", "Unable to open " & @ScriptDir & "\Putty.log" & " for single keyword parse.")
		Exit
	EndIf
	; Read in lines of text until the EOF is reached
	While 1
		$line = FileReadLine($logfile)
		If @error = -1 Then ExitLoop
		If StringInStr($line, $sKeyword) Then
			;switch is responding
			$bReturnFlag = True
		EndIf
	WEnd;end reading file
	FileClose($logfile)
	Sleep(2000) ;release resources
	Return $bReturnFlag
EndFunc   ;==>PuttyparseLogFileSingleKeyword

Func beenden()

	DriveMapDel($workbenchdrive)
EndFunc   ;==>beenden

