<?php
// app/services/ShellTool.php

declare(strict_types=1);

namespace ScMedia\Services;

final class ShellTool {
  private string $envPrefix = 'LANG=C.UTF-8 LC_ALL=C.UTF-8 ';

  public function wrap(string $cmd): string {
    return $this->envPrefix . $cmd;
  }

  public function exec(string $cmd): ?string {
    return @shell_exec($this->wrap($cmd));
  }

  public function execTrim(string $cmd): ?string {
    $out = $this->exec($cmd);
    return $out === null ? null : trim($out);
  }

  public function which(string $bin): ?string {
    $out = $this->execTrim('command -v ' . escapeshellarg($bin) . ' 2>/dev/null');
    return $out !== '' ? $out : null;
  }
}
