name: Release (Apple)
on:
  release:
    types:
    - created
jobs:
  build:
    name: Build ${{ matrix.platform }} Release
    runs-on: macos-14
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
        - platform: iOS
          rust-targets:
          - aarch64-apple-ios
        - platform: macOS
          rust-targets:
          - x86_64-apple-darwin
          - aarch64-apple-darwin
    env:
      DEVELOPER_DIR: /Applications/Xcode_16.0.app/Contents/Developer
      PROTOC_PATH: /opt/homebrew/bin/protoc
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - name: Import Certificate
      uses: ./.github/actions/import-cert
      with:
        certificate: ${{ secrets.DEVELOPER_CERT }}
        password: ${{ secrets.DEVELOPER_CERT_PASSWORD }}
    - name: Download Provisioning Profiles
      uses: ./.github/actions/download-profiles
      with:
        app-store-key: ${{ secrets.APPSTORE_KEY }}
        app-store-key-id: ${{ secrets.APPSTORE_KEY_ID }}
        app-store-key-issuer-id: ${{ secrets.APPSTORE_KEY_ISSUER_ID }}
    - name: Install Provisioning Profiles
      shell: bash
      run: |
        mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles/
        cp -f Apple/Profiles/* ~/Library/MobileDevice/Provisioning\ Profiles/
    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: ${{ join(matrix.rust-targets, ', ') }}
    - name: Install Protobuf
      shell: bash
      run: brew install protobuf
    - name: Configure Version
      id: version
      shell: bash
      run: echo "BUILD_NUMBER=$(Tools/version.sh)" >> $GITHUB_OUTPUT
    - name: Archive
      uses: ./.github/actions/archive
      with:
        scheme: App
        destination: generic/platform=${{ matrix.platform }}
        app-store-key: ${{ secrets.APPSTORE_KEY }}
        app-store-key-id: ${{ secrets.APPSTORE_KEY_ID }}
        app-store-key-issuer-id: ${{ secrets.APPSTORE_KEY_ISSUER_ID }}
        archive-path: Burrow.xcarchive
    - name: Export
      uses: ./.github/actions/export
      with:
        method: ${{ matrix.platform == 'macOS' && 'developer-id' || 'ad-hoc' }}
        destination: export
        app-store-key: ${{ secrets.APPSTORE_KEY }}
        app-store-key-id: ${{ secrets.APPSTORE_KEY_ID }}
        app-store-key-issuer-id: ${{ secrets.APPSTORE_KEY_ISSUER_ID }}
        archive-path: Burrow.xcarchive
        export-options: |
          {"teamID":"P6PV2R9443","destination":"export","method":"developer-id","provisioningProfiles":{"com.hackclub.burrow":"Burrow Developer ID","com.hackclub.burrow.network":"Burrow Network Developer ID"},"signingCertificate":"Developer ID Application","signingStyle":"manual"}
        export-path: Release
    - name: Notarize
      if: ${{ matrix.platform == 'macOS' }}
      uses: ./.github/actions/notarize
      with:
        app-store-key: ${{ secrets.APPSTORE_KEY }}
        app-store-key-id: ${{ secrets.APPSTORE_KEY_ID }}
        app-store-key-issuer-id: ${{ secrets.APPSTORE_KEY_ISSUER_ID }}
    - name: Compress (iOS)
      if: ${{ matrix.platform == 'iOS' }}
      shell: bash
      run: |
        cp Apple/Release/Burrow.ipa Burrow.ipa
        aa archive -a lzma -b 8m -d Apple -subdir Burrow.xcarchive -o Burrow-${{ matrix.platform }}.xcarchive.aar
        rm -rf Apple/Release
    - name: Compress (macOS)
      if: ${{ matrix.platform == 'macOS' }}
      shell: bash
      run: |
        aa archive -a lzma -b 8m -d Apple/Release -subdir Burrow.app -o Burrow.app.aar
        aa archive -a lzma -b 8m -d Apple -subdir Burrow.xcarchive -o Burrow-${{ matrix.platform }}.xcarchive.aar
        rm -rf Apple/Release
    - name: Upload to GitHub
      uses: SierraSoftworks/gh-releases@v1.0.7
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        release_tag: ${{ github.ref_name }}
        overwrite: 'true'
        files: |
          ${{ matrix.platform == 'macOS' && 'Burrow.aap.aar' || 'Burrow.ipa' }}
          Burrow-${{ matrix.platform }}.xcarchive.aar
    - name: Upload to App Store Connect
      if: ${{ matrix.platform == 'iOS' }}
      uses: ./.github/actions/export
      with:
        app-store-key: ${{ secrets.APPSTORE_KEY }}
        app-store-key-id: ${{ secrets.APPSTORE_KEY_ID }}
        app-store-key-issuer-id: ${{ secrets.APPSTORE_KEY_ISSUER_ID }}
        archive-path: Burrow.xcarchive
        export-options: |
          {"method": "app-store", "destination": "upload"}
        export-path: Release
