<project name="CWSDEV" default="clean_compile_all" basedir="../">

    <!-- init target initialize all the properties used in building
         please ensure that WEBLOGIC_HOME, SVNROOT, DOMAIN_HOME are set according to your system for proper
         building -->
    <property environment="env"/>

    <target name="init">
	<echo message="Inside init task..."/>
		
        <condition property="VALID_JAVA_VERSION">
            <equals arg1="${ant.java.version}" arg2="1.4"/>
        </condition>
        <fail message="Invalid Java version ${ant.java.version}" unless="VALID_JAVA_VERSION"/>
        <property name="DEPLOYMENT_DIR" value="${basedir}/deployment"/>
		<property file="${basedir}/build/properties/cws_dev.properties"/>
        <!--  SVN properties defined here -->
        <property name="svn.repository" value="${env.SVNROOT}"/>
		<path id = "classpath.svn" >
			<fileset dir = "/export/home/weblogic/svnant_lib" >
			<include name="svnant.jar" />
            <include name="svnClientAdapter.jar" />            
            <include name="svnkit.jar" />
            <include name="ganymed.jar" />
           <!-- <include name="svnjavahl.jar" />-->
			</fileset>
         </path>
		<!-- load the svn task, common for all the svn tasks -->
		<!--<taskdef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="classpath.svn"/> -->
		<!--<taskdef resource="org/tigris/subversion/svnant/svnantlib.xml" classpath="/export/home/weblogic/svnant_lib/svnant.jar"/> -->
		<taskdef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="classpath.svn"/>

        <!--  Testing Framework properties -->
        <property name="DEPLOYMENT_USER" value="${env.USER}"/>
        <property name="DEPLOYMENT_PASS" value="${env.PASS}"/>
        <property name="DEPLOYMENT_REMOTEDIR" value="${env.REMOTE_DIR}"/>
        <property name="DEPLOYMENT_REMOTEDIR_JSP" value="${env.REMOTE_DIR_JSP}"/>

        <!-- values needed to deploy -->
        <property name="PROJECT_HOME" value="${env.PROJECT_HOME}"/>
        <property name="JAVA_HOME" value="${env.JAVA_1_4_HOME}"/>
        <property name="WEBLOGIC_HOME" value="${env.WEBLOGIC_8_1_HOME}"/>
        <property name="WEBLOGIC_INSTANCE_HOME" value="${env.WEBLOGIC_INSTANCE_HOME}"/>
        <property name="WEBLOGIC_HOST_URL" value="${env.WEBLOGIC_HOST_URL}"/>
        <property name="WEBLOGIC_PORT" value="${env.WEBLOGIC_PORT}"/>
        <property name="WEBLOGIC_STARTUP_CLASS" value="${env.WEBLOGIC_STARTUP_CLASS}"/>
        <property name="WEBLOGIC_CLASSPATH" value="${EXTERNAL_LIB}/weblogic812.jar;${EXTERNAL_LIB}/webservices.jar;${EXTERNAL_LIB}/ejbgen.jar"/>
              

        <!-- default value for debug is on. It may be overwritten later -->
        <property name="DEBUG" value="on"/>

        <!-- XML base directory for files to be accessed in the classpath of tests -->
        <property name="XML_CLASSPATH_BASE" value="${basedir}/xml"/>

		<!-- new properties-->
		<property name="EAR_HOME" value="${DEPLOYMENT_DIR}/${FILES_DEPLOYMENT_ROOT}"/>
		<property name="SRC_HOME" value="${basedir}/src/ear"/>
		<property name="SRC_CWSPORTALAPP_HOME" value="${SRC_HOME}/cwsportalapp"/>
		<property name="SRC_SECURITY_HOME" value="${SRC_HOME}/cwssecurity"/>
		<property name="DOMAIN_HOME" value="${basedir}/user_projects/domains/cwsdomain"/>		
		<property name="SECURITY_TAR_LOC" value="${EAR_HOME}/extlib"/>

		
    </target>

    <target name="init_build_cws_dev" depends="init">
        <property name="BASE_DIR" value="./"/>
        <property name="BASE_PAGES_DIR" value=".."/>
        <property name="JARS_LOCATION" value="../appclasses"/>
        <property name="DEPLOY_IN_FOLDER" value="${UNIX_HOME}/${FILES_DEPLOYMENT_ROOT}"/>		
        <property name="USE_SECURITY" value="true"/>
        <property name="WORKING_DIR" value="${DEPLOYMENT_DIR}/${FILES_DEPLOYMENT_ROOT}"/>
		<property name="SECURITY_JAR_DIR" value="${SRC_SECURITY_HOME}/lib"/>
		<property name="WORKING_DIR_CWS" value="${SRC_CWSPORTALAPP_HOME}/cwsapp/install/classes"/>
		<property name="CWS_BUILT_EXPLODED" value="${DEPLOYMENT_DIR}/${FILES_DEPLOYMENT_ROOT}/apps/exploded/cwsportalapp"/>
		<property name="CWS_BUILT_EXPLODED_TAR" value="${DEPLOYMENT_DIR}/${FILES_DEPLOYMENT_ROOT}/apps/tar"/>
		<property name="DOMAIN_BUILT_EXPLODED_DEPLOYMENT" value="${DEPLOYMENT_DIR}/${FILES_DEPLOYMENT_ROOT}/domain"/>
		<property name="DOMAIN_BUILT_EXPLODED" value="${DOMAIN_HOME}/../"/>
    </target>

    <target name="init_build_domain_dev" depends="init">
        <property name="BASE_DIR" value="./"/>
        <property name="DEPLOY_IN_FOLDER" value="${UNIX_HOME}/${FILES_DEPLOYMENT_ROOT}"/>
		<property file="${basedir}/build/properties/cws_dev.properties"/>
        <property name="USE_SECURITY" value="true"/>
        <property name="WORKING_DIR" value="${DEPLOYMENT_DIR}/${FILES_DEPLOYMENT_ROOT}"/>
    </target>

    <!-- clean target delete all output files of this project -->
    <target name="clean_all" depends="init">
        <delete dir="${EAR_HOME}/apps/ear" failonerror="false"/>
        <delete dir="${EAR_HOME}/extlib" failonerror="false"/>
    </target>

	<target name="clean_security_tar" depends="init">        
        <delete dir="${SECURITY_TAR_LOC}" failonerror="false"/>
    </target>

	<target name="clean_cws_tar" depends="init">        
        <delete dir="${EAR_HOME}/apps/ear" failonerror="false"/>
    </target>

    <target name="svn_update_cwsportal" depends="init">
	<echo message="inside svn_update_cwsportal"/>
		<svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
		  <export srcUrl="${SVNROOT}/cwsportalapp" revision="HEAD" destPath="${SRC_HOME}" />
		</svn>        
    </target>

    <target name="svn_update_security" depends="init">
	<echo message="inside svn_update_security"/>
		<svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
			<export srcUrl="${SVNROOT}/cwssecurity" revision="HEAD" destPath="${SRC_HOME}" />
		</svn> 
    </target>
	
    <target name="svn_update_domain" depends="init">
	<echo message="inside svn_update_domain"/>
		<svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
			<export srcUrl="${SVNROOT}/cwsdomain" revision="HEAD" destPath="${SRC_HOME}" />
		</svn>         
    </target>

	<target name="svn_full_clean_checkout_cwsportal" depends="init_build_cws_dev">
	<echo message="inside svn_update_domain"/>
       <tstamp/>
       <echo level="info" message="TODAY value: ${TODAY}" />
       <delete dir="${SRC_CWSPORTALAPP_HOME}" failonerror="false"/>
	   <svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
			<checkout url="${SVNROOT}/cwsportalapp" revision="HEAD" destPath="${SRC_HOME}" />
		</svn>  
	</target>

	<target name="svn_full_clean_checkout_security" depends="init_build_cws_dev">
       <delete dir="${SRC_SECURITY_HOME}" failonerror="false"/>
	   <svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
			<checkout url="${SVNROOT}/cwssecurity" revision="HEAD" destPath="${SRC_HOME}" />
		</svn>  	   
	</target>

	<target name="svn_full_clean_checkout_domain" depends="init_build_cws_dev">
       <delete dir="${DOMAIN_HOME}" failonerror="false"/>
	   <svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
			<checkout url="${SVNROOT}/cwsdomain" revision="HEAD" destPath="${SRC_HOME}" />
		</svn> 
	</target>


    <target name="svn_full_checkout" depends="init_build_cws_dev">
		<echo level="info" message="src folder is ${env.DELETE_SRC_DIR}"/>   
		<svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
		<checkout url="${SVNROOT}/cwssecurity" revision="HEAD" destPath="${SRC_HOME}" />
		<checkout url="${SVNROOT}/cwsportalapp" revision="HEAD" destPath="${SRC_HOME}" />
		<checkout url="${SVNROOT}/cwsdomain" revision="HEAD" destPath="${SRC_HOME}" />
		</svn>
    </target>

    <target name="svn_update_by_tag" depends="init_build_cws_dev">
        <echo message="SVS checkout in progress ... using SVN tag : ${env.SVN_TAG}"/>
		<svn username="${SVN_USER}" password="${SVN_PASSWORD}" javahl="false">
		<checkout url="${SVNROOT}/cwssecurity" revision="HEAD" destPath="${SRC_HOME}" />
		<checkout url="${SVNROOT}/cwsportalapp" revision="HEAD" destPath="${SRC_HOME}" />
		<checkout url="${SVNROOT}/cwsdomain" revision="HEAD" destPath="${SRC_HOME}" />
		</svn>
    </target>

    <target
        name="clean_compile_all"
        depends="clean_all, svn_full_checkout, compile_all"
        description="wipes everything out, compiles"/>

    <target name="compile_all" depends="init" description="compiles">
        <echo level="info" message="Debug session is ${DEBUG}"/>
		<ant antfile="${SRC_CWSPORTALAPP_HOME}/build.xml" dir="${SRC_CWSPORTALAPP_HOME}/" target="all"/>
    </target>

	<target name="unzip_cws" depends="init_build_cws_dev">
		<unzip src="${WORKING_DIR_CWS}/cwsportalapp.ear" dest="${CWS_BUILT_EXPLODED}"/>
	</target>

	<target name="ftp_package_from_devapp1_to_devapp2" depends="init_build_cws_dev">
        <ftp server="devpbgapp2" userid="weblogic" password="logic05"
            remotedir="/export/home/weblogic/packaging" ignoreNoncriticalErrors="yes" action="put" binary="yes" >
            <fileset dir="/export/home/weblogic/packaging"/>
        </ftp>
	</target>

	<!-- Copies the generated domain from build area to deployment/domain folder -->
	<target name="copy_domain" depends="init_build_cws_dev">
		<echo message="--copying domain--"/>
		<copy todir="${DOMAIN_BUILT_EXPLODED_DEPLOYMENT}">
			<fileset dir="${DOMAIN_BUILT_EXPLODED}"/>
		</copy>		
	</target>

	<!-- Copies the generated security jar files from $SECURITY_JAR_DIR area to deployment/extlib folder -->
	<target name="copy_security" depends="init_build_cws_dev">
		<copy todir="${SECURITY_TAR_LOC}">
			<fileset dir="${SECURITY_JAR_DIR}"/>
		</copy>		
	</target>

	<target name="tar_security" depends="init">
        <property name="TAR_FILENAME" value="${TAR_FILE_NAME_SECURITY}.tar"/>
        <property name="TAR_FILENAME_WITH_GZ" value="${TAR_FILENAME}.gz"/>
        <property name="TAR_FILE_ON_DESKTOP" value="${SECURITY_TAR_LOC}/${TAR_FILENAME_WITH_GZ}"/>
        
		<!-- Delete any existing tar file and prepare it. Exclude script files. These will be be transferred in ascii mode -->
        <delete file="${TAR_FILE_ON_DESKTOP}" failonerror="false"/>
		<mkdir dir="${SECURITY_TAR_LOC}"/>
        <tar destfile="${TAR_FILE_ON_DESKTOP}" compression="gzip">
            <tarfileset dir="${SECURITY_JAR_DIR}">
                <include name="**/*.*"/>
                <exclude name="*.sh"/>
            </tarfileset>
        </tar>
	</target>

	<target name="tar_cws" depends="init_build_cws_dev">
        <property name="TAR_FILENAME" value="${TAR_FILE_NAME_CWS}.tar"/>
        <property name="TAR_FILENAME_WITH_GZ" value="${TAR_FILENAME}.gz"/>
        <property name="TAR_FILE_ON_DESKTOP" value="${CWS_BUILT_EXPLODED_TAR}/${TAR_FILENAME_WITH_GZ}"/>
		
		<!-- Delete any existing tar file and prepare it. Exclude script files. These will be be transferred in ascii mode -->
        <delete file="${TAR_FILE_ON_DESKTOP}" failonerror="false"/>
		<mkdir dir="${DEPLOYMENT_DIR}"/>
        <antcall target="unzip_cws"/>
	<tar destfile="${TAR_FILE_ON_DESKTOP}" compression="gzip">
            <tarfileset dir="${CWS_BUILT_EXPLODED}">
                <include name="**/*.*"/>
                <exclude name="*.sh"/>
                <exclude name="*.mov"/>
                <exclude name="**/cwsportalapp/media/images/**, **/cwsportalapp/media/resources/**, **/cwsportalapp/media/styles/**, **/cwsportalapp/media/resources/video/**"/>
            </tarfileset>
        </tar>
	</target>

	<target name="tar_domain" depends="init_build_cws_dev">
        <property name="TAR_FILENAME" value="${TAR_FILE_NAME_DOMAIN}.tar"/>
        <property name="TAR_FILENAME_WITH_GZ" value="${TAR_FILENAME}.gz"/>
        <property name="TAR_FILE_ON_DESKTOP" value="${DOMAIN_BUILT_EXPLODED_DEPLOYMENT}/${TAR_FILENAME_WITH_GZ}"/>
		
		<!-- Delete any existing tar file and prepare it. Exclude script files. These will be be transferred in ascii mode -->
        <delete file="${TAR_FILE_ON_DESKTOP}" failonerror="false"/>
		<mkdir dir="${DEPLOYMENT_DIR}"/>
		<tar destfile="${TAR_FILE_ON_DESKTOP}" compression="gzip">
            <tarfileset dir="${DOMAIN_BUILT_EXPLODED}">
                <include name="**/*.*"/>
                <exclude name="*.sh"/>
            </tarfileset>
        </tar>
	</target>

    <target name="changeRightsForDomain" depends="init_build_cws_dev">
		<chmod dir="${DOMAIN_BUILT_EXPLODED_DEPLOYMENT}/cwsdomain" perm="ugo+rx" includes="**/*.sh.*"/>
    </target>

    <target name="Run Unix Command">
	<property name="DEPLOY_ON_SERVER" value="weblogic"/>
	<property name="DEPLOY_WITH_USERID" value="weblogic"/>
	<property name="DEPLOY_WITH_PASSWORD" value="logic05"/>
	<property name="DEPLOY_IN_FOLDER" value="${TAR_FILENAME}.gz"/>

        <telnet server="${DEPLOY_ON_SERVER}" userid="${DEPLOY_WITH_USERID}" password="${DEPLOY_WITH_PASSWORD}">
            <read string="${UNIX_HOME}"/>
            <write string="cd ${DEPLOY_IN_FOLDER}"/>
            <read string="${DEPLOY_IN_FOLDER}"/>
            <write string="${command} > /dev/null"/>
            <read string="${DEPLOY_IN_FOLDER}"/>
            <write>exit</write>
        </telnet>
    </target>

    <target name="build_security">			
		<echo level="info" message="SRC_SECURITY_HOME folder is ${SRC_SECURITY_HOME}"/>
 		<antcall target="clean_security_tar"/>
        <antcall target="svn_update_security"/>	        
		<ant antfile="${SRC_SECURITY_HOME}/build.xml" dir="${SRC_SECURITY_HOME}/" target="all"/>
		<antcall target="copy_security"/>
    </target>

	<target name="replaceUrlRewriteXml" depends="init">
		<echo level="info" message="Replacing cwsextportal.portal references"/>
		<replaceregexp file="${SRC_CWSPORTALAPP_HOME}/contentredirector/WEB-INF/urlrewrite.xml"
                         match="cwsextportal.portal"
                         replace="appmanager/\pbgportal/\extdesktop"
						 flags="g"
                         byline="true"/>
        </target>
	<target name="build_cws">
		<echo level="info" message="SRC_CWSPORTALAPP_HOME folder is ${SRC_CWSPORTALAPP_HOME}"/>
 		<antcall target="clean_cws_tar"/>
        <antcall target="svn_full_clean_checkout_cwsportal"/>
        <!---antcall target="cvs_update_cwsportal" /-->
		<antcall target="replaceUrlRewriteXml"/>
        <ant antfile="${SRC_CWSPORTALAPP_HOME}/build.xml" dir="${SRC_CWSPORTALAPP_HOME}/" target="all"/>		
		<antcall target="tar_cws"/>
    </target>

	<target name="build_domain">	
		<echo level="info" message="base dir is ${basedir}"/>
  		<ant antfile="${DOMAIN_HOME}/build.xml" dir="${DOMAIN_HOME}/" target="purge"/>
    <!--antcall target="cvs_update_domain" /-->
        <antcall target="svn_full_clean_checkout_domain"/>
		<ant antfile="${DOMAIN_HOME}/build.xml" dir="${DOMAIN_HOME}/" target="setup.domain"/>
		<antcall target="copy_domain"/>
		<antcall target="changeRightsForDomain"/>		
	</target>

	<target name="build_cws_dev" depends="init_build_cws_dev, build_cws"/>	
	<target name="build_security_dev" depends="init_build_cws_dev, build_security"/>		
	<target name="build_domain_dev" depends="init_build_domain_dev,build_domain"/>	

	<target name="build_all" depends="init">
		<antcall target="build_domain_dev"/>
		<antcall target="build_security_dev"/>
		<antcall target="build_cws_dev"/>
	</target>


</project>
