<project name="modifyLibPathSetupCmdLine" default="install" basedir=".">
    <taskdef name="wscmtlog" classname="com.ibm.ws.install.configmanager.actionengine.ant.utils.AntTaskToLogToConfigManagersLogFiles"/>
     
    <property name="separatorFile" value="/"/>
   
    <target name="detectCurrentOSFamily"
            description="Detects the current OS family (UNIX or Windows).">

      <condition property="currentOSFamily" value="os/400">
      	<os family="os/400"/>
      </condition>
      <condition property="currentOSFamily" value="unix">            
    	    <os family="z/os"/>
      </condition>
      <condition property="currentOSFamily" value="unix">            
          <os family="unix"/>
      </condition>
        
      <wscmtlog>Detected current OS family to be: ${currentOSFamily}</wscmtlog>
    </target>

    <target name="setCurrentOSFamily"
            description="Detects the current OS family (UNIX)">

        <condition property="isCurrentOSUNIX" value="true">
            <equals arg1="${currentOSFamily}" arg2="unix"/>
        </condition>
        <condition property="isCurrentOSOS400" value="true">
            <equals arg1="${currentOSFamily}" arg2="os/400"/>
        </condition>
    </target>

    <target name="resolveSetupCmdLineName"
            description="Resolves the name of setupCmdLine script for the current platform.">

        <condition property="setupCmdLineName" 
                value="setupCmdLine.sh">
            <equals arg1="${currentOSFamily}" 
                arg2="unix"/>
        </condition>
        
        <condition property="setupCmdLineName" 
                value="setupCmdLine">
            <equals arg1="${currentOSFamily}" 
                arg2="os/400"/>
        </condition>
        
        <wscmtlog>Detected setupCmdLine script name to be: ${setupCmdLineName}</wscmtlog>
    </target>
    
    <target name="setFileName">
        <property name="file" value="${WAS_HOME}${separatorFile}bin${separatorFile}${setupCmdLineName}"/>
    	<wscmtlog>File name: ${file}</wscmtlog>
    </target>
    
    <target name="replaceStringWithRightValuesForSetupCmdLineInstallUNIX"
            description="Modify setupCmdLine shell script to add double quote around directory to be changed for UNIX."
            if="isCurrentOSUNIX">
            
        <!-- Modify AIX section -->
        <replace file="${file}">
          <replacetoken>  AIX)</replacetoken>
          <replacevalue><![CDATA[  AIX)
    if test "x$LIBPATH" = "x"; then
      LIBPATH="$WAS_HOME"/nulldllsdir
    fi]]></replacevalue>
        </replace>
       	
        <!-- Modify Linux section -->
        <replace file="${file}">
          <replacetoken>  Linux)</replacetoken>
          <replacevalue><![CDATA[  Linux)
    if test "x$LD_LIBRARY_PATH" = "x"; then
      LD_LIBRARY_PATH="$WAS_HOME"/nulldllsdir
    fi]]></replacevalue>
        </replace>
       
        <!-- Modify Solaris section -->
        <replace file="${file}">
          <replacetoken>  SunOS)</replacetoken>
          <replacevalue><![CDATA[  SunOS)
    if test "x$LD_LIBRARY_PATH" = "x"; then
      LD_LIBRARY_PATH="$WAS_HOME"/nulldllsdir
    fi]]></replacevalue>
        </replace>
        
        <!-- Modify HPUX section -->
        <replace file="${file}">
          <replacetoken>  HP-UX)</replacetoken>
          <replacevalue><![CDATA[  HP-UX)
    if test "x$SHLIB_PATH" = "x"; then
      SHLIB_PATH="$WAS_HOME"/nulldllsdir
    fi]]></replacevalue>
        </replace>
        
        <!-- Modify OS390 section -->
        <replace file="${file}">
          <replacetoken>  OS/390)</replacetoken>
          <replacevalue><![CDATA[  OS/390)
    if test "x$LIBPATH" = "x"; then
      LIBPATH="$WAS_HOME"/nulldllsdir
    fi]]></replacevalue>
        </replace>
    
        <!-- Modify others section -->
        <replace file="${file}">
          <replacetoken>  *)</replacetoken>
          <replacevalue><![CDATA[  *)
    if test "x$LIBPATH" = "x"; then
      LIBPATH="$WAS_HOME"/nulldllsdir
    fi]]></replacevalue>
        </replace>
    </target>
    
    <target name="replaceStringWithRightValuesForSetupCmdLineInstallOS400"
            description="Modify setupCmdLine shell script for security fix for UNIX."
            if="isCurrentOSOS400">
            
      <!-- Modify OS400 section -->
      <replace file="${file}">
        <replacetoken>LIBPATH=&quot;${JAVA_NATIVE_LIB_DIR}:${LIBPATH}&quot;</replacetoken>
        <replacevalue><![CDATA[if test "x$LIBPATH" = "x"; then
    LIBPATH="$WAS_HOME"/nulldllsdir
fi
LIBPATH="${JAVA_NATIVE_LIB_DIR}:${LIBPATH}"]]></replacevalue>
       </replace>
    </target>

	
    <target name="replaceStringWithRightValuesForSetupCmdLineUninstallUNIX"
            description="Revert setupCmdLine shell script change for security fix for UNIX."
            if="isCurrentOSUNIX">
	            
      <!-- Modify AIX section -->
      <replace file="${file}">
	          
        <replacetoken><![CDATA[  AIX)
    if test "x$LIBPATH" = "x"; then
      LIBPATH="$WAS_HOME"/nulldllsdir
    fi]]></replacetoken>
        <replacevalue>  AIX)</replacevalue>
      </replace>
	       	
      <!-- Modify Linux section -->
      <replace file="${file}">
        <replacetoken><![CDATA[  Linux)
    if test "x$LD_LIBRARY_PATH" = "x"; then
      LD_LIBRARY_PATH="$WAS_HOME"/nulldllsdir
    fi]]></replacetoken>
        <replacevalue>  Linux)</replacevalue>
      </replace>
       
      <!-- Modify Solaris section -->
      <replace file="${file}">
        <replacetoken><![CDATA[  SunOS)
    if test "x$LD_LIBRARY_PATH" = "x"; then
      LD_LIBRARY_PATH="$WAS_HOME"/nulldllsdir
    fi]]></replacetoken>
        <replacevalue>  SunOS)</replacevalue>
      </replace>
        
      <!-- Modify HPUX section -->
      <replace file="${file}">
        <replacetoken><![CDATA[  HP-UX)
    if test "x$SHLIB_PATH" = "x"; then
      SHLIB_PATH="$WAS_HOME"/nulldllsdir
    fi]]></replacetoken>
        <replacevalue>  HP-UX)</replacevalue>
      </replace>
	    
      <!-- Modify OS390 section -->
      <replace file="${file}">
        <replacetoken><![CDATA[  OS/390)
    if test "x$LIBPATH" = "x"; then
      LIBPATH="$WAS_HOME"/nulldllsdir
    fi]]></replacetoken>
        <replacevalue>  OS/390)</replacevalue>
      </replace>
	
      <!-- Modify other section -->
      <replace file="${file}">
        <replacetoken><![CDATA[  *)
    if test "x$LIBPATH" = "x"; then
      LIBPATH="$WAS_HOME"/nulldllsdir
    fi]]></replacetoken>
        <replacevalue>  *)</replacevalue>
      </replace>
    </target>
	    
    <target name="replaceStringWithRightValuesForSetupCmdLineUninstallOS400"
            description="Modify setupCmdLine shell script to add double quote around directory to be changed for UNIX."
            if="isCurrentOSOS400">
	            
      <!-- Modify OS400 section -->
      <replace file="${file}">
        <replacetoken><![CDATA[if test "x$LIBPATH" = "x"; then
    LIBPATH="$WAS_HOME"/nulldllsdir
fi
LIBPATH="${JAVA_NATIVE_LIB_DIR}:${LIBPATH}"]]></replacetoken>
        <replacevalue>LIBPATH=&quot;${JAVA_NATIVE_LIB_DIR}:${LIBPATH}&quot;</replacevalue>
      </replace>
    </target>
    
    <target name="giveExecutionPermission">
      <!-- chmod the setupCmdLine to give a+x permission -->
      <chmod file="${file}" perm="a+x"/>
    </target>

    <target name="replaceRegularExpressionWithRightValuesForSetupCmdLineInstall"
            description="Shield all shell scripts from LIBARTY PATH security hole."
            depends="replaceStringWithRightValuesForSetupCmdLineInstallUNIX,replaceStringWithRightValuesForSetupCmdLineInstallOS400"/>
	
    <target name="replaceRegularExpressionWithRightValuesForSetupCmdLineUninstall"
            description="Shield all shell scripts from LIBARTY PATH security hole."
            depends="replaceStringWithRightValuesForSetupCmdLineUninstallUNIX,replaceStringWithRightValuesForSetupCmdLineUninstallOS400"/>
    
    <target name="install"
        description="Shield all shell scripts from LIBARTY PATH security hole."
        depends="detectCurrentOSFamily,
          setCurrentOSFamily,
          resolveSetupCmdLineName,
          setFileName,
          replaceRegularExpressionWithRightValuesForSetupCmdLineInstall,
          giveExecutionPermission"/>
     
    <target name="uninstall"
        description="Shield all shell scripts from LIBARTY PATH security hole."
        depends="detectCurrentOSFamily,
          setCurrentOSFamily,
          resolveSetupCmdLineName,
          setFileName,
          replaceRegularExpressionWithRightValuesForSetupCmdLineUninstall,
          giveExecutionPermission"/>
          	
</project>
