name: Build OpenWrt LuCI Package

on:
  push:
    branches: [ main, master ]
    tags: [ 'v*' ]
  pull_request:
    branches: [ main, master ]
  workflow_dispatch:

env:
  PKG_NAME: luci-app-secubox
  OPENWRT_VERSION: '23.05.5'

jobs:
  validate:
    name: Validate
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Check structure
        run: |
          test -f Makefile
          grep -q "PKG_NAME:=luci-app-secubox" Makefile
          find . -name "*.json" -exec python3 -m json.tool {} \; >/dev/null

  build:
    name: Build ${{ matrix.arch }}
    needs: validate
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86_64
            target: x86
            subtarget: 64
          - arch: aarch64_cortex-a53
            target: mvebu
            subtarget: cortexa53
          - arch: aarch64_cortex-a72
            target: mvebu
            subtarget: cortexa72
          - arch: arm_cortex-a9_vfpv3-d16
            target: mvebu
            subtarget: cortexa9

    steps:
      - uses: actions/checkout@v4

      - name: Setup environment
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential clang flex bison g++ gawk \
            gcc-multilib gettext git libncurses5-dev libssl-dev \
            python3-setuptools rsync unzip zlib1g-dev file wget xsltproc

      - name: Download and extract SDK
        run: |
          SDK_BASE="https://downloads.openwrt.org/releases/${{ env.OPENWRT_VERSION }}/targets/${{ matrix.target }}/${{ matrix.subtarget }}"
          wget -q "${SDK_BASE}/sha256sums"
          SDK_FILE=$(grep -E "openwrt-sdk.*\.tar\.(xz|zst)" sha256sums | head -1 | awk '{print $NF}' | tr -d '*')
          [ -z "$SDK_FILE" ] && { echo "SDK not found"; exit 1; }
          wget -q "${SDK_BASE}/${SDK_FILE}"
          case "$SDK_FILE" in
            *.tar.xz) tar -xJf "$SDK_FILE" ;;
            *.tar.zst) tar --zstd -xf "$SDK_FILE" ;;
          esac
          SDK_DIR=$(find . -maxdepth 1 -type d -name "openwrt-sdk-*" -print -quit)
          mv "$SDK_DIR" sdk

      - name: Build package
        run: |
          cd sdk
          grep -q "^src-git luci" feeds.conf.default || \
            echo "src-git luci https://github.com/openwrt/luci.git;openwrt-23.05" >> feeds.conf.default
          
          # Création explicite de feeds.conf avec miroirs GitHub
          cat > feeds.conf << 'EOF'
          src-git base https://github.com/openwrt/openwrt.git;openwrt-23.05
          src-git packages https://github.com/openwrt/packages.git;openwrt-23.05
          src-git luci https://github.com/openwrt/luci.git;openwrt-23.05
          src-git routing https://github.com/openwrt/routing.git;openwrt-23.05
          EOF
          
          # Retry logic
          for attempt in 1 2 3; do
              ./scripts/feeds update -a && break
              sleep $((30 * attempt))
          done
          ./scripts/feeds install -a
          
          mkdir -p "package/${{ env.PKG_NAME }}"
          rsync -av --exclude='.git' --exclude='sdk' --exclude='*.tar.*' ../. "package/${{ env.PKG_NAME }}/"
          
          make defconfig
          make "package/${{ env.PKG_NAME }}/compile" V=s -j$(nproc) || \
            make "package/${{ env.PKG_NAME }}/compile" V=s -j1

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.PKG_NAME }}-${{ matrix.arch }}
          path: sdk/bin/**/*.ipk
          if-no-files-found: error

  release:
    needs: build
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true
      - uses: softprops/action-gh-release@v1
        with:
          files: artifacts/**/*.ipk
          generate_release_notes: true
