#
# Build configuration for Circle CI
#
# See this thread for speeding up and caching directories: https://discuss.circleci.com/t/installing-android-build-tools-23-0-2/924
#

machine:
  environment:
    ANDROID_HOME: /usr/local/android-sdk-linux
    GRADLE_OPTS: '-Xmx1536M -Dorg.gradle.jvmargs="-Xmx1536M -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError"'
    JAVA_OPTS: "-Xms518m -Xmx1536M"
  pre:
    - git clone https://github.com/RocketChat/Rocket.Chat.Kotlin.SDK.git Rocket.Chat.Kotlin.SDK

dependencies:
  pre:
    - sudo service mysql stop; sleep 5
    - sudo service mongod stop; sleep 5
    - sudo killall postgres; sleep 5

    - git fetch --tags

    - echo "sdk.dir="$ANDROID_HOME > local.properties

    - echo $ROCKET_JKS_BASE64 | base64 --decode > Rocket.jks
    - echo $ROCKET_PLAY_JSON | base64 --decode > app/rocket-chat.json

    - mkdir -p app/src/release/res/values
# TODO: remove the comment when using that file on the project
#    - echo $GOOGLE_SERVICES_BASE64 | base64 --decode > app/src/release/google-services.json
#   - echo $API_KEY_STRINGS_BASE64 | base64 --decode > app/src/release/res/values/api_key_strings.xml

    - mkdir -p $ANDROID_HOME/licenses/  
    - echo 8933bad161af4178b1185d1a37fbf41ea5269c55 >> $ANDROID_HOME/licenses/android-sdk-license
    - echo d56f5187479451eabf01fb78af6dfcb131a6481e >> $ANDROID_HOME/licenses/android-sdk-license  
    - echo y | android update sdk --no-ui --all --filter tools,platform-tools
    - echo y | android update sdk --no-ui --all --filter android-27
    - echo y | android update sdk --no-ui --all --filter extra-android-m2repository,extra-android-support
    - echo y | android update sdk --no-ui --all --filter extra-google-m2repository,extra-google-google_play_services
    - echo y | android update sdk --no-ui --all --filter build-tools-27.0.0
      #- yes | sdkmanager --licenses  
  cache_directories:
          #- /usr/local/android-sdk-linux/tools
          #- /usr/local/android-sdk-linux/build-tools/27.0.0

test:
  override:
    - ./gradlew assembleRelease --stacktrace
    - find . -name *.apk -exec mv {} $CIRCLE_ARTIFACTS/ \;

deployment:
  beta:
    tag: /v\d+\.\d+\.\d+(?!.)/
    owner: RocketChat
    commands:
      - ./gradlew publishListingRelease
        -Dorg.gradle.project.track=beta
  alpha:
    tag: /v\d+\.\d+\.\d+/
    owner: RocketChat
    commands:
      - ./gradlew publishListingRelease
        -Dorg.gradle.pr oject.track=alpha
