name: wrapper-qemu

on:
   workflow_run:
     workflows: ["Build"]
     types: [completed]

jobs:
   on-success:
     runs-on: ubuntu-latest
     if: ${{ github.event.workflow_run.conclusion == 'success' }}
     steps:
       - uses: actions/checkout@v4

       - name: Download wrapper-qemu basic image
         run: wget https://github.com/zhaarey/wrapper/releases/download/wrapper-qemu/wrapper.qcow2

       - name: Mount image
         run: |
           sudo apt-get install -y qemu-utils
           sudo modprobe nbd max_part=8
           sudo qemu-nbd --connect=/dev/nbd0 wrapper.qcow2
           sudo mkdir /mnt/wrapper
           sudo mount /dev/nbd0p3 /mnt/wrapper/

       - name: Download latest artifact
         uses: dawidd6/action-download-artifact@v6
         with:
            github_token: ${{secrets.ACTION_TOKEN}}
            run_id: ${{ github.event.workflow_run.id }}
      
       - name: Copy wrapper to image
         run: |
           mv Wrapper.x86_64.* wrapper
           sudo mv wrapper /mnt/wrapper/root/wrapper
           sudo chmod +x /mnt/wrapper/root/wrapper/wrapper
           
       - name: Unmount image
         run: |
           sudo umount /mnt/wrapper/
           sudo qemu-nbd --disconnect /dev/nbd0
      
       - name: Upload artifact
         uses: actions/upload-artifact@v4
         with:
           name: wrapper-qemu
           path: wrapper.qcow2
