name: Android

on:
  workflow_dispatch:
  push:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/android.yml'
  pull_request:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/android.yml'
  release:
    types: [published]

permissions:
  contents: read

jobs:
  build:
    permissions:
      contents: write  # for actions/upload-release-asset to upload release asset
    runs-on: windows-latest
    strategy:
      fail-fast: false
      max-parallel: 1
      matrix:
        ARCH: ["arm64", "x86_64"]
          
    env:
      RELEASE_NAME: raylib-dev_android_api29_${{ matrix.ARCH }}
    
    steps:
    - name: Checkout
      uses: actions/checkout@master
    
    - name: Setup Release Version
      run: |
        echo "RELEASE_NAME=raylib-${{ github.event.release.tag_name }}_android_api29_${{ matrix.ARCH }}" >> $GITHUB_ENV
      shell: bash
      if: github.event_name == 'release' && github.event.action == 'published'
      
    - name: Setup Android NDK
      id: setup-ndk
      uses: nttld/setup-ndk@v1
      with:
        ndk-version: r25
        add-to-path: false
      env:
        ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}

    - name: Setup Environment
      run: | 
        mkdir build
        cd build
        mkdir ${{ env.RELEASE_NAME }}
        cd ${{ env.RELEASE_NAME }}
        mkdir include
        mkdir lib
        cd ../..
   
    # Generating static + shared library for 64bit arquitectures and API version 29
    - name: Build Library
      run: |
        cd src
        make PLATFORM=PLATFORM_ANDROID ANDROID_ARCH=${{ matrix.ARCH }} ANDROID_API_VERSION=29 ANDROID_NDK=${{ env.ANDROID_NDK_HOME }} RAYLIB_LIBTYPE=STATIC RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib"
        make PLATFORM=PLATFORM_ANDROID ANDROID_ARCH=${{ matrix.ARCH }} ANDROID_API_VERSION=29 ANDROID_NDK=${{ env.ANDROID_NDK_HOME }} RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
      shell: cmd
  
    - name: Generate Artifacts
      run: |
        cp -v ./src/raylib.h ./build/${{ env.RELEASE_NAME }}/include
        cp -v ./src/raymath.h ./build/${{ env.RELEASE_NAME }}/include
        cp -v ./src/rlgl.h ./build/${{ env.RELEASE_NAME }}/include
        cp -v ./CHANGELOG ./build/${{ env.RELEASE_NAME }}/CHANGELOG
        cp -v ./README.md ./build/${{ env.RELEASE_NAME }}/README.md
        cp -v ./LICENSE ./build/${{ env.RELEASE_NAME }}/LICENSE
        cd build
        tar -czvf ${{ env.RELEASE_NAME }}.tar.gz ${{ env.RELEASE_NAME }}
        
    - name: Upload Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: ${{ env.RELEASE_NAME }}.tar.gz
        path: ./build/${{ env.RELEASE_NAME }}.tar.gz
        
    - name: Upload Artifact to Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./build/${{ env.RELEASE_NAME }}.tar.gz
          asset_name: ${{ env.RELEASE_NAME }}.tar.gz
          asset_content_type: application/gzip
      if: github.event_name == 'release' && github.event.action == 'published'  

