name: Test WasmEdge on OpenWrt

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - master
    paths:
      - ".github/workflows/build_for_openwrt.yml"
      - "include/**"
      - "lib/**"
      - "plugins/**"
      - "test/**"
      - "thirdparty/**"
      - "tools/**"
      - "CMakeLists.txt"
      - "cmake/**"
  pull_request:
    branches:
      - master
      - 'proposal/**'
    paths:
      - ".github/workflows/build_for_openwrt.yml"
      - "include/**"
      - "lib/**"
      - "plugins/**"
      - "test/**"
      - "thirdparty/**"
      - "tools/**"
      - "CMakeLists.txt"
      - "cmake/**"

jobs:
  # TODO: Refactor `lint` with `on.workflow_run`
  # https://docs.github.com/en/actions/using-workflows/triggering-a-workflow
  lint:
    uses: ./.github/workflows/reusable-call-linter.yml

  build_openwrt:
    name: OpenWrt
    needs: lint
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Ensure git safe directory
      run: |
        git config --global --add safe.directory $(pwd)
    - name: Restore OpenWrt
      id: restore-openwrt
      uses: actions/cache/restore@v3
      with:
        path: ./openwrt-x86-64-generic-rootfs.tar.gz
        key: openwrt
    - name: Install dependency
      if: steps.restore-openwrt.outputs.cache-hit != 'true'
      run: |
        git clone https://github.com/openwrt/openwrt.git ../openwrt
        cd ../openwrt
        git checkout 9dc86d19628f55bfed2048c412c5b23bda328cff
    - name: Update feeds
      if: steps.restore-openwrt.outputs.cache-hit != 'true'
      run: |
        cd ../openwrt
        ./scripts/feeds update -a
        ./scripts/feeds install -a
    - name: Make WasmEdge package
      if: steps.restore-openwrt.outputs.cache-hit != 'true'
      run: |
        cd ../
        mkdir -p ./openwrt/package/utils/WasmEdge
        cp -r ./WasmEdge ./openwrt/package/utils/WasmEdge/src
        cp ./WasmEdge/utils/openwrt/configuration/Makefile ./openwrt/package/utils/WasmEdge/
        cd ./openwrt
        ls ./
        rm -f ./.config*
        touch ./.config
        cat >> .config <<EOF
        CONFIG_TARGET_IMAGES_GZIP=y
        CONFIG_TARGET_x86=y
        CONFIG_TARGET_x86_64=y
        CONFIG_TARGET_x86_64_DEVICE_generic=y
        CONFIG_HAS_SUBTARGETS=y
        CONFIG_HAS_DEVICES=y
        CONFIG_TARGET_BOARD="x86"
        CONFIG_TARGET_SUBTARGET="64"
        CONFIG_TARGET_PROFILE="DEVICE_generic"
        CONFIG_TARGET_ARCH_PACKAGES="x86_64"
        CONFIG_PACKAGE_libpthread=y
        CONFIG_PACKAGE_libstdcpp=y
        CONFIG_PACKAGE_WasmEdge=y
        EOF
        sed -i 's/^[ \t]*//g' ./.config
        make defconfig
        make download -j8 || make download -j1 V=s
        rm -rf $(find ./dl/ -size -1024c)
        make -j$(nproc) || make -j1 V=s
    - name: Pack OpenWrt images and WasmEdge ipk
      if: steps.restore-openwrt.outputs.cache-hit != 'true'
      run: |
        cp -rf $(find ../openwrt/bin/ -type f -name "openwrt-x86-64-generic-rootfs.tar.gz") ./
    - name: Save OpenWrt
      if: steps.restore-openwrt.outputs.cache-hit != 'true'
      uses: actions/cache/save@v3
      with:
        path: ./openwrt-x86-64-generic-rootfs.tar.gz
        key: ${{ steps.restore-openwrt.outputs.cache-primary-key }}
    - name: Upload artifact
      uses: actions/upload-artifact@v3
      with:
        name: openwrt-rootfs
        path: ./openwrt-x86-64-generic-rootfs.tar.gz

  build_openwrt_docker:
    name: OpenWrt-docker
    runs-on: ubuntu-latest
    needs: build_openwrt
    steps:
    - name: Download image
      uses: actions/download-artifact@v3
      with:
        name: openwrt-rootfs
        path: ./
    - name: make_openwrt_docker
      run: |
        mkdir -p ./openwrt-docker
        cp openwrt-x86-64-generic-rootfs.tar.gz ./openwrt-docker/
        cd openwrt-docker

        touch ./Dockerfile
        cat >> ./Dockerfile <<EOF
        FROM scratch
        ADD openwrt-x86-64-generic-rootfs.tar.gz /
        EXPOSE 22 80 443
        ENTRYPOINT ["/sbin/init"]
        EOF

        ls ./
        docker build -t openwrt .
        docker run --name openwrt -d --privileged openwrt:latest /sbin/init
        docker exec openwrt wasmedge -v

