name: Lint Scripts
on:
  push:
    branches: [main]
  pull_request:

jobs:
  shellcheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: ShellCheck — all .sh files
        run: |
          echo "::group::Running ShellCheck"
          failed=0
          for f in *.sh; do
            [ -f "$f" ] || continue
            if ! shellcheck -S error "$f" 2>&1; then
              failed=1
            fi
          done
          echo "::endgroup::"
          if [ "$failed" -eq 1 ]; then
            echo "::error::ShellCheck found errors (severity: error)"
            exit 1
          fi
          echo "All shell scripts passed ShellCheck (error severity)"

  powershell-lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: PowerShell syntax check
        run: |
          echo "::group::PowerShell syntax check"
          failed=0
          for f in *.ps1; do
            [ -f "$f" ] || continue
            if ! pwsh -NoProfile -Command "
              \$null = [System.Management.Automation.Language.Parser]::ParseFile(
                (Resolve-Path '$f'),
                [ref]\$null,
                [ref]\$errors
              )
              if (\$errors.Count -gt 0) {
                \$errors | ForEach-Object { Write-Error \$_.Message }
                exit 1
              }
            " 2>&1; then
              echo "FAIL: $f"
              failed=1
            fi
          done
          echo "::endgroup::"
          if [ "$failed" -eq 1 ]; then
            echo "::error::PowerShell syntax errors found"
            exit 1
          fi
          echo "All PowerShell scripts passed syntax check"
