project.ext.hasBackend = false

if (file("../../../extra_properties.gradle").exists()) {
    apply from: '../../../extra_properties.gradle'
}

if (file("../../../../extra_properties.gradle").exists()) {
    apply from: '../../../../extra_properties.gradle'
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
        maven {
            url "http://google.bintray.com/googlevr"
        }
        flatDir {
            dirs '../../GearVRf/GVRf/gearvrf-libs', '../../../GearVRf/GVRf/gearvrf-libs', '../../../../GearVRf/GVRf/gearvrf-libs'
        }
    }
}

android {
    compileSdkVersion 26

    buildTypes {
        debug {
            debuggable = true
            jniDebuggable = true
            resValue 'string', 'app_name', System.getProperty("appName")
        }
        release {
            signingConfig signingConfigs.debug
            resValue 'string', 'app_name', System.getProperty("appName")
        }
    }

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 23
        ndk {
            if (rootProject.hasProperty("ARM64")) {
                abiFilters = ['arm64-v8a']
            } else {
                abiFilters = ['armeabi-v7a']
            }
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            output.outputFileName = System.getProperty("appName") + ".apk"
        }
    }

    // ignore the x86 files from the google vr libraries
    packagingOptions {
        exclude 'lib/x86/libgvr.so'
        pickFirst 'lib/*/libc++_shared.so'
        pickFirst 'lib/*/libgnustl_shared.so'
        if (!project.hasProperty("ARM64")) {
            exclude 'lib/arm64-v8a/*.so'
        } else {
            exclude 'lib/armeabi-v7a/*.so'
        }
    }
}

task copyOculusSignature(type: Copy) {
    if (project.hasProperty("OCULUS_SIGS_DIRECTORY")) {
        copy {
            from project.property("OCULUS_SIGS_DIRECTORY")
            into 'src/main/assets'
            include 'oculussig_*'
        }
    }
}

preBuild {}.dependsOn copyOculusSignature

file("../../..").eachDir {
    def fn = it.toString() + "/GearVRf-Demos_build.gradle"
    if (file(fn).exists()) {
        apply from: fn
        println "importing build file from " + it.toString()
    }
}

/*
To use local dependencies add the following line in a global gradle properties file
eg. ~/.gradle/gradle.properties:

useLocalDependencies=true

 */
ext.gearvrfVersion = '4.0.1-SNAPSHOT'
project.ext.daydreamVersion = '1.130.0'
project.ext.jomlVersion = "1.9.3-SNAPSHOT"
project.ext.gsonVersion = '2.8.2'

if (!project.ext.hasBackend) {
    project.ext.backend_oculus = true
    project.ext.backend_daydream = true
}

if (project.hasProperty("backend_monoscopic")) {
    project.ext.backend_monoscopic = project.property("backend_monoscopic")
} else if (project.hasProperty("backend_daydream")) {
    project.ext.backend_daydream = project.property("backend_daydream")
} else if (project.hasProperty("backend_oculus")) {
    project.ext.backend_oculus = project.property("backend_oculus")
}

dependencies {
    implementation "com.google.code.gson:gson:$gsonVersion"
    implementation "org.joml:joml-android:${jomlVersion}"

    if (project.hasProperty("useLocalDependencies") && project.useLocalDependencies) {
        if (findProject(':framework')) {
            implementation project(':framework')
        } else {
            debugImplementation(name: 'framework-debug', ext: 'aar')
            releaseImplementation(name: 'framework-release', ext: 'aar')
        }
    } else {
        implementation "org.gearvrf:framework:$gearvrfVersion"
    }

    if (project.hasProperty("backend_monoscopic") && project.property("backend_monoscopic")) {
        if (project.hasProperty("useLocalDependencies") && project.useLocalDependencies) {
            if (findProject(':backend_monoscopic')) {
                implementation project(':backend_monoscopic')
            } else {
                debugImplementation(name: 'backend_monoscopic-debug', ext: 'aar')
                releaseImplementation(name: 'backend_monoscopic-release', ext: 'aar')
            }
        } else {
            implementation "org.gearvrf:backend_monoscopic:$gearvrfVersion"
        }
    }

    if (project.hasProperty("backend_daydream") && project.property("backend_daydream")) {
        if (project.hasProperty("useLocalDependencies") && project.useLocalDependencies) {
            if (findProject(':backend_daydream')) {
                implementation project(':backend_daydream')
            } else {
                debugImplementation(name: 'backend_daydream-debug', ext: 'aar')
                releaseImplementation(name: 'backend_daydream-release', ext: 'aar')
            }
        } else {
            implementation "org.gearvrf:backend_daydream:$gearvrfVersion"
        }
        implementation "com.google.vr:sdk-base:${daydreamVersion}"
        implementation "com.google.vr:sdk-controller:${daydreamVersion}"
    }

    if (project.hasProperty("backend_oculus") && project.property("backend_oculus")) {
        if (project.hasProperty("useLocalDependencies") && project.useLocalDependencies) {
            if (findProject(':backend_oculus')) {
                implementation project(':backend_oculus')
            } else {
                debugImplementation(name: 'backend_oculus-debug', ext: 'aar')
                releaseImplementation(name: 'backend_oculus-release', ext: 'aar')
            }
        } else {
            implementation "org.gearvrf:backend_oculus:$gearvrfVersion"
        }
    }
}

