name: Build RyukGram tweak (rootless + rootful)

on:
    push:
        branches:
            - 'main'
            - 'dev'
    workflow_dispatch:

concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}
    cancel-in-progress: true
jobs:
    build:
        name: Build RyukGram
        runs-on: macos-latest
        permissions:
            contents: write

        steps:
            - name: Checkout Main
              uses: actions/checkout@v4
              with:
                path: main

            - name: Install Dependencies
              run: brew install ldid dpkg make

            - name: Set PATH environment variable
              run: echo "$(brew --prefix make)/libexec/gnubin" >> $GITHUB_PATH

            - name: Setup Theos
              uses: actions/checkout@v4
              with:
                repository: theos/theos
                ref: master
                path: ${{ github.workspace }}/theos
                submodules: recursive

            - name: SDK Caching
              id: SDK
              uses: actions/cache@v4
              env:
                cache-name: iPhoneOS16.2.sdk
              with:
                path: ${{ github.workspace }}/theos/sdks/
                key: ${{ env.cache-name }}
                restore-keys: ${{ env.cache-name }}

            - name: Download iOS SDK
              if: steps.SDK.outputs.cache-hit != 'true'
              run: |
                git clone --quiet -n --depth=1 --filter=tree:0 https://github.com/xybp888/iOS-SDKs/
                cd iOS-SDKs
                git sparse-checkout set --no-cone iPhoneOS16.2.sdk
                git checkout
                mv *.sdk $THEOS/sdks
              env:
                THEOS: ${{ github.workspace }}/theos

            - name: Get Version
              id: version
              run: |
                VERSION=$(awk '/Version:/ {print $2}' main/control)
                echo "VERSION=${VERSION}" >> "$GITHUB_ENV"
                echo "version=${VERSION}" >> "$GITHUB_OUTPUT"

            - name: Setup FFmpegKit
              run: cd main && ./scripts/setup-ffmpegkit.sh

            - name: Build rootless
              run: |
                cd main
                ./build.sh rootless
                cd packages
                DEB="$(ls -t *-rootless.deb | head -n1)"
                [ -n "$DEB" ] && mv "$DEB" "com.faroukbmiled.ryukgram_${VERSION}+debug-rootless.deb"
                ls -la
              env:
                THEOS: ${{ github.workspace }}/theos

            - name: Build rootful
              run: |
                cd main
                ./build.sh rootful
                cd packages
                DEB="$(ls -t *-rootful.deb | head -n1)"
                [ -n "$DEB" ] && mv "$DEB" "com.faroukbmiled.ryukgram_${VERSION}+debug-rootful.deb"
                ls -la
              env:
                THEOS: ${{ github.workspace }}/theos

            - name: Upload rootless artifact
              uses: actions/upload-artifact@v4
              with:
                name: com.faroukbmiled.ryukgram_${{ env.VERSION }}+debug-rootless.deb
                path: ${{ github.workspace }}/main/packages/com.faroukbmiled.ryukgram_${{ env.VERSION }}+debug-rootless.deb
                if-no-files-found: error

            - name: Upload rootful artifact
              uses: actions/upload-artifact@v4
              with:
                name: com.faroukbmiled.ryukgram_${{ env.VERSION }}+debug-rootful.deb
                path: ${{ github.workspace }}/main/packages/com.faroukbmiled.ryukgram_${{ env.VERSION }}+debug-rootful.deb
                if-no-files-found: error
