###############################################################################
# .gitlab-ci.yml — CI pipeline for linux-scripts repository
#
# All scripts are tested on every push:
#   1. lint  — ShellCheck + bash syntax + PowerShell syntax
#   2. test  — --help and --dry-run validation on Ubuntu and Rocky Linux
#
# On success on master, scripts are ready to sync to the website.
###############################################################################

stages:
  - lint
  - test

variables:
  # Start at "error" for a clean baseline, tighten to "warning" as scripts are cleaned up
  SHELLCHECK_SEVERITY: "error"

# ─────────────────────────────────────────────
# Lint Stage
# ─────────────────────────────────────────────

shellcheck:
  stage: lint
  image: koalaman/shellcheck-alpine:stable
  script:
    - echo "Running ShellCheck on $(find . -name '*.sh' -not -path './.git/*' | wc -l) scripts..."
    - find . -name "*.sh" -not -path "./.git/*" -print0 |
        xargs -0 -r shellcheck --severity="$SHELLCHECK_SEVERITY" --format=tty
    - echo "ShellCheck passed"

bash-syntax:
  stage: lint
  image: bash:5
  script:
    - echo "Checking bash syntax..."
    - |
      errors=0
      total=0
      for script in $(find . -name "*.sh" -not -path "./.git/*"); do
        total=$((total + 1))
        if ! bash -n "$script" 2>&1; then
          errors=$((errors + 1))
        fi
      done
      if [ "$errors" -gt 0 ]; then
        echo "FAILED: $errors/$total script(s) have syntax errors"
        exit 1
      fi
      echo "All $total scripts pass syntax check"

powershell-syntax:
  stage: lint
  image: mcr.microsoft.com/powershell:lts-ubuntu-22.04
  script:
    - echo "Checking PowerShell syntax..."
    - |
      errors=0
      total=0
      for script in $(find . -name "*.ps1" -not -path "./.git/*"); do
        total=$((total + 1))
        echo "Checking: $script"
        if ! pwsh -Command "try { \$null = [System.Management.Automation.Language.Parser]::ParseFile('$script', [ref]\$null, [ref]\$null); Write-Host 'OK: $script' } catch { Write-Error \$_; exit 1 }" 2>&1; then
          errors=$((errors + 1))
        fi
      done
      if [ "$errors" -gt 0 ]; then
        echo "FAILED: $errors/$total PowerShell script(s) have syntax errors"
        exit 1
      fi
      echo "All $total PowerShell scripts pass syntax check"
  rules:
    - exists:
        - "*.ps1"

# ─────────────────────────────────────────────
# Test Stage — Ubuntu 24.04
# ─────────────────────────────────────────────

test-ubuntu:
  stage: test
  image: ubuntu:24.04
  before_script:
    - apt-get update -qq
    - apt-get install -y -qq procps iproute2 kmod >/dev/null 2>&1
  script:
    - echo "=== Testing --help flags on Ubuntu 24.04 ==="
    - |
      passed=0
      failed=0
      for script in $(find . -maxdepth 1 -name "*.sh" -not -path "./.git/*" | sort); do
        name=$(basename "$script")
        if bash "$script" --help >/dev/null 2>&1; then
          echo "✓ $name --help"
          passed=$((passed + 1))
        elif bash "$script" -h >/dev/null 2>&1; then
          echo "✓ $name -h"
          passed=$((passed + 1))
        else
          echo "○ $name (no --help flag)"
        fi
      done
      echo ""
      echo "$passed scripts have working --help"
    - echo ""
    - echo "=== Testing networktuning.sh --dry-run ==="
    - bash networktuning.sh --dry-run 2>&1 || true

# ─────────────────────────────────────────────
# Test Stage — Rocky Linux 9
# ─────────────────────────────────────────────

test-rhel:
  stage: test
  image: rockylinux:9
  before_script:
    - dnf install -y -q procps iproute kmod >/dev/null 2>&1
  script:
    - echo "=== Testing --help flags on Rocky Linux 9 ==="
    - |
      passed=0
      for script in $(find . -maxdepth 1 -name "*.sh" -not -path "./.git/*" | sort); do
        name=$(basename "$script")
        if bash "$script" --help >/dev/null 2>&1; then
          echo "✓ $name --help"
          passed=$((passed + 1))
        elif bash "$script" -h >/dev/null 2>&1; then
          echo "✓ $name -h"
          passed=$((passed + 1))
        else
          echo "○ $name (no --help flag)"
        fi
      done
      echo ""
      echo "$passed scripts have working --help"
    - echo ""
    - echo "=== Testing networktuning.sh --dry-run ==="
    - bash networktuning.sh --dry-run 2>&1 || true
