name: "Build Theme Alpha | OpenWrt SDK"
on:
  repository_dispatch:
  workflow_dispatch:
    inputs:
      ssh:
        description: 'SSH connection to Actions'
        required: false
        default: 'false'
  push:
    branches:
      - 'master'
    paths:
      - 'luci-theme-alpha/Makefile'
env:
  TZ: Asia/Jakarta


jobs:
  job_check:
    if: github.repository == ${{ github.repository }}
    name: Check Version
    runs-on: ubuntu-latest
    outputs:
      alpha_version: ${{ steps.check_version.outputs.latest_version }}
      has_update: ${{ steps.check_version.outputs.has_update }}
    steps:
      - name: Checkout
        uses: actions/checkout@main
        with:
          fetch-depth: 0
          ref: 'master'

      - name: Check version
        id: check_version
        env:
          url_release: https://api.github.com/repos/${{ github.repository }}/releases/latest
        run: |
          latest_version=$(grep -oP 'PKG_VERSION:=\K.*' Makefile | sed 's/^/v/')
          latest_release=$(wget -qO- -t1 -T2 ${{env.url_release}} | awk -F '"' '/tag_name/{print $4}')
          has_update=$([ "${latest_version}" != "${latest_release}" ] && echo true || echo false)
          echo "latest_version=${latest_version}" >> $GITHUB_OUTPUT
          echo "has_update=${has_update}" >> $GITHUB_OUTPUT
          echo "latest_version: ${latest_version}"
          echo "latest_release: ${latest_release}"
          echo "has_update: ${has_update}"
      - name: Generate new tag & release
        if: steps.check_version.outputs.has_update == 'true'
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          name: Alpha Theme - OpenWrt
          tag_name: ${{steps.check_version.outputs.latest_version}}


  job_build_alpha:
    name: Build Alpha Theme (master)
    needs: job_check
    if: needs.job_check.outputs.has_update == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Install packages
        run: |
          echo "Install packages"
          sudo -E apt-get -qq update
          sudo -E apt-get -qq install build-essential clang flex bison g++ gawk gcc-multilib g++-multilib gettext git libncurses5-dev libssl-dev python3-distutils rsync unzip zlib1g-dev file wget
          sudo -E apt-get -qq autoremove --purge
          sudo -E apt-get -qq clean
      - name: Cache openwrt SDK
        id: cache-sdk
        uses: actions/cache@v3
        with:
          path: sdk
          key: openwrt-sdk-21.02-x86-64

      - name: Initialization environment
        if: steps.cache-sdk.outputs.cache-hit != 'true'
        env:
          url_sdk: https://archive.openwrt.org/releases/21.02.5/targets/x86/64/openwrt-sdk-21.02.5-x86-64_gcc-8.4.0_musl.Linux-x86_64.tar.xz
        run: |
          wget ${{ env.url_sdk }}
          file_name=$(echo ${{env.url_sdk}} | awk -F/ '{print $NF}')
          mkdir sdk && tar -xJf $file_name -C ./sdk --strip-components=1
          cd sdk  
          echo "src-git base https://github.com/openwrt/openwrt.git;openwrt-21.02" > feeds.conf
          echo "src-git-full packages https://github.com/openwrt/packages.git;openwrt-21.02" >> feeds.conf
          echo "src-git-full luci https://git.openwrt.org/project/luci.git;openwrt-21.02" >> feeds.conf
          echo "src-git-full routing https://git.openwrt.org/feed/routing.git;openwrt-21.02"  >> feeds.conf
          git clone -b master https://github.com/${{ github.repository }}.git package/downloads/luci-theme-alpha
          ./scripts/feeds update -a
          echo "CONFIG_PACKAGE_luci-theme-alpha=m" > .config
          ./scripts/feeds install -d n luci-theme-alpha
          make download -j8
      - name: Configure Alpha Theme (master)
        run: |
          cd sdk
          ./scripts/feeds install luci-theme-alpha
          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=n" >> .config
          echo "CONFIG_PACKAGE_luci-theme-alpha=m" >> .config
          make defconfig
      - name: Compile Alpha Theme (master)
        id: compile
        run: |
          cd sdk
          echo "make package/luci-theme-alpha/{clean,compile} -j$(nproc)"
          make package/luci-theme-alpha/{clean,compile} -j$(nproc)
          mv bin/packages/x86_64/base/ ../
          rm .config .config.old
          cd ..
          echo "status=success" >> $GITHUB_OUTPUT
          echo "FIRMWARE=$PWD" >> $GITHUB_ENV
      - name: Upload Alpha Theme (master) ipks to release
        uses: softprops/action-gh-release@v1
        if: steps.compile.outputs.status == 'success'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{needs.job_check.outputs.alpha_version}}
          files: ${{ env.FIRMWARE }}/base/luci-theme*.ipk
