/*
 * LunaticSMTP
 * Copyright (C) 2016  Anton Larionov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'nebula.ospackage' version '9.1.1'
    id "com.github.ben-manes.versions" version '0.44.0'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
    id 'org.owasp.dependencycheck' version '7.4.4'
}

repositories {
    mavenCentral()
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

javafx {
    version = "19"
    modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
}

dependencies {
    implementation 'org.slf4j:slf4j-api:2.0.5'
    implementation 'org.slf4j:slf4j-log4j12:2.0.5'

    implementation 'org.apache.commons:commons-lang3:3.12.0'
    implementation 'com.beust:jcommander:1.82'
    implementation 'org.subethamail:subethasmtp:3.1.7'
    implementation 'org.controlsfx:controlsfx:11.1.2'

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.testfx:testfx-core:4.0.16-alpha'
    testImplementation 'org.testfx:testfx-junit:4.0.15-alpha'
}

application {
    mainClass = 'com.gitlab.anlar.lunatic.Lunatic'
}

ext {
    applicationName = 'lunaticsmtp'
    baseVersion = '0.5.2'
    author = 'Anton Larionov'

    buildTime = new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
    gitRevision = 'git rev-parse HEAD'.execute().text.trim()
    gitShortRevision = 'git rev-parse --short HEAD'.execute().text.trim()
}

test {
    if (System.properties['test.profile'] == 'headless') {
        exclude '**/gui/*'
    }
}

task generateVersion {
    doLast {
        Properties props = new Properties()
        props.setProperty('version', baseVersion)
        props.setProperty('gitShortRevision', gitShortRevision)
        mkdir("$buildDir/resources/main")
        File propsFile = file("$buildDir/resources/main/version.properties")
        props.store(propsFile.newWriter(), null)
    }
}

jar {
    dependsOn generateVersion

    manifest {
        attributes(
                'Created-By': "${System.getProperty("java.runtime.version")} (${System.getProperty("java.vm.vendor")})",
                'Build-Time': buildTime,

                'Specification-Title': applicationName,
                'Specification-Version': baseVersion,
                'Specification-Vendor': author,

                'Implementation-Title': applicationName,
                'Implementation-Version': baseVersion + '-' + gitRevision,
                'Implementation-Vendor': author,

                'Main-Class': application.mainClass
        )
    }
}

shadowJar {
    dependsOn generateVersion

    archiveBaseName = applicationName
    archiveClassifier = null
    archiveVersion = 'v' + baseVersion

    dependencies {
        exclude(dependency('org.openjfx::'))
    }

    exclude '**/*.java'
}

task releaseDeb(type: Deb, dependsOn: 'shadowJar', description: 'Assembles deb-package.') {
    packageName = applicationName
    version = baseVersion
    release = 1
    user = 'root'
    permissionGroup = 'root'
    license = 'GPLv3'
    maintainer = author + ' <contact-via-project-issue-tracker@github.com>'
    url = 'https://github.com/anlar/lunaticsmtp'
    summary = 'dummy SMTP server with GUI'
    packageDescription = 'Simple JavaFX SMTP server for testing email sending applications.'
    requires('default-jre', '2:1.11', GREATER | EQUAL).or('java11-runtime')
    requires('openjfx')
    customField([
            'Vcs-Browser': 'https://github.com/anlar/lunaticsmtp',
    ])
 
    from("$buildDir/libs/$applicationName-v${baseVersion}.jar") {
        into '/usr/share/lunaticsmtp'
        rename("$applicationName-v${baseVersion}.jar", "${applicationName}.jar")
        fileMode 0644
    }
    from("../package/linux/lunaticsmtp") {
        into '/usr/bin'
        fileMode 0755
    }
    from("../package/linux/lunaticsmtp.desktop") {
        into '/usr/share/applications'
        fileMode 0644
    }
    from("../package/linux/lunaticsmtp.png") {
        into '/usr/share/pixmaps'
        fileMode 0644
    }
    from("../package/linux/copyright") {
        into "/usr/share/doc/$packageName"
        fileMode 0644
    }
    from("../package/linux/lunaticsmtp.1") {
        into "/usr/share/man/man1"
        fileMode 0644
    }
}

tasks.named("dependencyUpdates").configure {
    // Ignore openjfx EA updates
    rejectVersionIf {
        return (it.candidate.group == 'org.openjfx' && it.candidate.version.contains('ea'))
    }
}

dependencyCheck {
    failBuildOnCVSS = 0
}

