<!DOCTYPE project [
    <!ENTITY importExternalLogs SYSTEM "file:./scripts/importExternalLogs.xml">
]>
<project name="winserviceConfig" default="winserviceConfig" basedir=".">
    
    <taskdef name="wscmtlog"
        classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.AntTaskToLogToConfigManagersLogFiles"/>
 	&importExternalLogs;            
    <property name="NODE_NAME" value="${WS_CMT_NODE_NAME}"/>
    <property name="CELL_NAME" value="${WS_CMT_CELL_NAME}"/>

    <target name="detectCurrentOSFamily"
            description="Detecs the current OS family (UNIX or Windows)">
        
        <!-- OS/400 setting must come before the unix setting -->
        <condition property="currentOSFamily" value="os/400">
            <equals arg1="${os.name}" arg2="OS/400"/>
        </condition>
        
        <condition property="currentOSFamily" value="windows">
            <os family="windows"/>
        </condition>
        
        <condition property="currentOSFamily" value="unix">            
            <os family="unix"/>
        </condition>
        
        <wscmtlog>Detected current OS family to be: ${currentOSFamily}</wscmtlog>
    </target>
    
    <target name="defineOSSpecificConfigFlag"
            description="Defines either configUNIX or configWindows depending on the current OS">
            
        <condition property="configOS400" value="true">
            <equals arg1="${currentOSFamily}" arg2="os/400"/>
        </condition>
        
        <condition property="configUNIX" value="true">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        
        <condition property="configWindows" value="true">
            <equals arg1="${currentOSFamily}" arg2="windows"/>
        </condition>
    </target>

    <target name="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
        <replace file="${file}"
            token="${token}"
            value="${tokenValue}"/>
    </target>
    
    <target name="replacePathSeparator">
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
                        <param name="file" value="${profilePath}\properties\winservice.props"/>
                        <param name="token" value="\"/>
                        <param name="tokenValue" value="\\"/>
            </antcall>                 
            <antcall target="replaceAllInstancesOfGivenTokenWithGivenValueForTheGivenFile">
                        <param name="file" value="${profilePath}\properties\winservice.props"/>
                        <param name="token" value="/"/>
                        <param name="tokenValue" value="\\"/>
            </antcall>            
    </target>
    
    <target name="queryWinServiceCheckFlag"
            description="Determine the flag to create windows service">
            
        <condition property="createWinService" value="true">
            <equals arg1="${winserviceCheck}" arg2="true"/>
        </condition>
        
        <wscmtlog>Detected WinServiceCheckFlag to be: ${winserviceCheck}</wscmtlog>
    </target>

    <target name="queryWinServiceAccountTypeFlag"
            description="Determine the flag to create windows service, includes checking if security is set">            
        
        <!-- local system without admin security -->
        <condition property="localsystemaccount" value="true">
        	<and>
            	<equals arg1="${winserviceAccountType}" arg2="localsystem"/>
            	<not>
            		<equals arg1="${enableAdminSecurity}" arg2="true"/>
            	</not>
          </and>
        </condition>

        <!-- local system with admin security -->
		    <condition property="localWithAdminSecurity" value="true">
    		  <and>
    			    <equals arg1="${enableAdminSecurity}" arg2="true"/>
    			    <equals arg1="${winserviceAccountType}" arg2="localsystem"/>
    		  </and>
    	  </condition>
        
		    <!-- specified user without admin security -->
        <condition property="specifieduseraccount" value="true">
        	<and>
            	<equals arg1="${winserviceAccountType}" arg2="specifieduser"/>
            	<not>
            		<equals arg1="${enableAdminSecurity}" arg2="true"/>
            	</not>
          </and>
        </condition>      
        
        <!-- specified user with admin security -->
        <condition property="specifiedWithAdminSecurity" value="true">
    		  <and>
    			    <equals arg1="${enableAdminSecurity}" arg2="true"/>
    			    <equals arg1="${winserviceAccountType}" arg2="specifieduser"/>
    		  </and>
    	 </condition>  
        
        
        <wscmtlog>Detected WinServiceAccountTypeFlag to be: ${winserviceAccountType} and enableAdminSecurity to be: ${enableAdminSecurity}</wscmtlog>        
    </target>
    
    <target name="createWindowsWASServiceUsingLocalSystem"
            description="Creates the Windows WAS Service using localsystem account"
            if="localsystemaccount">
            
        <wscmtlog>Creating the Windows WAS service using localsystem account, the output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log</wscmtlog>
  	    <!-- fail on error was true, but changed to false to accomodate F69142-65189 -->           
        <exec executable="${WAS_HOME}\bin\wasservice.exe"
                output="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log"
                failonerror="false"
				resultproperty="rcProperty">
            <arg value="-add"/>
            <arg value="${WS_CMT_NODE_NAME}"/>
            <arg value="-serverName"/>
            <arg value="${serverName}"/>             
            <arg value="-profilePath"/>
            <arg value="${profilePathWindows}"/>
            <arg value="-logRoot"/>
            <arg value="${profilePathWindows}\logs\${serverName}"/>        
	    <arg value="-startType"/>
	    <arg value="${winserviceStartupType}"/>
	    <arg value="-restart"/>
	    <arg value="false"/>	    
        </exec>
		<antcall target="importLogsToMain" >
			<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log" />
			<param name="description" value="Creating Windows Service" />
			<param name="precondition" value="foetrue" />
		</antcall>	 	
    </target>

    <target name="createWindowsWASServiceUsingLocalSystemWithAdminSecurity"
            description="Creates the Windows WAS Service using localsystem account, admin security is set"
            if="localWithAdminSecurity">
            
        <wscmtlog>Creating the Windows WAS service using localsystem account, with admin security set, the output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log</wscmtlog>
   	    <!-- fail on error was true, but changed to false to accomodate F69142-65189 -->                  
        <exec executable="${WAS_HOME}\bin\wasservice.exe"
                output="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log"
                failonerror="fail"
				resultproperty="rcProperty">
            <arg value="-add"/>
            <arg value="${WS_CMT_NODE_NAME}"/>
            <arg value="-serverName"/>
            <arg value="${serverName}"/>             
            <arg value="-profilePath"/>
            <arg value="${profilePathWindows}"/>
            <arg value="-logRoot"/>
            <arg value="${profilePathWindows}\logs\${serverName}"/>
            <arg value="-stopArgs"/>
            <arg value="&quot;-username ${adminUserName} -password ${adminPassword}&quot;"/>
            <arg value="-encodeParams"/>        
	    <arg value="-startType"/>
	    <arg value="${winserviceStartupType}"/>
	    <arg value="-restart"/>
	    <arg value="false"/>	    
        </exec>
		<antcall target="importLogsToMain" >
			<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log" />
			<param name="description" value="Creating Windows Service" />
			<param name="precondition" value="foetrue" />
		</antcall>	 	
    </target>

    <target name="createWindowsWASServiceUsingSpecifiedUser"
            description="Creates the Windows WAS Service using specified user account"
            if="specifieduseraccount">
            
        <wscmtlog>Creating the Windows WAS service using specified user account, the output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log</wscmtlog>
  	    <!-- fail on error was true, but changed to false to accomodate F69142-65189 -->                   
        <exec executable="${WAS_HOME}\bin\wasservice.exe"
                output="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log"
                failonerror="fail"
				resultproperty="rcProperty">
            <arg value="-add"/>
            <arg value="${WS_CMT_NODE_NAME}"/>
            <arg value="-serverName"/>
            <arg value="${serverName}"/>            
            <arg value="-profilePath"/>
            <arg value="${profilePathWindows}"/>
            <arg value="-logRoot"/>
            <arg value="${profilePathWindows}\logs\${serverName}"/>
	    <arg value="-userid"/>
	    <arg value="${winserviceUserName}"/>
	    <arg value="-password"/>
	    <arg value="${winservicePassword}"/>
	    <arg value="-startType"/>
	    <arg value="${winserviceStartupType}"/>
	    <arg value="-restart"/>
	    <arg value="false"/>
        </exec>
		<antcall target="importLogsToMain" >
			<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log" />
			<param name="description" value="Creating Windows Service" />
			<param name="precondition" value="foetrue" />
		</antcall>	 	
    </target>
            
    <target name="createWindowsWASServiceUsingSpecifiedUserWithAdminSecurity"
            description="Creates the Windows WAS Service using specified user account, admin security is set"
            if="specifiedWithAdminSecurity">
            
        <wscmtlog>Creating the Windows WAS service using specified user account, with admin security set, the output will be recorded in: ${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log</wscmtlog>
  	    <!-- fail on error was true, but changed to false to accomodate F69142-65189 -->                   
        <exec executable="${WAS_HOME}\bin\wasservice.exe"
                output="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log"
                failonerror="false"
				resultproperty="rcProperty">
            <arg value="-add"/>
            <arg value="${WS_CMT_NODE_NAME}"/>
            <arg value="-serverName"/>
            <arg value="${serverName}"/>            
            <arg value="-profilePath"/>
            <arg value="${profilePathWindows}"/>
            <arg value="-logRoot"/>
            <arg value="${profilePathWindows}\logs\${serverName}"/>
            <arg value="-stopArgs"/>
            <arg value="&quot;-username ${adminUserName} -password ${adminPassword}&quot;"/>
            <arg value="-encodeParams"/>
	    <arg value="-userid"/>
	    <arg value="${winserviceUserName}"/>
	    <arg value="-password"/>
	    <arg value="${winservicePassword}"/>
	    <arg value="-startType"/>
	    <arg value="${winserviceStartupType}"/>
	    <arg value="-restart"/>
	    <arg value="false"/>
        </exec>
		<antcall target="importLogsToMain" >
			<param name="logfile" value="${WS_CMT_LOG_HOME}/${profileName}\winservice_config.log" />
			<param name="description" value="Creating Windows Service" />
			<param name="precondition" value="foetrue" />
		</antcall>	 	
    </target>    
    
            
    <target name="createWindowsWASService"
            description="Creates the Windows WAS Service"
            if="createWinService" unless="configUNIX">
            <echo file="${profilePath}\properties\winservice.props" message="nodeName=${WS_CMT_NODE_NAME}&#10;&#13;" append="true"/>
            <echo file="${profilePath}\properties\winservice.props" message="profilePathWindows=${profilePath}" append="true"/>
	                
            <antcall target="replacePathSeparator"/>
            <antcall target="executeNative2Ascii"/>
            <property file="${profilePath}\properties\winservice.props"/>
            
            <antcall target="createWindowsWASServiceUsingLocalSystem"/>
            <antcall target="createWindowsWASServiceUsingLocalSystemWithAdminSecurity"/>
            <antcall target="createWindowsWASServiceUsingSpecifiedUser"/>
            <antcall target="createWindowsWASServiceUsingSpecifiedUserWithAdminSecurity"/>				
    </target>

    <target name="executeNative2Ascii" 
            description="Converts files from native encodings to ASCII with escaped Unicode">

        <exec executable="${WAS_HOME}/java/bin/native2ascii">
            <arg value="${profilePath}/properties/winservice.props"/>
            <arg value="${profilePath}/temp/winservice.props"/>
        </exec>

        <copy file="${profilePath}/temp/winservice.props"
              tofile="${profilePath}/properties/winservice.props"
              overwrite="true"/>
    </target>
    
    <target name="winserviceConfig"
        description="Configures Windows WAS service"
        if="configWindows"
        depends="detectCurrentOSFamily,
            defineOSSpecificConfigFlag,
            queryWinServiceCheckFlag,
            queryWinServiceAccountTypeFlag,            
            createWindowsWASService"/>    

</project>
