name: Build Windows Release

on:
  release:
     types: [published]

permissions:
  contents: write

jobs:
  build:
    runs-on: windows-latest
    defaults:
      run:
        shell: msys2 {0}

    strategy:
      matrix:
        sys: [MINGW32, MINGW64, UCRT64]
        include:
          - sys: MINGW32
            arch: i686
            name: mingw32
            usb_libs:
            cfg_parms:
          - sys: MINGW64
            arch: x86-64
            name: mingw64
            usb_libs: libusb:p libftdi:p
            cfg_parms: --with-usbsid --with-exsid
          - sys: UCRT64
            arch: x86-64
            name: ucrt64
            usb_libs: libusb:p libftdi:p
            cfg_parms: --with-usbsid --with-exsid
    steps:
    - uses: msys2/setup-msys2@v2
      with:
        msystem: ${{matrix.sys}}
        update: true
        install: >-
          git
          zip
          upx
          base-devel
        pacboy: >-
          toolchain:p
          autotools:p
          ${{matrix.usb_libs}}
    - uses: dsaltares/fetch-gh-release-asset@master
      with:
        repo: 'libsidplayfp/libsidplayfp'
        regex: true
        file: "libsidplayfp-.*\\.tar\\.gz"
        target: 'subdir/'
    - name: build-lib
      run: |
        cd subdir
        export LDFLAGS="-s -Wl,-no-undefined -Wl,-O1 -static"
        export CXXFLAGS="-O2 -march=${{matrix.arch}} -fomit-frame-pointer -pipe -DUNICODE"
        export PKG_CONFIG="pkg-config --static"
        tar -xf libsidplayfp*.tar.gz
        cd libsidplayfp*/.
        ./configure --disable-dependency-tracking --disable-shared --enable-static --without-gcrypt --enable-hardsid ${{matrix.cfg_parms}} --with-simd=runtime
        make && make install
    - uses: actions/checkout@v4
      with:
        ref: ${{ github.ref }}
    - name: Get the tag name
      id: get_tag_name
      run: echo "tagname=${GITHUB_REF#refs/tags/}" | sed 's/v//' >> $GITHUB_OUTPUT
    - name: build
      run: |
        export LDFLAGS="-s -Wl,-no-undefined -Wl,-O1 -static"
        export CXXFLAGS="-O2 -march=${{matrix.arch}} -fomit-frame-pointer -pipe -DUNICODE"
        export PKG_CONFIG="pkg-config --static"
        autoreconf -vfi
        ./configure --disable-dependency-tracking
        make
        mv src/*.exe .
        strip *.exe
        upx *.exe
        pod2text doc/en/sidplayfp.ini.pod > sidplayfp.ini.txt
        pod2text doc/en/sidplayfp.pod > sidplayfp.txt
        pod2text doc/en/stilview.pod > stilview.txt
        zip sidplayfp-${{ steps.get_tag_name.outputs.tagname }}-${{matrix.name}}.zip *.exe README* AUTHORS *.txt
    - uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: sidplayfp-${{ steps.get_tag_name.outputs.tagname }}-${{matrix.name}}.zip
        tag: ${{ github.ref }}
        file_glob: true
        overwrite: true
