import org.apache.tools.ant.filters.ReplaceTokens
import java.nio.file.Files

plugins {
    id "com.github.jk1.dependency-license-report" version "0.3.5"
    id 'edu.sc.seis.launch4j' version '2.0.1'
}
apply plugin: 'java'
apply plugin: 'distribution'
apply plugin: 'maven'
apply plugin: 'idea'
apply from: "${project.rootDir}/gradle/eclipse.gradle"


sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'de.mediathekview'
version = '13.2.1'

def jarName = 'MediathekView.jar'
def mainClass = 'mediathek.Main'

ext {
    propsFile = file('src/main/resources/version.properties').absoluteFile
    if (!propsFile.exists()) {
        Files.createFile(propsFile.toPath())
    }
}

def loadVersionProperties() {
    Properties props = new Properties()
    props.load(propsFile.newDataInputStream())
    return props
}

compileJava {
    options.compilerArgs = ['-Xlint:all']
}
compileTestJava {
    options.compilerArgs = ['-Xlint:all']
}

repositories {
    mavenLocal()
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile ":jide-oss:3.7.4"
    compile "io.dropwizard.metrics:metrics-core:4.0.0"
    compile 'org.apache.commons:commons-lang3:3.6'
    compile 'com.google.guava:guava:26.0-jre'
    compile 'org.apache.commons:commons-compress:1.17'
    compile 'com.fasterxml.jackson.core:jackson-core:2.9.6'
    compile 'org.tukaani:xz:1.6'
    compile 'net.sf.jchart2d:jchart2d:3.3.2'
    compile 'org.apache.logging.log4j:log4j-core:2.8.1'
    compile 'net.engio:mbassador:1.3.0'
    compile 'org.controlsfx:controlsfx:8.40.14'
    compile 'com.h2database:h2:1.4.197'
    compile 'org.apache.commons:commons-configuration2:2.2'
    compile 'com.github.jiconfont:jiconfont-swing:1.0.1'
    compile 'com.github.jiconfont:jiconfont-font_awesome:4.7.0.0'
    compile 'com.nativelibs4java:bridj:0.7.0'
    compile 'com.miglayout:miglayout-swing:5.1'
    compile 'com.miglayout:miglayout-core:5.1'
    compile 'com.miglayout:miglayout-javafx:5.1'
    compile 'com.squareup.okhttp3:okhttp:3.11.0'
    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.5.0'

    compileOnly 'com.yuvimasory:orange-extensions:1.3.0'
    compileOnly group: 'org.jetbrains', name: 'annotations', version: '16.0.2'
}

build.dependsOn(install)

task updateVersion {
    doLast {
        Properties props = loadVersionProperties()

        def oldVersion = props.getProperty('VERSION')
        if (!oldVersion.equals(project.version)) {
            logger.lifecycle "==MediathekView======================"
            logger.lifecycle "Version: $project.version"
            logger.lifecycle "==MediathekView======================"
            props.setProperty('VERSION', project.version)
            props.store(propsFile.newWriter(), null)
        }
    }
}

processResources.dependsOn updateVersion

tasks.withType(Zip) { task ->
    task.doLast {
        ant.checksum(algorithm: 'SHA-512', file: it.archivePath)
    }
}
tasks.withType(Tar) { task ->
    task.doLast {
        ant.checksum(algorithm: 'SHA-512', file: it.archivePath)
    }
}

tasks.withType(Tar) {
    compression = Compression.GZIP
    extension = "tar.gz"
}

[distZip, distTar]*.shouldRunAfter compileJava, updateVersion, jar

jar {
    manifest {
        attributes(
            'SplashScreen-Image': 'mediathek/res/splash.png',
            'Main-Class': mainClass,
            'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
        )
    }
    archiveName = jarName
}

distributions {
    main {
        baseName = 'MediathekView'
        contents {
            into('lib') {
                from configurations.compile
            }
            from('res') {
                filesMatching('**/*.txt') {
                    filter(ReplaceTokens, tokens: [JARNAME: jarName])
                }
                filesMatching('**/*.sh') {
                    filter(ReplaceTokens, tokens: [JARNAME: jarName])
                }
            }
            from('build/libs') {
                include '*.jar'
            }
            from('build/launch4j') {
                include '*.exe'
                exclude 'lib'
            }
        }
    }
}

//Windows executable generation
launch4j {
    mainClassName = mainClass
    icon = "../../res/MediathekView.ico"
    jar = jarName
    dontWrapJar = true
    outfile = "MediathekView.exe"
    initialHeapSize = 256
    maxHeapSize = 1024
    maxHeapPercent = 80

}


task createIpv4Exe(type: edu.sc.seis.launch4j.tasks.Launch4jLibraryTask) {
    outfile = "MediathekView_ipv4.exe"
    opt = "-Djava.net.preferIPv4Stack=true"

    initialHeapSize = 256
    maxHeapSize = 1024
    maxHeapPercent = 80
}

task createPortableExe(type: edu.sc.seis.launch4j.tasks.Launch4jLibraryTask) {
    outfile = "MediathekView_Portable.exe"
    opt = "-DmvLogOutputPath=Einstellungen/.mediathek3/mediathekview.log"
    cmdLine = "Einstellungen/.mediathek3"

    initialHeapSize = 256
    maxHeapSize = 1024
    maxHeapPercent = 80
}

assemble.dependsOn createAllExecutables

/**
 * This is how you pass arguments: "./gradlew run -Pargs=arg1,arg2,arg3
 */
task run(type: JavaExec, dependsOn: classes) {
    main = mainClass
    classpath = sourceSets.main.runtimeClasspath

    if (project.hasProperty('args')) {
        args(project.args.split(','))
    }

}

/**
 * HOWTO debug:
 *  1. run "gradle debug"
 *  2. Call your IDE to connect to a remote java application on port 5005.
 *
 * This is how you pass arguments: "./gradlew debug -Pargs=arg1,arg2,arg3
 */
task debug(type: JavaExec, dependsOn: classes) {
    main = mainClass
    classpath = sourceSets.main.runtimeClasspath
    debug true

    if (project.hasProperty('args')) {
        args(project.args.split(','))
    }
}
