name: 'Release deploy workflow'

on: 
  workflow_dispatch:
  # push:
  # tags:
  #   - **

jobs:

  Build-Android-Release:
    name: 'Build-Android-Release'
    runs-on: ubuntu-latest

    env:
      QT_VERSION: 6.4.1
      QIF_VERSION: 4.5
      PROD_AGW_PUBLIC_KEY: ${{ secrets.PROD_AGW_PUBLIC_KEY }}
      PROD_S3_ENDPOINT: ${{ secrets.PROD_S3_ENDPOINT }}
      DEV_AGW_PUBLIC_KEY: ${{ secrets.DEV_AGW_PUBLIC_KEY }}
      DEV_AGW_ENDPOINT: ${{ secrets.DEV_AGW_ENDPOINT }}
      DEV_S3_ENDPOINT: ${{ secrets.DEV_S3_ENDPOINT }}

    steps:
    - name: 'Install desktop Qt'
      uses: jurplel/install-qt-action@v3
      with:
        version: ${{ env.QT_VERSION }}
        host: 'linux'
        target: 'desktop'
        arch: 'gcc_64'
        modules: 'qtremoteobjects qt5compat qtimageformats qtshadertools'
        dir: ${{ runner.temp }}
        setup-python: 'true'
        set-env: 'true'
        extra: '--external 7z'

    - name: 'Install android Qt x86_64'
      uses: jurplel/install-qt-action@v3
      with:
        version: ${{ env.QT_VERSION }}
        host: 'linux'
        target: 'android'
        arch: 'android_x86_64'
        modules: 'qtremoteobjects qt5compat qtimageformats qtshadertools'
        dir: ${{ runner.temp }}
        setup-python: 'true'
        set-env: 'true'
        extra: '--external 7z'

    - name: 'Install android Qt x86'
      uses: jurplel/install-qt-action@v3
      with:
        version: ${{ env.QT_VERSION }}
        host: 'linux'
        target: 'android'
        arch: 'android_x86'
        modules: 'qtremoteobjects qt5compat qtimageformats qtshadertools'
        dir: ${{ runner.temp }}
        setup-python: 'true'
        set-env: 'true'
        extra: '--external 7z'

    - name: 'Install android Qt arm_v7'
      uses: jurplel/install-qt-action@v3
      with:
        version: ${{ env.QT_VERSION }}
        host: 'linux'
        target: 'android'
        arch: 'android_armv7'
        modules: 'qtremoteobjects qt5compat qtimageformats qtshadertools'
        dir: ${{ runner.temp }}
        setup-python: 'true'
        set-env: 'true'
        extra: '--external 7z'

    - name: 'Install android Qt arm_v8'
      uses: jurplel/install-qt-action@v3
      with:
        version: ${{ env.QT_VERSION }}
        host: 'linux'
        target: 'android'
        arch: 'android_arm64_v8a'
        modules: 'qtremoteobjects qt5compat qtimageformats qtshadertools'
        dir: ${{ runner.temp }}
        setup-python: 'true'
        set-env: 'true'
        extra: '--external 7z'

    - name: 'Get sources'
      uses: actions/checkout@v3
      with:
        path: main
        submodules: 'true'
        fetch-depth: 10

    - name: 'Preparations before keystore fetching'
      run: | 
        mkdir keystore

    - name: 'Getting keystore'
      uses: actions/checkout@v3  
      with:
        repository: amnezia-vpn/amnezia-android-certificates
        ssh-key: ${{ secrets.ANDROID_CERTS_SSH_PRIVATE_KEY }}
        path: keystore

    - name: 'Setup ccache'
      uses: hendrikmuhs/ccache-action@v1.2

    - name: 'Setup Java'
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: '11'

    - name: 'Build project'
      run: |
        export QT_HOST_PATH="${{ runner.temp }}/Qt/${{ env.QT_VERSION }}/gcc_64"
        export NDK_VERSION=23c
        export ANDROID_NDK_PLATFORM=android-23
        export ANDROID_NDK_HOME=${{ runner.temp }}/android-ndk-r${NDK_VERSION}
        export ANDROID_NDK_ROOT=$ANDROID_NDK_HOME

        if [ ! -f $ANDROID_NDK_ROOT/ndk-build ]; then 
          wget https://dl.google.com/android/repository/android-ndk-r${NDK_VERSION}-linux.zip -qO ${{ runner.temp }}/ndk.zip &&
          unzip -q -d ${{ runner.temp }} ${{ runner.temp }}/ndk.zip ; 
        fi

        export QT_BIN_DIR=${{ runner.temp }}/Qt/${{ env.QT_VERSION }}/android_arm64_v8a/bin
        cd main
        bash deploy/build_android.sh

    - name: 'Signing APK'
      run: |
        pwd

        ANDROID_BUILD_TOOLS_VERSION=30.0.3

        ${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/zipalign -f -v 4 AmneziaVPN-release-unsigned.apk AmneziaVPN-release-aligned.apk
        ${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/apksigner sign --out AmneziaVPN-release-signed.apk --ks keystore/debug.keystore --ks-key-alias ${{ secrets.DEBUG_ANDROID_KEYSTORE_KEY_ALIAS }} --ks-pass pass:${{secrets.DEBUG_ANDROID_KEYSTOTE_KEY_PASS }} AmneziaVPN-release-aligned.apk

    - name: 'Upload'
      uses: actions/upload-artifact@v3
      with:
        name: Release APK
        path: ${{ runner.temp }}/main/AmneziaVPN-release-signed.apk
