name: Build APK

on:
  workflow_dispatch:
    inputs:
      release_tag:
        required: false
        type: string
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Prepare build dir
      run: |
        mkdir ${{ github.workspace }}/build

    - name: Fetch AndroidLibV2rayLite
      run: |
        cd ${{ github.workspace }}/build
        git clone --depth=1 -b master https://github.com/2dust/AndroidLibV2rayLite.git
        cd AndroidLibV2rayLite
        git submodule update --init

    - name: Restore cached libtun2socks
      id: cache-libtun2socks-restore
      uses: actions/cache/restore@v4
      with:
        path: ${{ github.workspace }}/build/AndroidLibV2rayLite/libs
        key: libtun2socks-${{ runner.os }}-${{ hashFiles('build/AndroidLibV2rayLite/.git/refs/heads/master') }}-${{ hashFiles('build/AndroidLibV2rayLite/.git/modules/badvpn/HEAD') }}-${{ hashFiles('build/AndroidLibV2rayLite/.git/modules/libancillary/HEAD') }}

    - name: Setup Android NDK
      if: steps.cache-libtun2socks-restore.outputs.cache-hit != 'true'
      uses: nttld/setup-ndk@v1
      id: setup-ndk
      # Same version as https://gitlab.com/fdroid/fdroiddata/metadata/com.v2ray.ang.yml
      with:
        ndk-version: r27
        add-to-path: true
        link-to-sdk: true
        local-cache: true

    - name: Restore Android Symlinks
      if: steps.cache-libtun2socks-restore.outputs.cache-hit != 'true'
      run: |
        directory="${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin"
        find "$directory" -type l | while read link; do
            current_target=$(readlink "$link")
            new_target="$directory/$(basename "$current_target")"
            ln -sf "$new_target" "$link"
            echo "Changed $(basename "$link") from $current_target to $new_target"
        done

    - name: Build libtun2socks
      if: steps.cache-libtun2socks-restore.outputs.cache-hit != 'true'
      run: |
        cd ${{ github.workspace }}/build/AndroidLibV2rayLite
        bash compile-tun2socks.sh
        tar -xvzf libtun2socks.so.tgz
        cp -r libs/* ${{ github.workspace }}/V2rayNG/app/libs/
      env:
        NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}

    - name: Save libtun2socks
      if: steps.cache-libtun2socks-restore.outputs.cache-hit != 'true'
      uses: actions/cache/save@v4
      with:
        path: ${{ github.workspace }}/build/AndroidLibV2rayLite/libs
        key: libtun2socks-${{ runner.os }}-${{ hashFiles('build/AndroidLibV2rayLite/.git/refs/heads/master') }}-${{ hashFiles('build/AndroidLibV2rayLite/.git/modules/badvpn/HEAD') }}-${{ hashFiles('build/AndroidLibV2rayLite/.git/modules/libancillary/HEAD') }}

    - name: Copy libtun2socks
      run: |
        cp -r ${{ github.workspace }}/build/AndroidLibV2rayLite/libs/* ${{ github.workspace }}/V2rayNG/app/libs/

    - name: Download libv2ray
      uses: robinraju/release-downloader@v1
      with:
        repository: '2dust/AndroidLibXrayLite'
        latest: true
        fileName: 'libv2ray.aar'
        out-file-path: V2rayNG/app/libs/

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

    - name: Setup Android environment
      uses: android-actions/setup-android@v3

    - name: Decode Keystore
      uses: timheuer/base64-to-file@v1
      id: android_keystore
      with:
        fileName: "android_keystore.jks"
        encodedString: ${{ secrets.APP_KEYSTORE_BASE64 }}

    - name: Build APK
      run: |
        cd ${{ github.workspace }}/V2rayNG
        chmod 755 gradlew
        ./gradlew licenseFdroidReleaseReport
        ./gradlew assembleRelease -Pandroid.injected.signing.store.file=${{ steps.android_keystore.outputs.filePath }} -Pandroid.injected.signing.store.password=${{ secrets.APP_KEYSTORE_PASSWORD }} -Pandroid.injected.signing.key.alias=${{ secrets.APP_KEYSTORE_ALIAS }} -Pandroid.injected.signing.key.password=${{ secrets.APP_KEY_PASSWORD }}

    - name: Upload arm64-v8a APK
      uses: actions/upload-artifact@v4
      if: ${{  success() }}
      with:
        name: arm64-v8a
        path: ${{ github.workspace }}/V2rayNG/app/build/outputs/apk/*/release/*arm64-v8a*.apk

    - name: Upload armeabi-v7a APK
      uses: actions/upload-artifact@v4
      if: ${{  success() }}
      with:
        name: armeabi-v7a
        path: ${{ github.workspace }}/V2rayNG/app/build/outputs/apk/*/release/*armeabi-v7a*.apk

    - name: Upload x86 APK
      uses: actions/upload-artifact@v4
      if: ${{  success() }}
      with:
        name: x86-apk
        path: ${{ github.workspace }}/V2rayNG/app/build/outputs/apk/*/release/*x86*.apk

    - name: Upload to release
      uses: svenstaro/upload-release-action@v2
      if: github.event.inputs.release_tag != ''
      with:
        file: ${{ github.workspace }}/V2rayNG/app/build/outputs/apk/*playstore*/release/*.apk
        tag: ${{ github.event.inputs.release_tag }}
        file_glob: true
        prerelease: true
