// Copyright 2016-2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed 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.
// internal plugins

apply plugin: 'java-library'
apply plugin: 'eclipse' // Eclipse integration

description = """"""
def version_str = "4.2.0"

import org.apache.tools.ant.taskdefs.condition.Os;
import org.gradle.api.tasks.options.Option;

/**
 * Task class to install fastddsgen in a specific path
 * @author eProsima
 * @author Nuno Marques <nuno.marques@dronesolutions.io>
 * @author Miguel Company <MiguelCompany@eprosima.com>
 * @version 2.0.0
 * @since 2020-03-31
 */
public class InstallPath extends DefaultTask {
    private String path = "";

    /**
     * Setter for the install path
     * @param path install path given by the task option "install_path"
     */
    @Option(option = "install_path", description = "Absolute path to the install dir")
    public void setInstallPath(String path) {
        this.path = path;
    }

    /**
     * Getter of the install path
     */
    @Input
    public String getInstallPath() {
        return path;
    }

    /**
     * Uses Gradle project Copy() to copy files
     * @param input file to copy
     * @param ouput destination directory
     */
    private void copy(String input, String output) {
        project.copy {
            from project.file(input)
            into project.file(output)
        }
    }

    /**
     * Install Task Action that copies the script and jar files
     */
    @TaskAction
    private void install() {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            final String install_path = (path != null && !path.isEmpty()) ? path : "C:\\Program Files\\fastddsgen\\";

            // Copy script
            copy("scripts\\fastddsgen.bat", new File(install_path, "bin\\").getAbsolutePath())
            // Copy jar
            copy("share\\fastddsgen\\java\\fastddsgen.jar", new File(install_path, "share\\fastddsgen\\java\\").getAbsolutePath())

        } else if (Os.isFamily(Os.FAMILY_UNIX) || Os.isFamily(Os.FAMILY_MAC)) {
            final String install_path = (path != null && !path.isEmpty()) ? path : "/usr/local/";

            // Copy script
            copy("scripts/fastddsgen", new File(install_path, "bin/").getAbsolutePath())
            // Copy jar
            copy("share/fastddsgen/java/fastddsgen.jar", new File(install_path, "share/fastddsgen/java/").getAbsolutePath())
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation files('thirdparty/idl-parser/build/libs/idlparser-4.2.0.jar')
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.2')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.5.2')
}

task submodulesUpdate(type: Exec) {
    description 'Updates (and inits) git submodules'
    commandLine 'git', 'submodule', 'update', '--init'
}

task buildIDLParser(type: GradleBuild) {
    dir = 'thirdparty/idl-parser'
    tasks = ['clean', 'build']
}
buildIDLParser.dependsOn submodulesUpdate

task copyResources {
    // Copy platforms file
    File outputDir = file(new File("${project.buildDir}", "resources/main"))
    outputDir.exists() || outputDir.mkdirs()
    File versionFile = new File("${project.buildDir}/resources/main/version")
    outputs.file versionFile
    doLast {
        // Create version file
        Properties pversion = new Properties()
        project.hasProperty('customversion')
            ? pversion.setProperty('version', project.customversion.toString())
            : pversion.setProperty('version', version_str)
        versionFile.withWriter {
            pversion.store(it, null)
        }
    }

}

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }

        resources {
            srcDir 'src/main/java'
            include '**/*.stg'
        }
    }

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

jar {
    duplicatesStrategy 'exclude'
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    archiveBaseName = 'fastddsgen'
    manifest {
        attributes(
            "Created-By": "eProsima",
            "Main-Class": "com.eprosima.fastdds.fastddsgen",
            "Class-Path": configurations.runtimeClasspath.collect { it.getName() }.join(' ')
            )
    }
    doLast
    {
        copy {
            from jar
            into "share/fastddsgen/java"
        }
    }
}

compileJava.dependsOn buildIDLParser,copyResources
compileJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    options.compilerArgs.add('-Xlint:deprecation')
    options.compilerArgs.add('-Xlint:unchecked')
}

task install(type: InstallPath, group: "Install", description: "Install fastddsgen script and fastddsgen.jar")
install.dependsOn assemble

test {
    useJUnitPlatform()
    testLogging.showStandardStreams = true
    systemProperties = System.getProperties()
}
