plugins {
    id 'java'
    id 'application'
    id 'maven'
    id 'signing'
}

repositories {
    mavenCentral()
    maven {
        // SWT libraries
        url "http://maven-eclipse.github.io/maven"
    }
}

mainClassName = 'com.webcodepro.applecommander.ui.AppleCommander'
version "${version}"

compileJava {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

test {
    String swtDependency
    switch (System.getProperty('os.name').toLowerCase().split()[0]) {
        case 'windows':
            swtDependency = "org.eclipse.swt:org.eclipse.swt.win32.win32.x86_64:$swtVersion"
            break
        case 'linux':
            swtDependency = "org.eclipse.swt:org.eclipse.swt.gtk.linux.x86_64:$swtVersion"
            break
        case 'mac':
            swtDependency = "org.eclipse.swt:org.eclipse.swt.cocoa.macosx.x86_64:$swtVersion"
            break
        default:
            throw new Exception('Unknown OS')
    }
    dependencies {
        compile "$swtDependency"
    }
    testLogging {
        exceptionFormat = 'full'
    }
}

// Disable default JAR creation
project.tasks.jar.setEnabled(false)

// Generic additions to JAR creation
tasks.withType(Jar) {
    manifest {
        attributes 'Main-Class': 'com.webcodepro.applecommander.ui.AppleCommander'
        attributes 'Implementation-Title': 'AppleCommander',
                   'Implementation-Version': version
    }
    from('LICENSE')
    doFirst {
        // Jar files with an appendix are standalone applications and need to have ShrinkIt included.
        if (appendix) {
            from { 
                configurations.runtime.collect { 
                    it.name.startsWith('ShrinkItArchive') || it.name.startsWith('applesingle-api') || 
                        it.name.startsWith('bastools-api') ? zipTree(it) : 'fake' 
                } 
            }
        } 
    } 
}

dependencies {
    compile "net.sf.applecommander:ShrinkItArchive:$shkVersion"
    compile "net.sf.applecommander:applesingle-api:$asVersion"
    compile "net.sf.applecommander:bastools-api:$btVersion"
    compileOnly "org.apache.ant:ant:$antVersion"

    testCompile "junit:junit:$junitVersion"
    testCompile "org.apache.commons:commons-lang3:$commonsLang3Version"
}

task mavenJar(type: Jar) {
    dependencies {
        // Just to pass the compile step; these classes are stripped out below.
        compile "org.eclipse.swt:org.eclipse.swt.cocoa.macosx.x86_64:$swtVersion"
    }
    from(sourceSets.main.output) {
        include 'com/webcodepro/applecommander/**'
        exclude 'com/webcodepro/applecommander/ui/images/**'
        exclude 'com/webcodepro/applecommander/ui/swing/**'
        exclude 'com/webcodepro/applecommander/ui/swt/**'
    }
}
task acJar(type: Jar) {
    dependencies {
        // Just to pass the compile step; these classes are stripped out below.
        compile "org.eclipse.swt:org.eclipse.swt.cocoa.macosx.x86_64:$swtVersion"
    }
    appendix 'ac'
    manifest {
        attributes 'Main-Class' : 'com.webcodepro.applecommander.ui.ac'
    }
    from(sourceSets.main.output) {
        include 'com/webcodepro/**'
        exclude 'com/webcodepro/applecommander/ui/images/**'
        exclude 'com/webcodepro/applecommander/ui/swing/**'
        exclude 'com/webcodepro/applecommander/ui/swt/**'
    }
}
task linuxJar(type: Jar) {
    dependencies {
        compile "org.eclipse.swt:org.eclipse.swt.gtk.linux.x86_64:$swtVersion"
    }
    appendix 'linux64-gtk'
    from(sourceSets.main.output) {
        include 'com/webcodepro/**'
    }
    doFirst {
        // Pick and include just the Linux JAR contents
        from { configurations.runtime.collect { it.name.contains('.linux.') ? zipTree(it) : 'fake' } } 
    } 
}
task macosxJar(type: Jar) {
    dependencies {
        compile "org.eclipse.swt:org.eclipse.swt.cocoa.macosx.x86_64:$swtVersion"
    }
    appendix 'macosx'
    from(sourceSets.main.output) {
        include 'com/webcodepro/**'
    }
    doFirst {
        // Pick and include just the Mac OS X JAR contents
        from { configurations.runtime.collect { it.name.contains('.macosx.') ? zipTree(it) : 'fake' } } 
    } 
}
task windowsJar(type: Jar) {
    dependencies {
        compile "org.eclipse.swt:org.eclipse.swt.win32.win32.x86_64:$swtVersion"
    }
    appendix 'win64'
    from(sourceSets.main.output) {
        include 'com/webcodepro/**'
    }
    doFirst {
        // Pick and include just the Windows SWT JAR contents
        from { configurations.runtime.collect { it.name.contains('.win32.') ? zipTree(it) : 'fake' } } 
    } 
}

task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from javadoc
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

javadoc {
    options.addStringOption('Xdoclint:none', '-quiet')
}

artifacts {
    archives mavenJar, acJar 
    archives linuxJar, macosxJar, windowsJar
    archives javadocJar, sourcesJar
}

signing {
    // Only sign if we're uploading...
    required { gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

uploadArchives {
  repositories {
    mavenDeployer {
      addFilter('AppleCommander') { artifact, file ->
        // Note that the other executables all have suffixes which change their name.
        return artifact.name == 'AppleCommander' && artifact.ext != 'zip' && artifact.ext != 'tar'
      } 
    
      beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

      repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
        authentication(userName: findProperty('ossrhUsername'), password: findProperty('ossrhPassword'))
      }

      snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
        authentication(userName: findProperty('ossrhUsername'), password: findProperty('ossrhPassword'))
      }

      // Only preserve the compile and non-SWT POM dependencies
      pom('AppleCommander').whenConfigured { p ->
        p.dependencies = p.dependencies.findAll { it.scope == "compile" }
        p.dependencies = p.dependencies.findAll { it.groupId != "org.eclipse.swt" }
      }
      
      pom('AppleCommander').project {
        name archivesBaseName
        packaging 'jar'
        description 'AppleCommander is a general utility for Apple II disk images.'
        url 'https://applecommander.github.io/'
        
        scm {
          url 'https://github.com/AppleCommander/AppleCommander'
        }

        licenses {
          license {
            name 'The GNU General Public License (GPL) Version 2, June 1991'
            url 'https://www.gnu.org/licenses/gpl-2.0.html'
          }
        }

        developers {
          developer {
            id 'robgreene'
            email 'robgreene@gmail.com'
          }
        }
      }
    }
  }
}
