//
//  RuntimeCode.swift
//  TINU
//
//  Created by ITzTravelInTime on 17/10/17.
//  Copyright © 2017 Pietro Caruso. All rights reserved.
//

import Cocoa

//functions used in in different parts of the app

//checks for the options
public func checkOtherOptions(){
	DispatchQueue.global(qos: .background).async{
		oom.shared.restoreOtherOptions()
		#if !macOnlyMode
			BootFilesReplacementManager.shared.eraseReplacementFilesData()
		#endif
		
		#if useEFIReplacement && !macOnlyMode
			
			let _ = EFIFolderReplacementManager.shared.unloadEFIFolder()
			
		#endif
		
		processLicense = ""
		
		if cvm.shared.sharedApp != nil{
			if let version = iam.shared.targetAppBundleVersion(), let name = iam.shared.targetAppBundleName(){
				cvm.shared.sharedBundleVersion = version
				cvm.shared.sharedBundleName = name
				
				var supportsTINU = false
				
				if let st = iam.shared.sharedAppNotSupportsTINU(){
					supportsTINU = st
				}
				
				var needsIA = false
				
				if let na = iam.shared.sharedAppNeedsIABoot(){
					needsIA = na
				}
				
				if let item = oom.shared.otherOptions[oom.shared.ids.otherOptionTinuCopyID]{
					item.isUsable = !supportsTINU
					item.isActivated = !supportsTINU
				}
				
				if sharedInstallMac{
					var supportsAPFS = false
					
					if let st = iam.shared.sharedAppNotSupportsAPFS(){
						supportsAPFS = st
					}
					
					if let st = iam.shared.sharedAppNotIsMojave(){
						if !st{
							supportsAPFS = true
						}
					}
					
					
					if let item = oom.shared.otherOptions[oom.shared.ids.otherOptionDoNotUseApfsID]{
						item.isVisible = !supportsAPFS
						item.isActivated = !cvm.shared.sharedSVReallyIsAPFS
						item.isUsable = !cvm.shared.sharedSVReallyIsAPFS
					}
				}else{
					#if !macOnlyMode
					
					for i in 0...(BootFilesReplacementManager.shared.filesToReplace.count - 1){
						let item = BootFilesReplacementManager.shared.filesToReplace[i]
						
						switch item.filename{
						case "prelinkedkernel":
							item.visible = !supportsTINU
						case "kernelcache":
							item.visible = supportsTINU
						case "immutablekernel", "BridgeVersion.bin", "SecureBoot.bundle":
							item.visible = needsIA
						default:
							break
						}
						
					}
					
					#endif
					
					#if !macOnlyMode
					
					if let item = oom.shared.otherOptions[oom.shared.ids.otherOptionCreateAIBootFID]{
							item.isActivated = false
							item.isVisible = needsIA
					}
					
					if let item = oom.shared.otherOptions[oom.shared.ids.otherOptionDeleteIAPMID]{
							item.isActivated = false
							item.isVisible = needsIA
					}
					
					#endif
				}
				
			}
			
			if let item = oom.shared.otherOptions[oom.shared.ids.otherOptionForceToFormatID]{
				if let st = cvm.shared.sharedVolumeNeedsPartitionMethodChange{
					item.isUsable = !st
					item.isActivated = st
				}
			}
		}
	}

}

//this funtionm terminates a process
public func terminateProcess(name: String) -> Bool!{
	
	let pid = getOut(cmd:"ps -Ac -o pid,comm | awk '/^ *[0-9]+ " + name + "$/ {print $1}'")
	
	if pid.isEmpty{
		log("Process \"" + name + "\" does not needs to be closed")
		return true
	}else{
		if let res = runCommandWithSudo(cmd: "/bin/sh", args: ["-c", "kill " + pid]){
			
			if res.exitCode != 0{
				log("Failed to close \"\(name)\" because the closing process has exited with a code that is not 0: \n     exit code: \(res.exitCode)\n    output: \(res.output)\n     error/s produced: \(res.error)")
				return false
			}
			
			if let f = res.output.first{
				if (f.isEmpty || f == "Password:" || f == "\n"){
					log("Process \"\(name)\" stopped with success")
					return true
				}else{
					log("Failed to close \"\(name)\": \n     exit code: \(res.exitCode)\n    output: \(res.output)\n     error/s produced: \(res.error)")
					return false
				}
			}else{
				log("Failed to close \"\(name)\" because is not possible to get the output of the termination process")
				return false
			}
		}else{
			log("Failed to close \"\(name)\" because of an authentication failure")
			return nil
		}
	}
	
	//return false
}
