name: Build and Release RyukGram

on:
    push:
        branches:
            - 'main'
    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
              uses: actions/checkout@v4
              with:
                submodules: recursive

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

            - name: Set PATH
              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}' control)
                echo "VERSION=${VERSION}" >> "$GITHUB_ENV"
                echo "version=${VERSION}" >> "$GITHUB_OUTPUT"

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

            - name: Build rootless deb
              run: |
                export THEOS="${{ github.workspace }}/theos"
                ./build.sh rootless
                cd packages
                DEB="$(ls -t *-rootless.deb | head -n1)"
                [ -n "$DEB" ] && mv "$DEB" "RyukGram_${VERSION}_rootless.deb"
                ls -la

            - name: Build rootful deb
              run: |
                export THEOS="${{ github.workspace }}/theos"
                ./build.sh rootful
                cd packages
                DEB="$(ls -t *-rootful.deb | head -n1)"
                [ -n "$DEB" ] && mv "$DEB" "RyukGram_${VERSION}_rootful.deb"
                ls -la

            - name: Upload rootless artifact
              uses: actions/upload-artifact@v4
              with:
                name: RyukGram_v${{ steps.version.outputs.version }}_rootless
                path: packages/RyukGram_${{ env.VERSION }}_rootless.deb
                if-no-files-found: error

            - name: Upload rootful artifact
              uses: actions/upload-artifact@v4
              with:
                name: RyukGram_v${{ steps.version.outputs.version }}_rootful
                path: packages/RyukGram_${{ env.VERSION }}_rootful.deb
                if-no-files-found: error

            - name: Check if release
              id: check_release
              run: |
                COMMIT_MSG=$(git log -1 --pretty=%B)
                if [[ "$COMMIT_MSG" == *"[release]"* ]] || [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
                  echo "should_release=true" >> "$GITHUB_OUTPUT"
                else
                  echo "should_release=false" >> "$GITHUB_OUTPUT"
                fi

            - name: Generate release notes
              if: steps.check_release.outputs.should_release == 'true'
              run: |
                PREV_TAG=$(git tag --sort=-creatordate | grep -v "v${VERSION}$" | head -n1 || true)
                {
                  echo "## Changelog"
                  echo ""
                  git log -1 --pretty=%B | tail -n +2 | sed -e '/^<!--/,/-->$/d' -e '/^\[release\]/d'
                  echo ""
                  echo "## Downloads"
                  echo ""
                  echo "| File | Description |"
                  echo "|------|-------------|"
                  echo "| \`RyukGram_rootless.deb\` | Rootless .deb (Dopamine/palera1n). Also works for sideloading via Feather/cyan. |"
                  echo "| \`RyukGram_rootful.deb\` | Rootful .deb (unc0ver/checkra1n). |"
                  echo ""
                  if [ -n "$PREV_TAG" ]; then
                    echo "**Full changelog:** [\`${PREV_TAG}\`...\`v${VERSION}\`](https://github.com/${{ github.repository }}/compare/${PREV_TAG}...v${VERSION})"
                  else
                    echo "**[All commits](https://github.com/${{ github.repository }}/commits/main)**"
                  fi
                } > /tmp/release_notes.md

            - name: Create Release
              if: steps.check_release.outputs.should_release == 'true'
              run: |
                SUBJECT=$(git log -1 --pretty=%s | sed 's/\[release\]//g' | xargs)
                TITLE="${SUBJECT:-RyukGram v${VERSION}}"
                gh release create "v${VERSION}" \
                  --repo "${{ github.repository }}" \
                  --title "$TITLE" \
                  --notes-file /tmp/release_notes.md \
                  "packages/RyukGram_${VERSION}_rootless.deb" \
                  "packages/RyukGram_${VERSION}_rootful.deb"
              env:
                GH_TOKEN: ${{ github.token }}
