
name: Compile The New Version OpenClash

on: 
  push:
    branches:
      - dev
    paths:
      - 'luci-app-openclash/Makefile'
  workflow_dispatch:

jobs:
  Get-Version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      current_version: ${{ steps.current_version.outputs.version }}
    steps:
      - name: Clone Repository
        uses: actions/checkout@v3
        with:
          ref: ${{ github.ref_name }}

      - name: New Version
        id: version
        run: |
          echo "version=$(grep 'PKG_VERSION:=' ./luci-app-openclash/Makefile |awk -F '=' '{print $2}')" >> $GITHUB_OUTPUT
          echo "New Version: $(grep 'PKG_VERSION:=' ./luci-app-openclash/Makefile |awk -F '=' '{print $2}')"

      - name: Clone Repository
        uses: actions/checkout@v3
        with:
          ref: package

      - name: Current Version
        id: current_version
        run: |
          echo "version=$(sed -n 1p ./${{ github.ref_name }}/version |awk -F '-' '{print $1}' |awk -F 'v' '{print $2}')" >> $GITHUB_OUTPUT
          echo "Current Version: $(sed -n 1p ./${{ github.ref_name }}/version |awk -F '-' '{print $1}' |awk -F 'v' '{print $2}')"

  Compile:
    runs-on: ubuntu-latest
    needs: Get-Version
    if: ${{ needs.Get-Version.outputs.version != needs.Get-Version.outputs.current_version }}
    steps:
      - name: Clone OpenClash dev Repository
        uses: actions/checkout@v3
        with:
          ref: ${{ github.ref_name }}

      - name: Apt Update
        env:
          DEBIAN_FRONTEND: noninteractive
        run: |
          sudo apt-get update
          sudo apt-get -y install curl git tar

      - name: Install OpenWrt SDK
        run: |
          cd ..
          mkdir tmp
          curl -SLk --connect-timeout 30 --retry 2 "https://archive.openwrt.org/snapshots/trunk/ar71xx/generic/OpenWrt-SDK-ar71xx-generic_gcc-5.3.0_musl-1.1.16.Linux-x86_64.tar.bz2" -o "./tmp/SDK.tar.bz2"
          cd \tmp
          tar xjf SDK.tar.bz2
          mv "OpenWrt-SDK-ar71xx-generic_gcc-5.3.0_musl-1.1.16.Linux-x86_64" "SDK"

      - name: Change OpenClash Source Codes Version
        run: |
          echo "v${{ needs.Get-Version.outputs.version }}-beta" > ./version
          echo "https://img.shields.io/badge/New Release-v${{ needs.Get-Version.outputs.version }}--beta-orange.svg" >> ./version

      - name: Copy OpenClash Source Codes
        run: |
          cd ..
          mkdir tmp/SDK/package/luci-app-openclash
          cp -rf "./OpenClash/luci-app-openclash/." "./tmp/SDK/package/luci-app-openclash/"

      - name: Compile po2lmo
        run: |
          cd ..
          cd tmp/SDK/package/
          pushd luci-app-openclash/tools/po2lmo
          make && sudo make install
          popd

      - name: Compile OpenClash IPK
        run: |
          cd ..
          cd tmp/SDK
          make package/luci-app-openclash/compile V=99

      - name: Commit and Push New Version
        run: |
          rm -rf ./luci-app-openclash_*
          cd ..
          cp "./tmp/SDK/bin/ar71xx/packages/base/luci-app-openclash_${{ needs.Get-Version.outputs.version }}-beta_all.ipk" "./OpenClash/luci-app-openclash_${{ needs.Get-Version.outputs.version }}-beta_all.ipk"
          cd \OpenClash
          git config user.name 'github-actions[bot]'
          git config user.email 'github-actions[bot]@users.noreply.github.com'
          git add .
          git commit -m "Auto Release: v${{ needs.Get-Version.outputs.version }}-beta"
          git push

      - name: Switch OpenClash Repository to Package
        uses: actions/checkout@v3
        with:
          ref: package

      - name: Commit and Push New Version
        run: |
          rm -rf ./${{ github.ref_name }}/luci-app-openclash_*
          echo "v${{ needs.Get-Version.outputs.version }}-beta" > ./${{ github.ref_name }}/version
          echo "https://img.shields.io/badge/New Release-v${{ needs.Get-Version.outputs.version }}--beta-orange.svg" >> ./${{ github.ref_name }}/version
          cd ..
          cp "./tmp/SDK/bin/ar71xx/packages/base/luci-app-openclash_${{ needs.Get-Version.outputs.version }}-beta_all.ipk" "./OpenClash/${{ github.ref_name }}/luci-app-openclash_${{ needs.Get-Version.outputs.version }}-beta_all.ipk"
          sed -i -E "s/OpenClash\/tree\/v(.*)-beta/OpenClash\/tree\/v${{ needs.Get-Version.outputs.version }}-beta/g" ./OpenClash/${{ github.ref_name }}/README.md
          sed -i -E "s/OpenClash\/releases\/tag\/v(.*)-beta/OpenClash\/releases\/tag\/v${{ needs.Get-Version.outputs.version }}-beta/g" ./OpenClash/${{ github.ref_name }}/README.md
          sed -i -E "s/source code-v(.*)--beta-green/source code-v${{ needs.Get-Version.outputs.version }}--beta-green/g" ./OpenClash/${{ github.ref_name }}/README.md
          sed -i -E "s/New Release-v(.*)--beta-orange/New Release-v${{ needs.Get-Version.outputs.version }}--beta-orange/g" ./OpenClash/${{ github.ref_name }}/README.md
          cd \OpenClash
          git config user.name 'github-actions[bot]'
          git config user.email 'github-actions[bot]@users.noreply.github.com'
          git add .
          git commit -m "Auto Release: v${{ needs.Get-Version.outputs.version }}-beta"
          git push
