(
	global install_ro
	try(destroydialog install_ro)catch()
	rollout install_ro "Sarfinet" width:280 height:296
	(
		fn slfbosalt yol = 
		(
			fn tasi file =
			(
				local tempfile = sysinfo.tempdir + @"\temp" + ((random 1 9999) as string)
				renameFile file tempfile
			)
			local files = getfiles (yol+"\*")
			for i in files do if (getfilenametype i) == ".pyd" then tasi i
		)
		fn ini_ayarla inifile root localization =
		(
			setinisetting inifile "Directories" "Root" root
			setinisetting inifile "Directories" "Root2" root
			setinisetting inifile "Directories" "Programs" "\programs"
			setinisetting inifile "Directories" "Resources" @"\resources"
			setinisetting inifile "Directories" "Localizations" "\localizations"
			setinisetting inifile "Directories" "Releases" @"\releases"
			setinisetting inifile "Directories" "MaterialLibrary" "\MaterialLibrary"
			local os = python.import "os"
			userfolder = os.path.expanduser("~")+"\Documents\Sarfinet"
			if not doesfileexist userfolder then makedir userfolder
			setinisetting inifile "Directories" "CommonPath" (userfolder+"\products")
			setinisetting inifile "Directories" "LocalPath" (userfolder+"\products")
			setinisetting inifile "Directories" "QuickCabinetCommonPath" @"\resource\QuickCabinet"
			setinisetting inifile "Directories" "QuickCabinetLocalPath" @"\resource\QuickCabinet"
			setinisetting inifile "Settings" "Admin" "0"
			setinisetting inifile "Settings" "AutoStart" "1"
			setinisetting inifile "Settings" "AutoUpdate" "1"
			setinisetting inifile "Settings" "EdgebandCode" "e02"
			setinisetting inifile "Settings" "EdgebandThickness" "1"
			loc_file = case localization of
			(
				default:("\en-US")
				0:("\en-US")
				1:("\zh-CN")
			)
			setinisetting inifile "Settings" "MaterialLibrary" "\SarfinetMaterialLibrary.mat"
			setinisetting inifile "Settings" "Localization" loc_file
			setinisetting inifile "Settings" "Drill" "\SarfinetDrill.xml"
			setinisetting inifile "Settings" "CostCalculation" "\SarfinetCostCalculation.xml"
			setinisetting inifile "Settings" "Packing" "\SarfinetPacking.xml"
		)
		fn startcopy kaynakklasor hedefklasor =
		(
			try(
				dotNet.loadAssembly "Microsoft.VisualBasic" -- tek komutla klasor ve icerigini kopyalamak icin visualbasic komutlari tercih edilmistir 
				(dotNetClass "Microsoft.VisualBasic.FileIO.FileSystem").CopyDirectory kaynakklasor hedefklasor true
				true
			)
			catch(
				false
			)
		)
		fn wait seconds = (
			local msec = seconds * 1000
			local thread = dotnetclass "System.Threading.Thread"
			thread.Sleep msec
		)
		fn temp_temizle = 
		(
			try(
				(dotNetClass "System.IO.Directory").Delete ((getdir #temp)+"\Sarfinet") true -- 'true' parametresi içindekileri de siler.
-- 				dotNet.loadAssembly "Microsoft.VisualBasic"
-- 				(dotNetClass "Microsoft.VisualBasic.FileIO.FileSystem").DeleteDirectory ((getdir #temp)+"\Sarfinet") (dotnetclass "Microsoft.VisualBasic.FileIO.DeleteDirectoryOption").DeleteAllContents
			)
			catch(print (getcurrentexception()))
		)
		fn create_material_library matlibdosya =
		(
-- 			local arama_klasoru1 = if klasor != undefined then klasor else getinisetting sarfinet.inifile "Directories" "MaterialLibrary"
-- 			local arama_klasoru2 = sarfinet.getdir.resources+"\MaterialLibrary"
-- 			local matlibklasor = if doesfileexist arama_klasoru1 then arama_klasoru1 else arama_klasoru2
-- 			local dosya1 = (getinisetting sarfinet.inifile "Settings" "MaterialLibrary") -- tam adres ise
-- 			local dosya2 = arama_klasoru2+(getinisetting sarfinet.inifile "Settings" "MaterialLibrary")
-- 			local matlibdosya = if doesfileexist dosya1 then dosya1 else if doesfileexist dosya2 then dosya2 else undefined
			local matlibklasor = getFilenamePath matlibdosya
			local matlib = materiallibrary()
			local dosyalar = getfiles (matlibklasor+"\*")
			local formatlar = (getpropnames (dotnetclass "System.Drawing.Imaging.ImageFormat"))+#("jpg")
			local gecerliformatlar = for i in formatlar collect ("."+(tolower (i as string)))
			local dosyaisimleri = for i in dosyalar 
			where finditem gecerliformatlar (tolower (getfilenametype i)) collect i
			local dosyaisimleri_kucuk = for i in dosyaisimleri collect (tolower (filenamefrompath i))
			for i = 1 to dosyaisimleri.count do
			(
				bmtex = bitmaptexture filename:dosyaisimleri[i]
				pmat1 = physicalmaterial name:(getFilenameFile dosyaisimleri[i]) basecolor:white base_color_map:bmtex
				pmat2 = physicalmaterial name:"raw" basecolor:blue
				mats = #(pmat1,pmat2,pmat1)
				multimat = multimaterial name:(getFilenameFile dosyaisimleri[i]) count:3 materiallist:mats
				append matlib multimat
			)
			local isimler = for i in currentmateriallibrary collect i.name
			for i in isimler do deleteitem currentmateriallibrary i -- gecici material kutuphanesini temizle
			for i in matlib do append currentmateriallibrary i -- gecici material kutuphanesine yeni material ekle
			savetempmateriallibrary currentmateriallibrary matlibdosya
		)
		local version = "1.0"
		dotNetControl 'lbl1' "System.Windows.Forms.label" pos:[24,32] width:232 height:40 align:#left
		dotNetControl 'lbl2' "System.Windows.Forms.label" pos:[24,80] width:232 height:40 align:#left
		button 'btn1' "Install" pos:[48,160] width:176 height:40 align:#left
		button 'btn2' "Uninstall" pos:[48,216] width:176 height:40 align:#left
		
		on install_ro open do
		(
			lbl1.backcolor = (dotnetclass "system.drawing.color").fromargb 255 68 68 68
			lbl1.text = "SARFINET"
			lbl1.font = dotnetobject "System.Drawing.Font" "Arial" 24 (dotNetclass"System.Drawing.fontstyle").bold
			lbl1.TextAlign = (dotnetclass "System.Drawing.ContentAlignment").bottomcenter
			lbl1.forecolor = (dotnetclass "system.drawing.color").white
			lbl2.text = "Professional Cabinet Design Software\n www.sarfinet.com\nVersion: "+version
			lbl2.backcolor = (dotnetclass "system.drawing.color").fromargb 255 68 68 68
			lbl2.font = dotNetObject "System.Drawing.Font" "Verdana" 8
			lbl2.ForeColor = (dotNetClass "System.Drawing.Color").LightGray
			lbl2.TextAlign = (dotnetclass "System.Drawing.ContentAlignment").middlecenter
		)
		on btn1 pressed do
		(
			local ctypes = python.import "ctypes" -- yonetici izni ile cmd calistirmak icin gerekli kutuphane
			local py_path = "\""+(getdir #maxroot)+"Python\python.exe"+"\""
			local ilave1 = " -m ensurepip" -- pip aktif et (aktif degilse)
			local ilave2 = " -m pip install requests cryptography "-- --ignore-installed" -- 2 tane kutuphane kur
			local komut1 = py_path+ilave1 -- /k > cmd islem bitince acik kalir -- /c > cmd islem bitince kapanir
			local komut2 = py_path+ilave2
			doscommand komut1
			doscommand komut2
			local kaynakklasor = (getFilenamePath (getThisScriptFileName()))
			local sys = python.import "sys"
			local temp_path = (kaynakklasor+"\Programs") -- gecici olarak eklenecek kutuphane adresi
			sys.path.append temp_path -- python kutuphane adresi ekle
			local slf = python.import "slf" -- slf kutuphanesini yukle
			global st
			global sarfinet_version = 1 -- lodi kullanir
			slf.lodi() -- yukleme ekrani (cevap gelmeden yurutme devam etmez)
			if st != undefined and superclassof st == structdef then -- serial_key onaylanirsa st isimli degisken tanimlanir
			(
				if kaynakklasor != st.root then
				(
					try(
						if sarfinet != undefined and superclassof sarfinet == structdef then slfbosalt (sarfinet.getdir.programs+"\slf")
						if not doesfileexist st.root then makedir st.root -- klasor olustur
						startcopy kaynakklasor st.root
						if doesfileexist (st.root+"\icons") then(
							makedir ((getdir #usericons)+"\Sarfinet")
							startcopy (kaynakklasor+"\icons") ((getdir #usericons)+"\Sarfinet")
						)
						if (maxversion())[8] == 2027 then
						(
							local slf_files = getfiles (kaynakklasor+"\Programs\slf2027\*")
							for i in slf_files do 
							(
								local newfile = (st.root+"\Programs\slf\\")+(filenameFromPath i)
								try(
									(dotnetclass "system.io.file").copy i newfile true
								)catch()
							)
						)
						-- eklenti listesine Sarfinet\startup klasorunu ekle (customize > configure user and system path > 3rd party plug-ins)
						setinisetting ((getdir #maxdata)+"\Plugin.UserSettings.ini") "Directories" "Sarfinet" (st.root+"\startup")
						ini_ayarla (st.root+"\startup\sarfinet.ini") st.root st.localization
						deletefile (st.root+"\mzp.run")
						deletefile (st.root+"\install.ms")
						local pluginsfiles = getfiles ((getdir #maxroot)+"Plugins\SARF\*")
						try(for i in pluginsfiles do deletefile i)catch()
						destroydialog install_ro -- kapandiginda close eventi devreye girer
						if doesfileexist (st.root+"\startup\SarfinetStartup.ms") then filein (st.root+"\startup\SarfinetStartup.ms")
						else if doesfileexist (st.root+"\startup\SarfinetStartup.mse") then filein (st.root+"\startup\SarfinetStartup.mse")
						create_material_library (st.root+@"\resources\MaterialLibrary\sarfinetmateriallibrary.mat")
						globalvars.remove "st"
						displaytempprompt "Sarfinet - Installation Successfully !" 15000
						
					)
					catch(
						destroydialog install_ro
						globalvars.remove "st"
						displaytempprompt "Sarfinet - Installation Failed !" 15000
						messagebox (getcurrentexception()) title:"Sarfinet"
						print (getcurrentexception())
					)
				)
			)
		)
		on btn2 pressed do
		(
			local var = hasINISetting ((getdir #maxdata)+"\Plugin.UserSettings.ini") "Directories" "Sarfinet"
			if var then(
				local startup = getinisetting ((getdir #maxdata)+"\Plugin.UserSettings.ini") "Directories" "Sarfinet"
				if doesfileexist (getFilenamePath startup) and filenameFromPath (getFilenamePath startup) == @"Sarfinet\" then(
					menu = menuMan.findMenu "Sarfinet"
					if menu != undefined do menuMan.unRegisterMenu menu
					try((dotNetClass "System.IO.Directory").Delete @"C:\Sarfinet" true)catch()
					delinisetting ((getdir #maxdata)+"\Plugin.UserSettings.ini") "Directories" "Sarfinet"
					messagebox "Uninstall has been completed." title:"Sarfinet"
				)
			)
		)
		on install_ro close do
		(
			slfbosalt ((getdir #temp)+"\Sarfinet\Programs\slf")
			temp_temizle()
		)
	)
	createdialog install_ro
	ok
)