name: Build

on:
  push:
    branches: [ "main", "master" ]
  pull_request:
    branches: [ "main", "master" ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive
        fetch-depth: 0
        fetch-tags: true

    - name: Configure version information
      id: calculate-version
      if: ${{ github.event_name == 'push' }}
      shell: bash
      run: |
        git fetch --tags --force
        
        if LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null); then
            RANGE="${LAST_TAG}..HEAD"
        else
            LAST_TAG="v0.1.9"
            RANGE="HEAD"
        fi
        
        IFS='.' read -r major minor patch <<< "${LAST_TAG#v}"
        NEW_VERSION="$major.$minor.$((patch + 1))"
        
        CHANGELOG=$(git log $RANGE --oneline | sed 's/^/* /')
        if [ -z "$CHANGELOG" ]; then
          CHANGELOG="Re-run of version $NEW_VERSION or maintenance build."
        fi
        
        echo "version-string=$NEW_VERSION" >> $GITHUB_OUTPUT
        echo "new-tag=v$NEW_VERSION" >> $GITHUB_OUTPUT
        {
          echo "changelog<<EOF"
          echo "## What's Changed"
          echo "$CHANGELOG"
          echo "EOF"
        } >> $GITHUB_OUTPUT

    - name: Setup Tools Cache
      id: tools-cache
      uses: actions/cache@v4
      with:
        path: |
          cmake-3.30.5-linux-x86_64
          ccache
        key: ${{ runner.os }}-tools-${{ hashFiles('**/CMakeLists.txt') }}

    - name: Install tools
      run: |
        sudo apt-get update
        sudo apt-get install -y mingw-w64 zstd ccache zip
        mkdir -p ccache
        echo "CCACHE_DIR=$GITHUB_WORKSPACE/ccache" >> $GITHUB_ENV

    - name: Install CMake 3.30
      if: steps.tools-cache.outputs.cache-hit != 'true'
      run: |
        wget -q https://github.com/Kitware/CMake/releases/download/v3.30.5/cmake-3.30.5-linux-x86_64.tar.gz
        tar xzf cmake-3.30.5-linux-x86_64.tar.gz

    - name: Add CMake to path
      run: echo "$GITHUB_WORKSPACE/cmake-3.30.5-linux-x86_64/bin" >> $GITHUB_PATH

    - name: Get OpenSSL for MinGW (pre-built from MSYS2)
      run: |
        if [ ! -d "/usr/x86_64-w64-mingw32/include/openssl" ]; then
          wget -q "https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-openssl-3.3.2-1-any.pkg.tar.zst"
          tar xf mingw-w64-x86_64-openssl-3.3.2-1-any.pkg.tar.zst
          sudo cp -r mingw64/include/openssl /usr/x86_64-w64-mingw32/include/
          sudo cp mingw64/lib/libssl.a mingw64/lib/libcrypto.a /usr/x86_64-w64-mingw32/lib/
        fi

    - name: Patch missing MinGW headers
      run: |
        [ -f /usr/x86_64-w64-mingw32/include/consoleapi.h ] || \
          printf '#pragma once\n#include <windows.h>\n' | \
          sudo tee /usr/x86_64-w64-mingw32/include/consoleapi.h > /dev/null

    - name: Configure CMake
      run: |
        cmake -B build -S . \
          -DCMAKE_SYSTEM_NAME=Windows \
          -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \
          -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ \
          -DCMAKE_RC_COMPILER=x86_64-w64-mingw32-windres \
          -DCMAKE_C_COMPILER_LAUNCHER=ccache \
          -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
          -DOPENSSL_ROOT_DIR=/usr/x86_64-w64-mingw32 \
          -DOPENSSL_INCLUDE_DIR=/usr/x86_64-w64-mingw32/include \
          -DOPENSSL_LIBRARIES="/usr/x86_64-w64-mingw32/lib/libssl.a;/usr/x86_64-w64-mingw32/lib/libcrypto.a"

    - name: Build Unlocker
      run: cmake --build build --target dbd-unlocker

    - name: Package Build
      run: |
        EXE=$(find build -name "*.exe" -not -path "*/CMakeFiles/*" | head -1)
        echo "Found EXE: $EXE"
        mkdir -p release
        cp "$EXE" release/
        cp res/*.json release/
        cd release && zip -j ../unlocker.zip *

    - name: Upload Artifact
      uses: actions/upload-artifact@v3
      with:
        name: unlocker-build
        path: unlocker.zip

    - name: Finalize Version and Push Tag
      if: ${{ github.event_name == 'push' && success() }}
      run: |
        git tag ${{ steps.calculate-version.outputs.new-tag }}
        git push origin ${{ steps.calculate-version.outputs.new-tag }}

    - name: Create Gitea Release
      if: ${{ github.event_name == 'push' && success() }}
      uses: akkuman/gitea-release-action@v1
      with:
        files: unlocker.zip
        tag_name: ${{ steps.calculate-version.outputs.new-tag }}
        name: Release ${{ steps.calculate-version.outputs.new-tag }}
        body: ${{ steps.calculate-version.outputs.changelog }}
        