stages:
  - build
  - build2
  - deploy
  - release

build arm64 apk:
  stage: build
  image: lbry/android-base:latest
  before_script:
    - export BUILD_VERSION=$(cat $CI_PROJECT_DIR/src/main/python/main.py | grep --color=never -oP '([0-9]+\.?)+')
    - git submodule sync --recursive
    - git submodule update --init --force --recursive
  artifacts:
    paths:
      - bin/browser-*-release__arm64.apk
    expire_in: 1 week
  script:
    - export PATH=/usr/bin:$PATH
    - echo "$PGP_PRIVATE_KEY" | gpg --batch --import
    - cd app
    - npm install
    - cd ..
    - wget -q 'https://eu.crystax.net/download/crystax-ndk-10.3.2-linux-x86_64.tar.xz' -P ~/.buildozer/android/
    - tar -xf ~/.buildozer/android/crystax-ndk-10.3.2-linux-x86_64.tar.xz -C ~/.buildozer/android/
    - rm -rf ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-9
    - ln -s ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-21 ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-9
    - cp -f $CI_PROJECT_DIR/scripts/build-target-python.sh ~/.buildozer/android/crystax-ndk-10.3.2/build/tools/build-target-python.sh
    - cp -f $CI_PROJECT_DIR/scripts/mangled-glibc-syscalls__arm64.h ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-21/arch-arm64/usr/include/crystax/bionic/libc/include/sys/mangled-glibc-syscalls.h
    - rm ~/.buildozer/android/crystax-ndk-10.3.2-linux-x86_64.tar.xz
    - git secret reveal
    - mv buildozer.spec.arm64.ci buildozer.spec
    - "./release.sh | grep -Fv -e 'working:' -e 'copy' -e 'Compiling' --line-buffered"
    - cp $CI_PROJECT_DIR/bin/browser-$BUILD_VERSION-release.apk $CI_PROJECT_DIR/bin/browser-$BUILD_VERSION-release__arm64.apk
    - cp $CI_PROJECT_DIR/bin/browser-$BUILD_VERSION-release.apk /dev/null

build arm apk:
  stage: build2
  image: lbry/android-base:latest
  before_script:
    - export BUILD_VERSION=$(cat $CI_PROJECT_DIR/src/main/python/main.py | grep --color=never -oP '([0-9]+\.?)+')
    - git submodule sync --recursive
    - git submodule update --init --force --recursive
  artifacts:
    paths:
      - bin/browser-*-release__arm.apk
    expire_in: 1 week
  script:
    - export PATH=/usr/bin:$PATH
    - echo "$PGP_PRIVATE_KEY" | gpg --batch --import
    - cd app
    - npm install
    - cd ..
    - wget -q 'https://eu.crystax.net/download/crystax-ndk-10.3.2-linux-x86_64.tar.xz' -P ~/.buildozer/android/
    - tar -xf ~/.buildozer/android/crystax-ndk-10.3.2-linux-x86_64.tar.xz -C ~/.buildozer/android/
    - rm -rf ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-9
    - ln -s ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-21 ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-9
    - cp -f $CI_PROJECT_DIR/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle.arm $CI_PROJECT_DIR/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle
    - cp -f $CI_PROJECT_DIR/scripts/build-target-python.sh ~/.buildozer/android/crystax-ndk-10.3.2/build/tools/build-target-python.sh
    - cp -f $CI_PROJECT_DIR/scripts/mangled-glibc-syscalls.h ~/.buildozer/android/crystax-ndk-10.3.2/platforms/android-21/arch-arm/usr/include/crystax/bionic/libc/include/sys/mangled-glibc-syscalls.h
    - rm ~/.buildozer/android/crystax-ndk-10.3.2-linux-x86_64.tar.xz
    - git secret reveal
    - mv buildozer.spec.arm.ci buildozer.spec
    - "./release.sh | grep -Fv -e 'working:' -e 'copy' -e 'Compiling' --line-buffered"
    - cp $CI_PROJECT_DIR/bin/browser-$BUILD_VERSION-release.apk $CI_PROJECT_DIR/bin/browser-$BUILD_VERSION-release__arm.apk
    - cp $CI_PROJECT_DIR/bin/browser-$BUILD_VERSION-release.apk /dev/null

deploy build.lbry.io:
  image: python:latest
  stage: deploy
  dependencies:
    - build arm apk
    - build arm64 apk
  before_script:
    - pip install awscli
    - export BUILD_VERSION=$(cat $CI_PROJECT_DIR/src/main/python/main.py | grep --color=never -oP '([0-9]+\.?)+')
    - export BUILD_APK_FILENAME__32=browser-$BUILD_VERSION-release__arm.apk
    - export BUILD_APK_FILENAME__64=browser-$BUILD_VERSION-release__arm64.apk
  script:
    - aws s3 cp bin/$BUILD_APK_FILENAME__64 s3://build.lbry.io/android/build-${CI_PIPELINE_IID}_commit-${CI_COMMIT_SHA:0:7}/$BUILD_APK_FILENAME__64
    - aws s3 cp bin/$BUILD_APK_FILENAME__32 s3://build.lbry.io/android/build-${CI_PIPELINE_IID}_commit-${CI_COMMIT_SHA:0:7}/$BUILD_APK_FILENAME__32
    - aws s3 cp bin/$BUILD_APK_FILENAME__64 s3://build.lbry.io/android/push.apk

release apk:
  image: python:latest
  stage: release
  only:
    - tags
  dependencies:
    - build arm apk
    - build arm64 apk
  before_script:
    - pip install awscli githubrelease
    - export BUILD_VERSION=$(cat $CI_PROJECT_DIR/src/main/python/main.py | grep --color=never -oP '([0-9]+\.?)+')
    - export BUILD_APK_FILENAME__32=browser-$BUILD_VERSION-release__arm.apk
    - export BUILD_APK_FILENAME__64=browser-$BUILD_VERSION-release__arm64.apk
  script:
    - githubrelease release lbryio/lbry-android create $CI_COMMIT_TAG --publish bin/$BUILD_APK_FILENAME__64 bin/$BUILD_APK_FILENAME__32
    - githubrelease release lbryio/lbry-android edit $CI_COMMIT_TAG --draft
    - aws s3 cp bin/$BUILD_APK_FILENAME__64 s3://build.lbry.io/android/latest.apk
