name: release cutechess
on:
  push:
    tags:
      - 'v*'

jobs:
    githubrel:
      name: "Create GitHub release"
      runs-on: ubuntu-latest
      outputs:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
      steps:
        - uses: actions/checkout@v4

        - name: set version
          run: echo "VERSION=$(cat .version)" >> $GITHUB_ENV

        - name: create a draft github release
          id: create_release
          uses: actions/create-release@v1
          env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          with:
            tag_name: ${{ github.ref }}
            release_name: Cute Chess ${{ env.VERSION }}
            draft: true

    linuxrel:
        needs: githubrel
        name: "${{ matrix.os }} Qt ${{ matrix.qt_version }}"
        runs-on: ${{ matrix.os }}
        strategy:
          matrix:
            qt_version: [6.7.2]
            os: [ubuntu-latest]
        steps:
            - uses: actions/checkout@v4

            - name: install qt 6.x
              uses: jurplel/install-qt-action@v3
              with:
                version: ${{ matrix.qt_version }}
                modules: 'qt5compat'

            - name: build cute chess
              run: |
                  mkdir build
                  cd build
                  cmake .. -DWITH_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr
                  cmake --build .
                  make install DESTDIR=AppDir

            - name: download appimage tools
              run: |
                sudo apt install libfuse2 libxcb-cursor0
                cd build
                curl -fsSLO https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
                curl -fsLSO https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-x86_64.AppImage
                chmod +x linuxdeploy-*.AppImage

            - name: set version
              run: echo "VERSION=$(cat .version)" >> $GITHUB_ENV

            - name: build appimage
              run: |
                cd build
                ./linuxdeploy-x86_64.AppImage --appdir AppDir --plugin qt --output appimage --custom-apprun=../dist/appimage/CuteChess-AppRun --icon-file=../projects/gui/res/icons/cutechess_256x256.png --executable cutechess --executable cutechess-cli

            - name: upload release asset
              uses: actions/upload-release-asset@v1
              env:
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                upload_url: ${{ needs.githubrel.outputs.upload_url }}
                asset_path: build/Cute_Chess-${{ env.VERSION }}-x86_64.AppImage
                asset_name: Cute_Chess-${{ env.VERSION }}-x86_64.AppImage
                asset_content_type: application/octet-stream

    winrel:
        needs: githubrel
        name: "${{ matrix.os }} Qt ${{ matrix.qt_version }}"
        runs-on: ${{ matrix.os }}
        env:
          VCDIR: 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise'
          ISDIR: 'C:\Program Files (x86)\Inno Setup 6'
        strategy:
          matrix:
            qt_version: [6.7.2]
            os: [windows-latest]
        steps:
            - uses: actions/checkout@v4

            - name: install qt 6.x
              uses: jurplel/install-qt-action@v3
              with:
                version: ${{ matrix.qt_version }}
                modules: 'qt5compat'

            - name: build cute chess
              run: |
                  mkdir build
                  cd build
                  cmake .. -DWITH_TESTS=OFF
                  cmake --build . --config release

            - name: set environment
              run: |
                chcp 65001 # set code page to utf-8
                $version = Get-Content -Path .version
                echo ("VERSION=" + $version) >> $env:GITHUB_ENV

            - name: create a release zip file
              run: |
                mkdir cutechess-$env:VERSION-win64
                cd cutechess-$env:VERSION-win64
                cp ..\build\Release\cutechess.exe .
                cp ..\build\Release\cutechess-cli.exe .
                cp ..\docs\cutechess-cli.6.html .
                cp ..\docs\cutechess-cli.6.txt .
                cp ..\docs\cutechess-engines.json.5.html .
                cp ..\docs\cutechess-engines.json.5.txt .
                cp "$env:VCDIR\VC\Redist\MSVC\v143\vc_redist.x64.exe" .
                cp "$env:Qt6_Dir\bin\Qt6Core.dll" .
                cp "$env:Qt6_Dir\bin\Qt6Gui.dll" .
                cp "$env:Qt6_Dir\bin\Qt6Svg.dll" .
                cp "$env:Qt6_Dir\bin\Qt6PrintSupport.dll" .
                cp "$env:Qt6_Dir\bin\Qt6Widgets.dll" .
                cp "$env:Qt6_Dir\bin\Qt6ConCurrent.dll" .
                cp "$env:Qt6_Dir\bin\Qt6Core5Compat.dll" .
                mkdir platforms
                cp "$env:Qt6_Dir\plugins\platforms\qwindows.dll" platforms\
                mkdir styles
                cp "$env:Qt6_Dir\plugins\styles\qmodernwindowsstyle.dll" styles\
                mkdir plugins
                mkdir plugins\iconengines
                cp "$env:Qt6_Dir\plugins\iconengines\qsvgicon.dll" plugins\iconengines
                mkdir plugins\imageformats
                cp "$env:Qt6_Dir\plugins\imageformats\qsvg.dll" plugins\imageformats
                cd ..
                7z a cutechess-$env:VERSION-win64.zip cutechess-$env:VERSION-win64\

            - name: create an installer
              run: |
                & "$env:ISDIR\iscc.exe" /DMyAppVersion=$env:VERSION /DQtLibPath=$env:Qt6_Dir /DMSVCPath=$env:VCDIR /DCuteChessPath=$env:GITHUB_WORKSPACE dist\windows_setup.iss

            - name: upload zip release asset
              uses: actions/upload-release-asset@v1
              env:
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                upload_url: ${{ needs.githubrel.outputs.upload_url }}
                asset_path: cutechess-${{ env.VERSION }}-win64.zip
                asset_name: cutechess-${{ env.VERSION }}-win64.zip
                asset_content_type: application/zip

            - name: upload installer release asset
              uses: actions/upload-release-asset@v1
              env:
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                upload_url: ${{ needs.githubrel.outputs.upload_url }}
                asset_path: dist/cutechess-${{ env.VERSION }}-win64.exe
                asset_name: cutechess-${{ env.VERSION }}-win64.exe
                asset_content_type: application/octet-stream
