/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.4.2/userguide/building_java_projects.html
 */
plugins {
    id 'application'
        id 'eclipse'
        id 'java'
        id 'com.github.johnrengelman.shadow' version "7.1.2"
        id 'org.beryx.runtime' version '1.12.7'
        id "com.github.langmo.gradlensis" version "0.1.0"
}

def os = System.getProperty("os.name").toLowerCase()
if (os.contains("windows")) os='windows'
else if (os.contains("linux")) os='linux'
else if (os.contains("mac")) os='mac'
 
repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

java {
        sourceCompatibility = JavaVersion.toVersion("17")
        targetCompatibility = JavaVersion.toVersion("17")
}

dependencies {
    // Use TestNG framework, also requires calling test.useTestNG() below
    testImplementation 'org.testng:testng:7.4.0'

        annotationProcessor group: 'org.projectlombok'                 ,name: 'lombok'                         ,version: '1.18.24'

        implementation 'com.google.guava:guava:31.1-jre'
        implementation 'info.picocli:picocli:4.6.3'
        implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
        implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '5.12.1'
        implementation group: 'com.igormaznitsa', name: 'jbbp', version: '2.0.4'
        implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.4.0'
        implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.4.0'

        implementation('org.slf4j:slf4j-api:2.0.0')
        implementation group: 'org.slf4j', name: 'slf4j-ext', version: '2.0.0'

        implementation group: 'org.eclipse.platform', name: 'org.eclipse.swt', version: '3.120.0'
        implementation group: 'org.eclipse.platform', name: 'org.eclipse.core.runtime', version: '3.25.0'
        implementation group: 'org.apache.commons', name: 'commons-compress', version: '1.21'
        implementation group: 'org.rauschig', name: 'jarchivelib', version: '1.2.0'
        implementation group: 'org.eclipse.platform', name: 'org.eclipse.core.commands', version: '3.10.200'
        implementation group: 'org.eclipse.platform', name: 'org.eclipse.jface', version: '3.26.0'
        implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '6.3.0.202209071007-r'
        implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit.archive', version: '6.3.0.202209071007-r'

        implementation group: 'net.dongliu', name: 'apk-parser', version: '2.6.10'
        implementation group: 'org.riversun', name: 'bigdoc', version: '0.3.0'
        implementation group: 'org.lz4', name: 'lz4-java', version: '1.8.0'
        implementation group: 'org.bidib.com.github.markusbernhardt', name: 'proxy-vole', version: '1.0.17'

        implementation group: 'org.bouncycastle', name: 'bcutil-jdk18on', version: '1.71'
        implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6.1'
        implementation 'net.fornwall:jelf:0.7.0'
        implementation group: 'com.turn', name: 'ttorrent-core', version: '1.5'
        implementation group: 'org.apache-extras.beanshell', name: 'bsh', version: '2.0b6'

        compileOnly         group: 'org.projectlombok'                 ,name: 'lombok'                         ,version: '1.18.24'

}

configurations.all {
        resolutionStrategy {
                dependencySubstitution {
                        if (os.contains("windows")) {
                                substitute module('org.eclipse.platform:org.eclipse.swt.${osgi.platform}') with module("org.eclipse.platform:org.eclipse.swt.win32.win32.x86_64:3.120.0")
                        }
                        else if (os.contains("linux")) {
                                substitute module('org.eclipse.platform:org.eclipse.swt.${osgi.platform}') with module("org.eclipse.platform:org.eclipse.swt.gtk.linux.x86_64:3.120.0")
                        }
                        else if (os.contains("mac")) {
                                substitute module('org.eclipse.platform:org.eclipse.swt.${osgi.platform}') with module("org.eclipse.platform:org.eclipse.swt.cocoa.macosx.x86_64:3.120.0")
                        }
                }
        }
}

