name: PCem Release Build

on:
  workflow_dispatch:
    branches: [ dev ]
    inputs:
      logLevel:
        description: 'PCem Version Number'
        required: true
        default: 'v18'

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Ubuntu 64bit (gcc)
            os: ubuntu-latest
            args: -DCMAKE_BUILD_TYPE=Release -DPCEM_VERSION_STRING="${{ github.event.inputs.version }}"
            artifacts_name: PCem-${{ github.event.inputs.version }}-Ubuntu-${{ github.run_number }}
            artifacts_path: PCem-${{ github.event.inputs.version }}-Linux64.tar.bz2
            installdeps: >-
              libsdl2-dev
              libopenal-dev
              libwxgtk3.0-gtk3-dev
              libpcap-dev
              cmake
              ninja-build
          - name: Windows 32bits (MSYS2)
            os: windows-latest
            compiler: MINGW32
            args: -DCMAKE_BUILD_TYPE=Release -DPCEM_VERSION_STRING="${{ github.event.inputs.version }}"
            artifacts_name: PCem-${{ github.event.inputs.version }}-Windows-MINGW32-${{ github.run_number }}
            artifacts_path: PCem-${{ github.event.inputs.version }}-Windows32.zip
            installdeps: >-
              base-devel
              zip
              unzip
              mingw-w64-i686-ntldd-git
              mingw-w64-i686-toolchain 
              mingw-w64-i686-SDL2 
              mingw-w64-i686-openal 
              mingw-w64-i686-wxWidgets
              mingw-w64-i686-cmake
              mingw-w64-i686-libpcap
              mingw-w64-i686-ninja
          - name: Windows 64bits (MSYS2)
            os: windows-latest
            compiler: MINGW64
            args: -DCMAKE_BUILD_TYPE=Release -DPCEM_VERSION_STRING="${{ github.event.inputs.version }}"
            artifacts_name: PCem-${{ github.event.inputs.version }}-Windows-MINGW64-${{ github.run_number }}
            artifacts_path: PCem-${{ github.event.inputs.version }}-Windows64.zip
            installdeps: >-
              base-devel
              zip
              unzip
              mingw-w64-x86_64-ntldd-git
              mingw-w64-x86_64-toolchain
              mingw-w64-x86_64-SDL2
              mingw-w64-x86_64-openal
              mingw-w64-x86_64-wxWidgets
              mingw-w64-x86_64-cmake
              mingw-w64-x86_64-libpcap
              mingw-w64-x86_64-ninja
    runs-on: ${{ matrix.os }}
    name: ${{ matrix.name }}
    
    steps:
      - name: "Check out repository"
        uses: actions/checkout@v4.1.1
        with:
          path: temp
          fetch-depth: 0

      - name: Setup MSYS2 (Windows)
        if: runner.os == 'Windows'
        # You may pin to the exact commit or the version.
        # uses: msys2/setup-msys2@a43b8403533fffe0c157dd8498f021ddec66bff7
        uses: msys2/setup-msys2@v2
        with:
          # Variant of the environment to set by default: MSYS, MINGW32, MINGW64, UCRT64 or CLANG64
          msystem: ${{ matrix.compiler }}
          # Retrieve and extract base installation from upstream GitHub Releases
          release: false # optional, default is true
          # Update MSYS2 installation through pacman
          update: false
          # Install packages after installation through pacman
          install: ${{ matrix.installdeps }}
      - name: Setup ubuntu dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install ${{ matrix.installdeps }}

      # Copy all the sources to the dist folder, before we start generating intermediate files.
      - name: prepare-package (Windows)
        if: runner.os == 'Windows'
        shell: msys2 {0}
        run: |
          mkdir dist
          mkdir dist/src
          cp -R ./temp/* dist/src
          rm -Rf ./dist/src/.git
      - name: configure (Windows)
        if: runner.os == 'Windows'
        shell: msys2 {0}
        run: |
          export INSTALL_PREFIX=$(pwd)/dist
          mkdir temp/build
          cd temp/build
          cmake -G "Ninja" -DMSYS=TRUE -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX ${{ matrix.args }} ..
      - name: ninja (Windows)
        if: runner.os == 'Windows'
        shell: msys2 {0}
        run: |
          cd temp/build
          ninja
      - name: package (Windows)
        if: runner.os == 'Windows'
        shell: msys2 {0}
        run: |
          export INSTALL_PREFIX=$(pwd)/dist          
          export ARTIFACTSDIR=$(pwd)/artifacts
          mkdir -p $ARTIFACTSDIR
          cd temp/build
          ninja install
          cd $INSTALL_PREFIX
          ntldd -R "bin/pcem.exe" | sed -e 's/^[[:blank:]]*//g' | cut -d ' ' -f 3 | grep -E -i '(mingw|clang)(32|64)' | sed -e 's|\\|/|g' | xargs cp --target-directory="bin"
          zip -r -9 ${{ matrix.artifacts_path }} *
          cp ${{ matrix.artifacts_path }} $ARTIFACTSDIR

      - name: prepare-package (Linux)
        if: runner.os == 'Linux'
        run: |
          mkdir dist
          mkdir dist/src
          cp -R ./temp/* dist/src
          rm -Rf ./dist/src/.git
      - name: configure (Linux)
        if: runner.os == 'Linux'
        run: |
          mkdir temp/build
          cd temp/build
          cmake -G "Ninja" ${{ matrix.args }} ..
      - name: ninja (Linux)
        if: runner.os == 'Linux'
        run: |
          cd temp/build
          ninja
      - name: package (Linux)
        if: runner.os == 'Linux'
        run: |
          export DESTDIR=$(pwd)/dist
          export ARTIFACTSDIR=$(pwd)/artifacts
          mkdir -p $ARTIFACTSDIR
          cd temp/build
          ninja install
          cd $DESTDIR
          tar -cjf ${{ matrix.artifacts_path }} *
          cp ${{ matrix.artifacts_path }} $ARTIFACTSDIR

      - name: "Upload GitHub Actions artifacts"
        uses: actions/upload-artifact@v4.3.1
        with:
          name: ${{ matrix.artifacts_name }}
          path: ./artifacts
                  
      - name: Upload to NasuTek MinIO S3
        uses: shallwefootball/s3-upload-action@master
        id: S3
        with:
          aws_key_id: ${{secrets.nte_cdn_s3_key_id}}
          aws_secret_access_key: ${{secrets.nte_cdn_s3_secret_access_key}}
          aws_bucket: pcem-dev-builds
          source_dir: ./artifacts
          destination_dir: ''
          endpoint:  https://cdn.ntgecdn.com:443    
