// 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.

//=============================================================================
// Plugins
//=============================================================================
plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '1.2.3'
    id 'com.google.protobuf' version '0.8.0'
}
apply plugin: 'checkstyle'
apply plugin: 'com.google.protobuf'

//=============================================================================
// Project Level Settings
//=============================================================================
version = 0.1
sourceCompatibility = 1.8
targetCompatibility = 1.8
def title = 'Apache Wave PST Compiler'
def vendor = 'The Apache Software Foundation'

jar {
    manifest {
        attributes "Specification-Vendor": vendor,
                "Specification-Title": title,
                "Specification-Version": version,
                "Implementation-Vendor": vendor,
                "Implementation-Title": title,
                "Implementation-Version": version,
                "Built-By": "No one in particular",
                "Main-Class": "org.apache.wave.pst.PstMain"
    }
}

tasks.withType(Tar) {
    compression = Compression.BZIP2
}

//=============================================================================
// Source's
//=============================================================================
sourceSets {
    main {
        java {
            srcDirs = [
                    'src/main/java',
                    'generated/main/java'
            ]
        }
        proto {
            srcDir 'src/main/proto'
        }
    }

    test {
        java {
            srcDir 'src/test/java'
        }
    }
}

//=============================================================================
// Dependencies
// Note: next to each dependency is a review stamp [last review, next review].
//       If a dependency is past its review date pls create a jira issue.
//       https://issues.apache.org/jira/browse/WAVE
//=============================================================================
repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.google.protobuf', name: 'protobuf-java', version: '2.6.1' // [12/2015, 3/2016]
    compile group: 'com.google.guava', name: 'guava', version: '19.0'             // [1/2016, 6/2016]
    compile group: 'org.antlr', name: 'antlr', version: '3.2'                     // [12/2015, 3/2016]
    compile group: 'commons-cli', name: 'commons-cli', version: '1.3.1'           // [1/2016, 6/2016]

    testCompile group: 'junit', name: 'junit', version: '4.12'                    // [1/2016, 1/2017]
}

//=============================================================================
// CheckStyle Config
//=============================================================================
checkstyle {
    toolVersion = "7.6.1"
    configFile = file("/google_checks.xml")
}

checkstyleMain.source = "src/main/java" // only checks the manually written java

//=============================================================================
// Protobuf Config
//=============================================================================
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:2.6.1'
    }
    generatedFilesBaseDir = "$projectDir/generated"
}

//=============================================================================
// UberJar (shadowJar) config
//=============================================================================

shadowJar {
    baseName = 'wave-pst'
    classifier = ''
    exclude 'META-INF/**/*'
}

//=============================================================================
// Clean - Must remove generated sources.
//=============================================================================
clean {
    delete "generated/"
}

//=============================================================================
// Source Distribution
//=============================================================================

def srcName = this.group + "-" + this.name + "-src"
def srcExcludes = [
        'build/*',
        '*.iml'
]

task createDistSourceTar(type: Tar) {
    baseName = srcName
    destinationDir = file('../distributions')
    from('./') {
        into 'apache-wave-pst-src'
    }
    excludes = srcExcludes

}

task createDistSourceZip(type: Zip) {
    baseName = srcName
    destinationDir = file('../distributions')
    from('./') {
        into 'apache-wave-pst-src'
    }
    excludes = srcExcludes
}

task createDistSource() {
    doFirst {
        println ''
        println '--------------------------------------------------------'
        println '     Creating Deployment Source - Apache Wave PST       '
        println '--------------------------------------------------------'
        println ''
    }
}

createDistSource.dependsOn createDistSourceTar, createDistSourceZip

//=============================================================================
// Binary Distribution
//=============================================================================

def binName = this.group + "-" + this.name + "-bin"

task createDistBinTar(type: Tar) {
    baseName = binName
    destinationDir = file('../distributions')
    from(shadowJar) {
        into 'apache-wave-pst/bin'
    }
    from('src/dist') {
        into 'apache-wave-pst'
    }
}

task createDistBinZip(type: Zip) {
    baseName = binName
    destinationDir = file('../distributions')
    from(shadowJar) {
        into 'apache-wave-pst/bin'
    }
    from('src/dist') {
        into 'apache-wave-pst'
    }
}

task createDistBin() {
    doFirst {
        println ''
        println '--------------------------------------------------------'
        println '     Creating Deployment Binary - Apache Wave PST       '
        println '--------------------------------------------------------'
        println ''
    }
}

createDistBin.dependsOn createDistBinTar, createDistBinZip

//=============================================================================
// Distribution's
//=============================================================================

task createDist() {
    doFirst {
        println ''
        println '--------------------------------------------------------'
        println '      Creating Deployments - Apache Wave PST            '
        println '--------------------------------------------------------'
        println ''
    }
}

createDist.dependsOn createDistSource, createDistBin