name: "Auto compile with OpenWrt SDK"
on:
  workflow_dispatch:
  push:
    tags:
      - 'v*'
    branches:
      - main
    paths-ignore:
      - "docs/**"
      - "README.md"
      - "README.en.md"
      - ".github/workflows/**"
      - ".github/**"

env:
  TZ: Asia/Shanghai

jobs:
  job_prepare:
    runs-on: ubuntu-latest
    outputs:
      branch_name: ${{ env.branch_name }}
      tag_name: ${{ env.tag_name }}
      push_type: ${{ env.push_type }}

    steps:
      - name: Determine push type
        run: |
            if [[ "${{ github.ref }}" == refs/tags/* ]]; then
              echo "This is a tag push."
              echo "tag_name=${{ github.ref_name }}" >> $GITHUB_ENV
              echo "push_type=tag" >> $GITHUB_ENV
            else
              echo "This is a branch push."
              echo "branch_name=${{ github.ref_name }}" >> $GITHUB_ENV
              echo "push_type=branch" >> $GITHUB_ENV
            fi

      - name: Checkout
        if: 
        uses: actions/checkout@v2
        with:
          path: 'qmodem'

      - name: Update Modem_support_list
        id: update_modem_support_list
        run: |
          cd qmodem
          python3 ./scripts/update_support_list.py temp_support_list ./luci/luci-app-qmodem/root/usr/share/qmodem/modem_support.json
          is_diff=$(diff ./temp_support_list.md ./docs/support_list.md > /dev/null 2>&1 && echo 0 || echo 1)
          echo "is_diff=${is_diff}" >> $GITHUB_ENV
          if [[ ${is_diff} == 1 ]]; then
            mv temp_support_list.md ./docs/support_list.md
            git config --global user.name "GitHub Action"
            git config --global user.email "github-action@users.noreply.github.com"
            git add ./docs/support_list.md
            git commit -m "Update support list"
            git push origin ${{ env.branch_name }}
          fi
          mv temp_support_list_release_notes.md ./release_note.md
      
      - name: Upload Release Note
        id: upload_release_note
        uses: actions/upload-artifact@v4
        with:
          name: Release Note
          path: ./qmodem/release_note.md

  job_build_qmodem:
    name: Build QModem
    needs: job_prepare
    continue-on-error: true
    strategy:
      matrix:
        build_arch: ['arm64_ipk',"arm64_apk","x64_apk","x64_ipk" ]
    runs-on: ubuntu-latest
    steps:
      - name: Install packages
        run: |
          sudo -E apt-get -qq update
          sudo -E apt-get -qq install zstd build-essential clang flex bison g++ gawk gcc-multilib g++-multilib gettext git libncurses5-dev libssl-dev python3-venv rsync unzip zlib1g-dev file wget
          sudo -E apt-get -qq autoremove --purge
          sudo -E apt-get -qq clean

      
      - name: Checkout
        uses: actions/checkout@v2
        with:
          path: 'qmodem'


      - name: Import Env
        run: cat qmodem/.github/workflows/${{ matrix.build_arch }}.env >> "$GITHUB_ENV"

      - name: Cache openwrt SDK
        id: cache-sdk
        uses: actions/cache@v3
        with:
          path: sdk
          key: openwrt-sdk-${{ matrix.build_arch }}

      - name: Initialization environment
        if: steps.cache-sdk.outputs.cache-hit != 'true'
        env:
          url_sdk: ${{ env.SDK_URL }}${{ env.SDK_NAME}}.${{ env.SDK_EXT }}
        run: |
          wget ${{ env.url_sdk }}
          file_name=${{ env.SDK_NAME}}.${{ env.SDK_EXT }}
          if [ "${{ env.SDK_EXT }}" == "tar.zst" ]; then
            mkdir sdk && tar --zstd -xvf $file_name -C ./sdk --strip-components=1
          elif [ "${{ env.SDK_EXT }}" == "tar.xz" ]; then
            mkdir sdk && tar -xvf $file_name -C ./sdk --strip-components=1
          fi
          cd sdk
          echo "src-git base https://github.com/openwrt/openwrt.git;main" > feeds.conf
          echo "src-git packages https://github.com/openwrt/packages.git;master" >> feeds.conf
          echo "src-git luci https://github.com/openwrt/luci.git;master" >> feeds.conf
          echo "src-git routing https://git.openwrt.org/feed/routing.git;master"  >> feeds.conf
          ./scripts/feeds update -a
          ./scripts/feeds install -a
          cd ..
          echo "src-link qmodem `pwd`/qmodem" >> sdk/feeds.conf
          cd sdk
          ./scripts/feeds update qmodem
          ./scripts/feeds install -a -p qmodem
          echo "CONFIG_ALL_NONSHARED=n" > .config
          echo "CONFIG_ALL_KMODS=n" >> .config
          echo "CONFIG_ALL=n" >> .config
          echo "CONFIG_AUTOREMOVE=n" >> .config
          echo "CONFIG_LUCI_LANG_zh_Hans=y" >> .config
          cat ../qmodem/.github/workflows/openwrt_package.config >> .config
          make defconfig
          make download -j$(nproc) || true

      - name: Compile QModem
        id: compile
        run: |
          cd sdk
          ./scripts/feeds update qmodem
          ./scripts/feeds install -a -p qmodem
          generic_package=$(cat ../qmodem/.github/workflows/qmodem_package_generic)
          arch_package=$(cat ../qmodem/.github/workflows/qmodem_package_arch)
          for package in $generic_package; do
            make package/$package/compile -j$(nproc) || true
          done
          for package in $arch_package; do
            make package/$package/compile -j$(nproc) || true
          done
          ls bin/packages/${{ env.SDK_ARCH }}/qmodem/
          echo "status=success" >> $GITHUB_OUTPUT
          echo "FIRMWARE=$(pwd)/bin/packages/${{ env.SDK_ARCH }}/qmodem/" >> $GITHUB_ENV

      - name: Upload QModem
        if: ${{ steps.compile.outputs.status == 'success' }}
        uses: actions/upload-artifact@v4
        with:
          name: QModem-${{ matrix.build_arch }}
          path: ${{ env.FIRMWARE }}*
          retention-days: 7

  job_release_artifacts:
    name: Release Artifacts
    needs: [job_build_qmodem, job_prepare]
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
        
      - name: Download Release Note
        uses: actions/download-artifact@v4
        with:
          name: Release Note
          path: release_note

      - name: Create releases directory
        run: mkdir -p releases

      - name: Package artifacts into tar.gz
        run: |
          for dir in artifacts/*; do
            if [ -d "$dir" ]; then
              base_name=$(basename "$dir")
              tar -czf "releases/${base_name}.tar.gz" -C "$dir" .
            fi
          done

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        if: ${{ needs.job_prepare.outputs.push_type == 'tag' }}
        with:
          body_path: release_note/release_note.md
          generate_release_notes: true
          tag_name: ${{ needs.job_prepare.outputs.tag_name }}
          prerelease: ${{ contains(needs.job_prepare.outputs.tag_name, '-beta') || contains(needs.job_prepare.outputs.tag_name, '-rc') }}
          files: releases/*
