apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'signing'

ext.artifactId = 'zxing-android-embedded'


ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''
ext["sonatypeStagingProfileId"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        ext[name] = value
    }
} else {
    ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
    ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
    ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
    ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
    ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
    ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
}


dependencies {
    api project.zxingCore
    implementation 'androidx.core:core:1.6.0'
    implementation 'androidx.fragment:fragment:1.3.6'
    testImplementation 'junit:junit:4.13.1'
    testImplementation 'org.mockito:mockito-core:1.9.5'
    implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2'
}

android {
    resourcePrefix 'zxing_'
    compileSdkVersion project.androidTargetSdk

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        test.setRoot('test')
    }

    // This is bad practice - we should fix the warnings instead.
    lintOptions {
        // Android warns about the he and id locale folders. he -> iw is already handled with a
        // symlink. TODO: what about id?
        disable 'LocaleFolder'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    testOptions {
        // We test with primitives such as Rect, and rely on their default behaviour working.
        unitTests.returnDefaultValues = true
    }

    defaultConfig {
        minSdkVersion 19
    }

    buildTypes {
        debug {
            versionNameSuffix ".debug"
            resValue "string", "app_version", "${defaultConfig.versionName}${versionNameSuffix}"
        }
        release {
            resValue "string", "app_version", "${defaultConfig.versionName}"
        }
    }
}

task sourceJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

project.afterEvaluate {
    publishing {
        publications {
            maven(MavenPublication) {
                artifact bundleReleaseAar
                artifactId project.artifactId

                artifact sourceJar
                
                pom {
                    name = project.artifactId
                    description = 'Barcode scanner library for Android, based on the ZXing decoder'
                    url = 'https://github.com/journeyapps/zxing-android-embedded'

                    licenses {
                        license {
                            name = 'The Apache License, Version 2.0'
                            url = 'https://github.com/journeyapps/zxing-android-embedded/blob/master/COPYING'
                        }
                    }

                    developers {
                        developer {
                            id = ''
                            name = 'Ralf Kistner'
                            email = 'ralf@journeyapps.com'
                            organization = 'Journey Mobile, Inc'
                            organizationUrl = 'https://journeyapps.com'
                        }
                    }
                    scm {
                        connection = 'scm:git:github.com/journeyapps/zxing-android-embedded.git'
                        developerConnection = 'scm:git:ssh://github.com/journeyapps/zxing-android-embedded.git'
                        url = 'https://github.com/journeyapps/zxing-android-embedded'
                    }
                }


                pom.withXml {
                    // HACK to add dependencies to POM.
                    // When maven-publish can do this automatically for Android projects,
                    // remove this section.
                    def deps = asNode().appendNode('dependencies')

                    project.configurations.api.allDependencies.each { dep ->
                        def node = deps.appendNode('dependency')
                        node.appendNode('groupId', dep.group)
                        node.appendNode('artifactId', dep.name)
                        node.appendNode('version', dep.version)
                        node.appendNode('scope', 'api')
                    }

                }
            }
        }

        repositories {
            maven {
                name = "sonatype"
                url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
                credentials {
                    username ossrhUsername
                    password ossrhPassword
                }
            }
        }
    }
}

signing {
    sign publishing.publications
}
