<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project basedir="." name="apache-cassandra--rat-tasks"
     xmlns:rat="antlib:org.apache.rat.anttasks">

    <!--
      License audit tool
    -->

    <target name="_rat_init" depends="resolver-init">
        <typedef uri="antlib:org.apache.rat.anttasks" classpathref="rat.classpath"/>
    </target>

    <target name="_build_ratinclude" depends="_rat_init">
        <exec executable="git" failifexecutionfails="false" failonerror="false" resultproperty="git.success" output="build/.versioned_files">
            <arg line="ls-tree -r HEAD --name-only"/>
        </exec>
        <condition property="rat.skip" value="true">
            <not>
                <equals arg1="${git.success}" arg2="0"/>
            </not>
        </condition>
        <copy file="build/.versioned_files" tofile="build/.ratinclude">
            <filterchain>
                <linecontainsregexp>
                    <regexp pattern=".*\.(java|py|sh|xml|spec|md|iml|bat|btm|cql|css|g|html|jflex|jks|mod|name|pom|textile|yml|yaml|MIDRES|HIGHRES|LOWRES)$"/>
                </linecontainsregexp>
            </filterchain>
        </copy>
    </target>

    <target name="rat-check" depends="_build_ratinclude" unless="${rat.skip}" description="License checks on source" >
        <rat:report reportFile="${build.dir}/rat.txt">
            <fileset dir="." includesfile="build/.ratinclude">
                 <!-- Config files with not much creativity -->
                 <exclude name="**/ide/**/*"/>
                 <exclude name="**/metrics-reporter-config-sample.yaml"/>
                 <exclude name="**/cassandra.yaml"/>
                 <exclude name="**/cassandra-murmur.yaml"/>
                 <exclude name="**/cassandra-seeds.yaml"/>
                 <exclude NAME="**/doc/antora.yml"/>
                 <exclude name="**/test/conf/cassandra.yaml"/>
                 <exclude name="**/test/conf/cassandra_deprecated_parameters_names.yaml"/>
                 <exclude name="**/test/conf/cassandra_encryption.yaml"/>
                 <exclude name="**/test/conf/cdc.yaml"/>
                 <exclude name="**/test/conf/commitlog_compression_LZ4.yaml"/>
                 <exclude name="**/test/conf/commitlog_compression_Zstd.yaml"/>
                 <exclude name="**/test/conf/system_keyspaces_directory.yaml"/>
                 <exclude name="**/test/conf/sstableloader_with_encryption.yaml"/>
                 <exclude name="**/test/conf/unit-test-conf/test-native-port.yaml"/>
                 <exclude name="**/test/data/jmxdump/cassandra-3.0-jmx.yaml"/>
                 <exclude name="**/test/data/jmxdump/cassandra-3.11-jmx.yaml"/>
                 <exclude name="**/test/data/jmxdump/cassandra-4.0-jmx.yaml"/>
                 <exclude name="**/tools/cqlstress-counter-example.yaml"/>
                 <exclude name="**/tools/cqlstress-example.yaml"/>
                 <exclude name="**/tools/cqlstress-insanity-example.yaml"/>
                 <exclude name="**/tools/cqlstress-lwt-example.yaml"/>
                 <!-- Documentation files -->
                 <exclude NAME="**/doc/modules/**/*"/>
                 <!-- NOTICE files -->
                 <exclude NAME="**/NOTICE.md"/>
                 <!-- LICENSE files -->
                 <exclude NAME="**/LICENSE.md"/>
            </fileset>
        </rat:report>
        <exec executable="grep" outputproperty="rat.failed.files" failifexecutionfails="false">
            <arg line="-A5 'Unapproved licenses' ${build.dir}/rat.txt"/>
        </exec>
        <fail message="Some files have missing or incorrect license information. Check RAT report in ${build.dir}/rat.txt for more details! \n ${rat.failed.files}">
            <condition>
                <and>
                    <not>
                        <resourcecontains resource="${build.dir}/rat.txt" substring="0 Unknown Licenses" casesensitive="false" />
                    </not>
                </and>
            </condition>
        </fail>
    </target>

    <target name="_assert_rat_output">
        <fail message="The rat report at build/rat.txt was not generated. Please ensure that the rat-check task is able to run successfully. For dev builds only, touch build/rat.txt to skip this check">
            <condition>
                <not>
                    <available file="build/rat.txt" />
                </not>
            </condition>
        </fail>
    </target>

</project>
