<?xml version="1.0" encoding="UTF-8"?>

<project name="phase-package" default="execute">

  <condition property="checkstyle.ant.skip">
    <isset property="checkstyle.ant.skip" />
  </condition>

  <target name="execute" unless="checkstyle.ant.skip">
    <taskdef name="checkstyle"
             classname="com.puppycrawl.tools.checkstyle.ant.CheckstyleAntTask"
             classpath="${mvn.runtime_classpath}"
             />

    <property name="checkstyle.pattern.todo" value="NOTHingWillMatCH_-"/>
    <property name="check.config" location="config/checkstyle_checks.xml"/>
    <property name="check.config_non_main"
              location="config/checkstyle_non_main_files_checks.xml"/>
    <property name="check.config_resources"
              location="config/checkstyle_resources_checks.xml"/>

    <tstamp>
      <format property="STARTED" pattern="dd/MM/yyyy hh:mm:ss aa" />
    </tstamp>
    <echo>Checkstyle started (checkstyle_checks.xml): ${STARTED}</echo>

    <checkstyle config="${check.config}"
                failOnViolation="false"
                failureProperty="checkstyle.failure.property"
                executeIgnoredModules="true"
                >
      <fileset dir="src"
               includes="**/*"
               excludes="it/resources/**/*,it/resources-noncompilable/**/*,
               ,test/resources/**/*,test/resources-noncompilable/**/*"/>
      <formatter type="plain"/>
      <formatter type="xml" toFile="${mvn.project.build.directory}/cs_errors.xml"/>
      <classpath path="${mvn.runtime_classpath}"/>
      <property key="checkstyle.cache.file" file="${mvn.project.build.directory}/cachefile"/>
      <property key="checkstyle.header.file" file="config/java.header"/>
      <property key="checkstyle.regexp.header.file" file="config/java_regexp.header"/>
      <property key="checkstyle.importcontrol.file" file="config/import-control.xml"/>
      <property key="checkstyle.importcontroltest.file" file="config/import-control-test.xml"/>
      <property key="checkstyle.suppressions.file"
                file="config/suppressions.xml"/>
      <property key="checkstyle.suppressions-xpath.file"
                file="config/suppressions-xpath.xml"/>
    </checkstyle>

    <tstamp>
      <format property="FINISHED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
    </tstamp>
    <echo>Checkstyle finished (checkstyle_checks.xml) : ${FINISHED}</echo>

    <tstamp>
      <format property="STARTED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
    </tstamp>
    <echo>Checkstyle started (checkstyle_non_main_files_checks.xml): ${STARTED}</echo>

    <checkstyle config="${check.config_non_main}"
                failOnViolation="true"
                failureProperty="checkstyle.failure.property"
                executeIgnoredModules="true"
                >
      <path>
        <fileset dir="${basedir}" includes="**/*">
          <exclude name=".git/**/*"/>
          <exclude name="src/main/**/*"/>
          <exclude name="src/test/java/**/*"/>
          <exclude name="src/site/resources/images/**/*"/>
          <!-- styleguides are copied/cached from origin place we should not change them  -->
          <exclude name="src/site/resources/styleguides/**/*"/>

          <!-- Eclipse project files -->
          <exclude name=".settings/**/*"/>
          <exclude name=".externalToolBuilders/**/*"/>
          <exclude name=".classpath"/>
          <exclude name=".project"/>

          <!-- m2e-code-quality Eclipse IDE plugin temporary configuration files
               for Eclipse CS Checkstyle / PMD / SpotBugs Plug-Ins -->
          <exclude name=".checkstyle"/>
          <exclude name=".pmd/**/*"/>
          <exclude name=".pmdruleset.xml"/>
          <exclude name=".fbExcludeFilterFile/**/*"/>

          <!-- NetBeans project files -->
          <exclude name="**/nbactions.xml"/>
          <exclude name="**/nb-configuration.xml"/>

          <!-- Maven build folder -->
          <exclude name="target/**/*" />
          <exclude name="bin/**/*" />

          <!-- IDEA project files -->
          <exclude name="*.iml"/>
          <exclude name="*.ipr"/>
          <exclude name="*.iws"/>
          <exclude name=".idea/**/*"/>

          <!-- Temp files -->
          <exclude name="*~" />

          <!-- Java Virtual machine crash logs -->
          <exclude name="hs_err_pid*"/>
          <exclude name="replay_pid*"/>

          <!-- Apple MAC OSX hidden file -->
          <exclude name=".DS_Store" />

          <!-- NonDex files -->
          <exclude name=".nondex/**/*" />

          <!-- Do not validate possible source code remnants after regression testing -->
          <exclude name=".ci-temp/**/*"/>

        </fileset>
      </path>
      <formatter type="plain"/>
      <formatter type="xml" toFile="${mvn.project.build.directory}/cs_non_main_files_errors.xml"/>
      <classpath path="${mvn.runtime_classpath}"/>
    </checkstyle>

    <tstamp>
      <format property="FINISHED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
    </tstamp>
    <echo>Checkstyle finished (checkstyle_non_main_files_checks.xml): ${FINISHED}</echo>

    <tstamp>
      <format property="STARTED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
    </tstamp>
    <echo>Checkstyle started (checkstyle_resources_checks.xml): ${STARTED}</echo>

    <checkstyle config="${check.config_resources}"
                failOnViolation="true"
                failureProperty="checkstyle.failure.property"
                executeIgnoredModules="true"
                >
      <path>
        <fileset dir="src"
            includes="it/resources/**/*,test/resources/**/*,test/resources-noncompilable/**/*">
         <!-- bad syntax, permanent exclude -->
         <exclude name="**/InputAstTreeStringPrinter.java" />
         <exclude name="**/InputSuppressionsStringPrinter.java"/>
         <exclude name="**/InputMainIncorrectClass.java"/>
         <exclude name="**/InputJavadocPropertiesGeneratorParseError.java"/>
         <exclude name="**/InputMainFrameModelIncorrectClass.java"/>
         <exclude name="**/InputBeforeExecutionExclusionFileFilterIncorrectClass.java"/>
         <exclude name="**/InputJavaParser.java"/>
        </fileset>
      </path>
      <formatter type="plain"/>
      <formatter type="xml" toFile="${mvn.project.build.directory}/cs_resources_errors.xml"/>
      <classpath path="${mvn.runtime_classpath}"/>
    </checkstyle>

    <tstamp>
      <format property="FINISHED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
    </tstamp>
    <echo>Checkstyle finished (checkstyle_resources_checks.xml): ${FINISHED}</echo>

    <fail if="checkstyle.failure.property"
          message="Checkstyle failed: ${checkstyle.failure.property}"
          />
  </target>
</project>