nsis {
        // Enter here the configuration settings of the gradle-nsis plugin.
        // Note: a task "createInstaller" is automatically generated.

        // path to the NSIS configuration file
        configuration = file("${projectDir}/nsis/setup.nsi")

        variables = ['VERSION':"${flashtoolVersion}"]
        // folder where the NSIS installer generator is run. All paths
        // in the NSIS configuration file are relative to this folder.
        runIn =  file("${projectDir}/build/install/Flashtool")
}

distributions {
    main {
        contents {
            from ('src/main/resources/native/'+os) { 
                if (os.contains("linux") || os.contains("mac")) {
					fileMode 0755
				}
			}
			if (os.contains("mac")) {
               from ('src/main/resources/custom') { into 'FlashTool.app/Contents/MacOS/custom' }
 			   from(jre) { into 'FlashTool.app/Contents/MacOS/jre' }
			} else {
               from ('src/main/resources/custom') { into 'custom' }
 			   from(jre) { into 'jre' }
 			}
            if (os.contains("windows")) {
                from (project(':FlashtoolDrivers').buildDir.toString()+'/distributions') { into 'drivers' }
            }
        }
    }
}

[distZip, distTar, installDist, createInstaller]*.dependsOn(':FlashtoolDrivers:createInstaller')

[createInstaller]*.dependsOn('installDist')

installDist.doLast() {
        if (os.contains("mac")) {
           def dist = "${buildDir}/install/${project.name}"
           ant.move(file: "$dist/lib", toFile: "$dist/FlashTool.app/Contents/MacOS/lib")
           ant.symlink(resource: "$dist/FlashTool.app/Contents/MacOS/x10flasher_native/udev/libusbx-1.0.0.dylib", link: "$dist/FlashTool.app/Contents/MacOS/x10flasher_native/udev/libusbx-1.0.dylib")
        }
}

task createDMG() {
        doLast() {
        if (os.contains("mac")) {
           def inst = "${buildDir}/install/${project.name}/"
           def dist = "${buildDir}/distributions/"
           new File(dist).mkdirs()
           def command="hdiutil create -fs HFS+ -srcfolder ${inst} -volname FlashTool ${dist}/${project.name}-${flashtoolVersion}-${os}.dmg"
           println command
           command.execute()
        }
        }
}

createDMG.dependsOn('installDist')

distTar.doLast() {
        def dist = "${buildDir}/distributions/"
        ant.bzip2(src: "${dist}/Flashtool.tar", destfile: "${dist}/${project.name}-${flashtoolVersion}-${os}.tar.bz2")
        delete "${dist}/Flashtool.tar"
}

jar {
        processResources {
                exclude("native/**")
                exclude("custom/**")
                exclude("launch4j/**")
        }
        manifest {
                attributes('Implementation-Title'   : 'Flashtool',
                               'Implementation-Date'    : "${flashtoolReleaseDate}",
                                   'Implementation-Version' : "${flashtoolVersion}",
                                   'Internal-Version'       : "${flashtoolVersion}",
                                   'Internal-Date'          : "${flashtoolReleaseDate}",
                                   'Internal-Channel'       : 'release'
                                   )
        }
}

application {
    // Define the main class for the application.
    mainClass = 'org.flashtool.gui.Main'
        applicationDefaultJvmArgs = ['-Dlog4j2.Script.enableLanguages=bsh','-Xms256m','-Xmx1024m','-Duser.country=US','-Duser.language=en','-Djsse.enableSNIExtension=false']
        startScripts.enabled = false
}


runtime {
        options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
        modules = ['java.base','java.compiler','java.datatransfer','java.desktop','java.instrument','java.logging','java.management','java.naming','java.prefs','java.rmi','java.scripting','java.security.jgss','java.security.sasl','java.sql','java.transaction.xa','java.xml','jdk.attach','jdk.jdi','jdk.unsupported','jdk.xml.dom','jdk.security.auth','jdk.security.jgss','jdk.crypto.cryptoki','jdk.crypto.ec','jdk.management']
        imageDir = file("$buildDir/image/jre")
}

testing {
    suites {
        // Configure the built-in test suite
        test {
            // Use TestNG test framework
            useTestNG('7.4.0')
        }
    }
}
