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@v3

        - 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.5.2]
            os: [ubuntu-latest]
        steps:
            - uses: actions/checkout@v3

            - 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: [5.15.2]
            os: [windows-latest]
        steps:
            - uses: actions/checkout@v3

            - name: install qt 5.x
              uses: jurplel/install-qt-action@v3
              if: startsWith(matrix.qt_version, '5.')
              with:
                version: ${{ matrix.qt_version }}

            - 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:Qt5_Dir\bin\Qt5Core.dll" .
                cp "$env:Qt5_Dir\bin\Qt5Gui.dll" .
                cp "$env:Qt5_Dir\bin\Qt5Svg.dll" .
                cp "$env:Qt5_Dir\bin\Qt5PrintSupport.dll" .
                cp "$env:Qt5_Dir\bin\Qt5Widgets.dll" .
                cp "$env:Qt5_Dir\bin\Qt5ConCurrent.dll" .
                mkdir platforms
                cp "$env:Qt5_Dir\plugins\platforms\qwindows.dll" platforms\
                mkdir styles
                cp "$env:Qt5_Dir\plugins\styles\qwindowsvistastyle.dll" styles\
                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:Qt5_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
