/*
 * Firetweet - Twitter client for Android
 *
 *  Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
apply plugin: 'com.android.application'

/**
 * Gradle script for signing applications
 * Just apply this script to your application build.gradle file.
 * DON'T FORGET TO IGNORE signing.properties FROM YOUR VERSION CONTROL!!!
 *
 * @author Mariotaku Lee <mariotaku.lee@gmail.com>
 */

android {

    signingConfigs {
        debug {
            File signingPropFile = rootProject.file('signing.properties')
            if (signingPropFile.exists()) {
                Properties signingProp = new Properties()
                signingProp.load(signingPropFile.newDataInputStream())
                storeFile file(signingProp.get("debug.storeFile"))
                storePassword signingProp.get("debug.storePassword")
                keyAlias signingProp.get("debug.keyAlias")
                keyPassword signingProp.get("debug.keyPassword")
            } else if (System.getenv('DEBUG_KEYSTORE_BASE64') != null) {
                storeFile decodeKeyStoreFileFromBase64Env('DEBUG_KEYSTORE_BASE64')
                storePassword System.getenv('DEBUG_KEYSTORE_PASSWORD')
                keyAlias System.getenv('DEBUG_KEYSTORE_KEY_ALIAS')
                keyPassword System.getenv('DEBUG_KEYSTORE_KEY_PASSWORD')
            }
        }
        release {
            File signingPropFile = rootProject.file('signing.properties')
            if (signingPropFile.exists()) {
                Properties signingProp = new Properties()
                signingProp.load(signingPropFile.newDataInputStream())
                storeFile file(signingProp.get("release.storeFile"))
                storePassword signingProp.get("release.storePassword")
                keyAlias signingProp.get("release.keyAlias")
                keyPassword signingProp.get("release.keyPassword")
            } else if (System.getenv('RELEASE_KEYSTORE_BASE64') != null) {
                storeFile decodeKeyStoreFileFromBase64Env('RELEASE_KEYSTORE_BASE64')
                storePassword System.getenv('RELEASE_KEYSTORE_PASSWORD')
                keyAlias System.getenv('RELEASE_KEYSTORE_KEY_ALIAS')
                keyPassword System.getenv('RELEASE_KEYSTORE_KEY_PASSWORD')
            }
        }
    }
    buildTypes {
        debug {
            if (rootProject.file('signing.properties').exists()
                    || System.getenv('DEBUG_KEYSTORE_BASE64') != null) {
                signingConfig signingConfigs.debug
            }
        }
        release {
            if (rootProject.file('signing.properties').exists()
                    || System.getenv('RELEASE_KEYSTORE_BASE64') != null) {
                signingConfig signingConfigs.release
            }
        }
    }

}

def decodeKeyStoreFileFromBase64Env(String name) {
    String keyStoreBase64 = System.getenv(name)
    if (keyStoreBase64 == null) return null
    File tempKeyStoreFile = File.createTempFile("tmp_ks_", ".jks", File.createTempDir())
    FileOutputStream fos = null
    try {
        fos = new FileOutputStream(tempKeyStoreFile)
        fos.write(keyStoreBase64.decodeBase64())
        fos.flush()
    } finally {
        if (fos != null) {
            fos.close()
        }
    }
    return tempKeyStoreFile
}
