{ stdenv
, fetchFromGitHub
, ... }:

stdenv.mkDerivation rec {
  pname = "freeswitch-sounds";
  version = "1.0.53";

  src = fetchFromGitHub {
    owner = "freeswitch";
    repo = "freeswitch-sounds";
    rev = "fbe44d17b3cb13fcc8dff190d232162e411ce2db";
    sha256 = "sha256:021qbdvbhc63va32xndqbdanjysijx2b5an3bzw4ya7b2icnfl0s";
  };

  installPhase = ''
    runHook preInstall

    mkdir $out
    for voice in en/ca/june en/us/{allison,callie} es/ar/mario fr/ca/june pt/BR/karina ru/RU/{elena,kirill,vika} sv/se/jakob zh/{cn,hk}/sinmei; do
      mkdir -p $out/$voice
      for category in $voice/*; do
        test -d $category || continue

        for f in $category/*/*; do
          filename=$(basename $f)
          rootname=$(dirname $(dirname $f))
          mkdir -p $out/$rootname
          cp $f $out/$rootname/$filename
        done
      done
    done

    cp -Rv music $out/music

    runHook postInstall
  '';
}