<%@ include file="../../EgTrackerAJSON.jsp"%>
<%@ page import = "net.sf.json.*"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "egurkha.EgDbAdapterUI"%>
<%@ page import = "com.eg.EgDbAdapterTypeUI"%>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>

<%
	response.setHeader("Cache-Control", "no-store");
	response.setHeader("Pragma", "no-cache");
	response.setDateHeader("Expires", -1);
%>

<%
	JSONObject rootJson = new JSONObject();
	String comeFrom = request.getParameter("comeFrom");
	if (comeFrom !=  null && comeFrom.equalsIgnoreCase("getMgrModel"))
	{
		IniFile egServicesIniFile = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_services.ini");
		IniFile eglsIniFile = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_ls_portal.ini");
		String eGMgrModel = egServicesIniFile.getValue("MISC_ARGS","eGMgrModel");
		String issueUUID = egServicesIniFile.getValue("MISC_ARGS","issueUUID");
		String enableCloudRegistration = "true";
		String hideRegistrationTab = eglsIniFile.getValue("LS_TRIAL_SETTINGS","hideRegistrationTab");
		if (hideRegistrationTab != null && (hideRegistrationTab.equalsIgnoreCase("true") || hideRegistrationTab.equalsIgnoreCase("yes")))
		{
			enableCloudRegistration = "false";
		}
		rootJson.put("eGMgrModel",eGMgrModel);
		rootJson.put("issueUUID",issueUUID);
		rootJson.put("enableCloudRegistration",enableCloudRegistration);
		String selfRegistrationMailID = eglsIniFile.getValue("LS_TRIAL_SETTINGS","selfRegistrationMailID");
		if (selfRegistrationMailID != null && selfRegistrationMailID.trim().length() > 0)
		{
			rootJson.put("selfRegistrationMailID",selfRegistrationMailID);
		}
	}
	else if (comeFrom !=  null && comeFrom.equalsIgnoreCase("updateMgrModel"))
	{
		boolean isAuditChange = false;
		AuditLogThread auditLogThread = new AuditLogThread();
		auditLogThread.setValue("Category","Admin");
		auditLogThread.setValue("User_id",_user.getLoginID());
		auditLogThread.setValue("Host_ip",_user.getUserIP(session.getId()));
		auditLogThread.setValue("Session_id",session.getId());
		auditLogThread.setValue("Module","Manager Settings");
		auditLogThread.setValue("Activity","Manager Model");

		String eGMgrModel = request.getParameter("eGMgrModel");
		String issueUUID = request.getParameter("issueUUID");
		IniFile egServicesIniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_services.ini",true);
		IniFile egUIIniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_ui.ini",true);
		IniFile egPortalIniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_ls_portal.ini",true);
		IniFile egMgrMiscIniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_mgrmisc.ini",true);
		IniFile testIniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_tests.ini",true);
		IniFile egDiscHelperIniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_DiscoveryHelper.ini",true);
		String preeGMgrModel = egServicesIniFileWr.getValue("MISC_ARGS","eGMgrModel");
		
		if (!preeGMgrModel.equalsIgnoreCase(eGMgrModel))
		{
			isAuditChange = true;
			String displayName = "01~~Manager Model";
			auditLogThread.setPreviousValue(displayName,preeGMgrModel);
			auditLogThread.setCurrentValue(displayName,eGMgrModel);
		}

		egServicesIniFileWr.setValue("MISC_ARGS","eGMgrModel",eGMgrModel);
		//egServicesIniFileWr.setValue("MISC_ARGS","issueUUID",issueUUID);
		if (eGMgrModel != null && eGMgrModel.equalsIgnoreCase("msp"))
		{
			String preselfRegStr = egPortalIniFileWr.getValue("LS_TRIAL_SETTINGS","hideRegistrationTab");
			if (preselfRegStr == null || preselfRegStr.trim().length() == 0)
			{
				preselfRegStr = "false";
			}
			String selfRegYes = request.getParameter("selfRegYes");
			String selfRegMailId = request.getParameter("selfRegMailId");
			String selfRegStr = selfRegYes.equalsIgnoreCase("yes")?"false":"true";
			egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","hideRegistrationTab",selfRegStr);
			if (!preselfRegStr.equalsIgnoreCase(selfRegStr))
			{
				isAuditChange = true;
				String displayName = "02~~Allow users to self-register";
				String preselfRegStrAL = preselfRegStr.equalsIgnoreCase("false")?"Yes":"No";
				String selfRegStrAL = selfRegStr.equalsIgnoreCase("false")?"Yes":"No";
				auditLogThread.setPreviousValue(displayName,preselfRegStrAL);
				auditLogThread.setCurrentValue(displayName,selfRegStrAL);
			}
			if (selfRegStr.equalsIgnoreCase("false"))
			{
				String preselfRegMailId = egPortalIniFileWr.getValue("LS_TRIAL_SETTINGS","selfRegistrationMailID");
				if (preselfRegMailId == null || preselfRegMailId.trim().length() == 0)
				{
					preselfRegMailId = "-";
				}
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","selfRegistrationMailID",selfRegMailId);
				if (selfRegMailId == null || selfRegMailId.trim().length() == 0)
				{
					selfRegMailId = "-";
				}
				if (!preselfRegMailId.equalsIgnoreCase(selfRegMailId))
				{
					isAuditChange = true;
					String displayName = "03~~Send user registration details to these mail IDs";
					auditLogThread.setPreviousValue(displayName,preselfRegMailId);
					auditLogThread.setCurrentValue(displayName,selfRegMailId);
				}
			}
			else
			{
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","selfRegistrationMailID","");
			}
		}
		else
		{
			egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","hideRegistrationTab","false");
			egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","selfRegistrationMailID","");
		}

		String enableOtherCloudSettings = egServicesIniFileWr.getValue("MISC_ARGS","enableOtherCloudSettings");
		String enableCloudRegistration = egServicesIniFileWr.getValue("MISC_ARGS","enableCloudRegistration");
		if (eGMgrModel != null && eGMgrModel.equalsIgnoreCase("enterprise") && !preeGMgrModel.equalsIgnoreCase(eGMgrModel))
		{
			egServicesIniFileWr.setValue("MISC_ARGS","DiscBase","DNS");
			egServicesIniFileWr.setValue("MISC_ARGS","DHCPEnvironment","false");
			egServicesIniFileWr.setValue("MISC_ARGS","AutoIpNameCheck","Yes");
			egServicesIniFileWr.setValue("MISC_ARGS","NickIpCheck","Yes");
			egServicesIniFileWr.setValue("ZoneAndUserCertUsage","UserWiseCertReport","No");
			egServicesIniFileWr.setValue("ZoneAndUserCertUsage","ZoneWiseCertReport","Yes");
			egServicesIniFileWr.setValue("MISC_ARGS","AllowDeleteOrg","no");
			egServicesIniFileWr.setValue("MISC_ARGS","DeleteOrg","no");
			egServicesIniFileWr.setValue("MISC_ARGS","DeleteSubOU","no");
			egServicesIniFileWr.setValue("MISC_ARGS","DeleteOptions","");
			egServicesIniFileWr.setValue("MISC_ARGS","AutoDeleteExpiredUsersAfterDays","30");
			egServicesIniFileWr.setValue("MISC_ARGS","daysCompsNotReporting","30");
			egServicesIniFileWr.setValue("MISC_ARGS","ImmediateDelete","yes");
			egServicesIniFileWr.setValue("MISC_ARGS","checkUID","No");

			//egUIIniFileWr.setValue("LOGIN","AUTHENTICATION","No");
			
			egServicesIniFileWr.setValue("MISC_ARGS","Automanage","true");
			egDiscHelperIniFileWr.setValue("DISCOVERY_SETTINGS","AgentDiscoveryEnabled","true");
			testIniFileWr.setValue("AGENT_SETTINGS","AgentDiscover","true");
			egMgrMiscIniFileWr.setValue("MISC_ARGS","autoCreateZoneForUser","false");
			egServicesIniFileWr.setValue("MISC_ARGS","RediscoverDeletedComponents","No");

			egUIIniFileWr.setValue("USER_SIGN_IN","defaultLandingUI","defaultLogin");
			//egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","defaultPortal","clsTrial");
			egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","enableLS","false");
			egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","enable2FAForReg","false");
		}
		else if (eGMgrModel != null && eGMgrModel.equalsIgnoreCase("msp") && !preeGMgrModel.equalsIgnoreCase(eGMgrModel))
		{
			egServicesIniFileWr.setValue("MISC_ARGS","DiscBase","DNS");
			egServicesIniFileWr.setValue("MISC_ARGS","DHCPEnvironment","true");
			egServicesIniFileWr.setValue("MISC_ARGS","AutoIpNameCheck","No");
			egServicesIniFileWr.setValue("MISC_ARGS","NickIpCheck","No");
			egServicesIniFileWr.setValue("ZoneAndUserCertUsage","UserWiseCertReport","No");
			egServicesIniFileWr.setValue("ZoneAndUserCertUsage","ZoneWiseCertReport","No");
			egServicesIniFileWr.setValue("MISC_ARGS","AuthenticationModeForLogin","eG Enterprise");
			egServicesIniFileWr.setValue("MISC_ARGS","AllowDeleteOrg","no");
			egServicesIniFileWr.setValue("MISC_ARGS","DeleteOrg","no");
			egServicesIniFileWr.setValue("MISC_ARGS","DeleteSubOU","no");
			egServicesIniFileWr.setValue("MISC_ARGS","DeleteOptions","");
			egServicesIniFileWr.setValue("MISC_ARGS","AutoDeleteExpiredUsersAfterDays","30");
			egServicesIniFileWr.setValue("MISC_ARGS","daysCompsNotReporting","30");
			egServicesIniFileWr.setValue("MISC_ARGS","ImmediateDelete","yes");
			egServicesIniFileWr.setValue("MISC_ARGS","checkUID","Yes");

			//egUIIniFileWr.setValue("LOGIN","AUTHENTICATION","Yes");

			if (enableOtherCloudSettings != null && enableOtherCloudSettings.equalsIgnoreCase("true"))
			{
				egServicesIniFileWr.setValue("MISC_ARGS","Automanage","true");
				egDiscHelperIniFileWr.setValue("DISCOVERY_SETTINGS","AgentDiscoveryEnabled","true");
				testIniFileWr.setValue("AGENT_SETTINGS","AgentDiscover","true");
				egMgrMiscIniFileWr.setValue("MISC_ARGS","autoCreateZoneForUser","true");
				egServicesIniFileWr.setValue("MISC_ARGS","RediscoverDeletedComponents","Yes");

				IniFile disableWrObj = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_disable.ini",true);
				disableWrObj.deleteSubject("Deleted_Servers");
				disableWrObj.saveFile();
				EgConfigFilesSync.releaseInstance(disableWrObj);
			}

			if (enableCloudRegistration != null && enableCloudRegistration.equalsIgnoreCase("true"))
			{
				egUIIniFileWr.setValue("USER_SIGN_IN","defaultLandingUI","portal");
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","defaultPortal","cloud");
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","enableLS","true");
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","enable2FAForReg","true");
			}
			else
			{
				egUIIniFileWr.setValue("USER_SIGN_IN","defaultLandingUI","defaultLogin");
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","defaultPortal","clsTrial");
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","enableLS","false");
				egPortalIniFileWr.setValue("LS_TRIAL_SETTINGS","enable2FAForReg","false");
			}
		}
		
		if (isAuditChange)
		{
			auditLogThread.setValue("Description","eG Manager Model has been configured successfully");
			auditLogThread.sendItToDB();
		}

		egServicesIniFileWr.saveFile();
		EgConfigFilesSync.releaseInstance(egServicesIniFileWr);
		egUIIniFileWr.saveFile();
		EgConfigFilesSync.releaseInstance(egUIIniFileWr);
		egPortalIniFileWr.saveFile();
		EgConfigFilesSync.releaseInstance(egPortalIniFileWr);
		egMgrMiscIniFileWr.saveFile();
		EgConfigFilesSync.releaseInstance(egMgrMiscIniFileWr);
		egDiscHelperIniFileWr.saveFile();
		EgConfigFilesSync.releaseInstance(egDiscHelperIniFileWr);
		testIniFileWr.saveFile();
		EgConfigFilesSync.releaseInstance(testIniFileWr);
		rootJson.put("success",true);
	}
	out.println(rootJson.toString());
%>



