name: Build

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

jobs:
  build:
    runs-on: ubuntu-latest
    container: archlinux:latest
    steps:
    - name: Install Dependencies
      run: |
        pacman -Syu --noconfirm
        pacman -S --noconfirm nodejs mingw-w64-gcc cmake ninja ccache git base-devel

    - 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 config --global --add safe.directory $GITHUB_WORKSPACE
        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: Cache FetchContent & ccache
      uses: actions/cache@v3
      with:
        path: |
          build/_deps
          ccache
        key: ${{ runner.os }}-deps-${{ hashFiles('**/CMakeLists.txt') }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-deps-${{ hashFiles('**/CMakeLists.txt') }}-
          ${{ runner.os }}-deps-

    - name: Setup ccache
      run: |
        mkdir -p ccache
        echo "CCACHE_DIR=$GITHUB_WORKSPACE/ccache" >> $GITHUB_ENV

    - name: Configure CMake
      run: |
        cmake -B build -S . -G Ninja \
          -DCMAKE_BUILD_TYPE=Release \
          -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

    - name: Build Unlocker
      run: cmake --build build --target hex-unlocked

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

    - name: Upload Artifact
      uses: actions/upload-artifact@v3
      with:
        name: runner-build
        path: release/

    - 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: release/*
        tag_name: ${{ steps.calculate-version.outputs.new-tag }}
        name: Release ${{ steps.calculate-version.outputs.new-tag }}
        body: ${{ steps.calculate-version.outputs.changelog }}