name: Build

on:
  push:
    branches: [ "main" ]
    paths-ignore:
      - '**/README.md'
  pull_request:
    branches: [ "main" ]
    paths-ignore:
      - '**/README.md'

env:
  BUILD_NUMBER: ${{ github.run_number }}
  PROJECT_PATH_CS2_SIMPLEADMIN: "CS2-SimpleAdmin/CS2-SimpleAdmin.csproj"
  PROJECT_PATH_CS2_SIMPLEADMINAPI: "CS2-SimpleAdminApi/CS2-SimpleAdminApi.csproj"
  PROJECT_NAME_CS2_SIMPLEADMIN: "CS2-SimpleAdmin"
  PROJECT_NAME_CS2_SIMPLEADMINAPI: "CS2-SimpleAdminApi"
  OUTPUT_PATH: "./counterstrikesharp"
  TMP_PATH: "./tmp"

jobs:
  build:
    permissions: write-all
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 8.0.x
    - name: Restore CS2-SimpleAdmin
      run: dotnet restore ${{ env.PROJECT_PATH_CS2_SIMPLEADMIN }}
    - name: Build CS2-SimpleAdmin
      run: dotnet build ${{ env.PROJECT_PATH_CS2_SIMPLEADMIN }} -c Release -o ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}
    - name: Restore CS2-SimpleAdminApi
      run: dotnet restore ${{ env.PROJECT_PATH_CS2_SIMPLEADMINAPI }}
    - name: Build CS2-SimpleAdminApi
      run: dotnet build ${{ env.PROJECT_PATH_CS2_SIMPLEADMINAPI }} -c Release -o ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }}

  publish:
    if: github.event_name == 'push'
    permissions: write-all
    runs-on: ubuntu-latest
    needs: build
    steps:
    - uses: actions/checkout@v4
    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 8.0.x
    - name: Restore CS2-SimpleAdmin
      run: dotnet restore ${{ env.PROJECT_PATH_CS2_SIMPLEADMIN }}
    - name: Build CS2-SimpleAdmin
      run: dotnet build ${{ env.PROJECT_PATH_CS2_SIMPLEADMIN }} -c Release -o ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}
    - name: Restore CS2-SimpleAdminApi
      run: dotnet restore ${{ env.PROJECT_PATH_CS2_SIMPLEADMINAPI }}
    - name: Build CS2-SimpleAdminApi
      run: dotnet build ${{ env.PROJECT_PATH_CS2_SIMPLEADMINAPI }} -c Release -o ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }}
    - name: Clean files
      run: |
        rm -f \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}/CounterStrikeSharp.API.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}/McMaster.NETCore.Plugins.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}/Microsoft.DotNet.PlatformAbstractions.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}/Microsoft.Extensions.DependencyModel.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}/CS2-SimpleAdminApi.* \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}/Microsoft.* \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }}/CounterStrikeSharp.API.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }}/McMaster.NETCore.Plugins.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }}/Microsoft.DotNet.PlatformAbstractions.dll \
          ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }}/Microsoft.Extensions.DependencyModel.dll
    - name: Combine projects
      run: |
        mkdir -p ${{ env.OUTPUT_PATH }}/plugins
        mkdir -p ${{ env.OUTPUT_PATH }}/shared
        cp -r ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }} ${{ env.OUTPUT_PATH }}/plugins/
        cp -r ${{ env.TMP_PATH }}/${{ env.PROJECT_NAME_CS2_SIMPLEADMINAPI }} ${{ env.OUTPUT_PATH }}/shared/
    - name: Zip combined
      uses: thedoctor0/zip-release@0.7.6
      with:
        type: 'zip'
        filename: '${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}-${{ env.BUILD_NUMBER }}.zip'
        path: ${{ env.OUTPUT_PATH }}
    - name: Publish combined release
      uses: ncipollo/release-action@v1.14.0
      with:
        artifacts: "${{ env.PROJECT_NAME_CS2_SIMPLEADMIN }}-${{ env.BUILD_NUMBER }}.zip"
        name: "CS2-SimpleAdmin - Build ${{ env.BUILD_NUMBER }}"
        tag: "build-${{ env.BUILD_NUMBER }}"
        body: |
          Place files in addons/counterstrikesharp
          After first launch, configure the plugins in the respective configs:
          - CS2-SimpleAdmin: addons/counterstrikesharp/configs/plugins/CS2-SimpleAdmin/CS2-SimpleAdmin.json
