{ lib, pkgs, cfg }:

# Generate one directory per MOH class containing each source file transcoded
# to multiple formats. Asterisk's mode=files picks the best format at runtime
# based on the channel's active codec, avoiding transcoding where possible.
#
# Format variants per file:
#   N.ulaw  — G.711 μ-law  8kHz  (universal narrowband)
#   N.alaw  — G.711 A-law  8kHz  (European narrowband)
#   N.g722  — G.722       16kHz  (wideband, no transcoding for G.722 calls)

lib.mapAttrs (className: cls:
  pkgs.runCommand "moh-${className}" {
    nativeBuildInputs = [ pkgs.ffmpeg ];
  } (lib.concatStringsSep "\n" (
    [ "mkdir -p $out" ]
    ++ lib.concatLists (lib.imap1 (i: src: [
      "ffmpeg -i ${src} -ar 8000  -ac 1 -f mulaw        $out/${toString i}.ulaw"
      "ffmpeg -i ${src} -ar 8000  -ac 1 -f alaw         $out/${toString i}.alaw"
      "ffmpeg -i ${src} -ar 16000 -ac 1 -acodec adpcm_g722 $out/${toString i}.g722"
    ]) cls.files)
  ))
) cfg.mohClasses
