{ config, containerCfg, pkgs, lib, builder, name, ... }:
let 
  serverCfg = config.syscfg.server;

  mkServarrImage = appName: appPkg: binaryPath: pkgs.dockerTools.streamLayeredImage {
    name = appPkg.name;
    tag = appPkg.version;
    contents = with pkgs; [ cacert openssl ];
    config = {
      Cmd = [ "${appPkg}/${binaryPath}" "-nobrowser" "-data=/config" ];
      Env = [ "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=0" "HOME=/tmp" ];
    };
  };

  images = {
    prowlarr = mkServarrImage "prowlarr" pkgs.prowlarr "bin/Prowlarr";
    radarr   = mkServarrImage "radarr"   pkgs.radarr   "bin/Radarr";
    sonarr   = mkServarrImage "sonarr"   pkgs.sonarr   "bin/Sonarr";
    bazarr   = mkServarrImage "bazarr"   pkgs.bazarr   "bin/bazarr";
    lidarr   = mkServarrImage "lidarr"   pkgs.lidarr   "bin/Lidarr";
    readarr  = mkServarrImage "readarr"  pkgs.readarr  "bin/Readarr";
    #dispatcharr
  };

  sharedVolumes = [
    "${serverCfg.path.data.path}/media:/media" # Fast hardlinking requires a single shared root
    "${serverCfg.path.config.path}/servarr:/config-root"
  ];
in 
  assert containerCfg.subpath == null || throw "Error: Servarr does not support subpath.";
{
  requires.secrets = [ name ];
  # db = [ "prowlarr" "sonarr" "radarr" ]; -> one db for each

  runtime = {
  paths = [
    { path = "${serverCfg.path.data.path}/media/";             mode = "0755"; }
    { path = "${serverCfg.path.config.path}/servarr/prowlarr"; mode = "0755"; }
    { path = "${serverCfg.path.config.path}/servarr/radarr";   mode = "0755"; }
    { path = "${serverCfg.path.config.path}/servarr/sonarr";   mode = "0755"; }
    { path = "${serverCfg.path.config.path}/servarr/lidarr";   mode = "0755"; }
  ];

  containers = {
  }// lib.optionalAttrs (builtins.elem "prowlarr" (containerCfg.extra.modules)) {
    prowlarr = builder.mkContainer {
      authentik = true;
      tmpfs = true;
      subdomain = containerCfg.subdomain;
      subpath = "prowlarr";
      imageStream = images.prowlarr;
      port = 8989;
      secret = name;
      extraEnv = {
        PROWLARR__APP__INSTANCENAME = "Prowlarr";
        PROWLARR__AUTH__METHOD = "External";
        PROWLARR__SERVER__PORT = "8989";
        PROWLARR__SERVER__URLBASE = "/prowlarr";
        PROWLARR__LOG__ANALYTICSENABLED = "False";
      };
      extraOptions = [
        "--user=0:0"
        "--passwd-entry=root:x:0:0:root:/root:/bin/sh"
      ];
        overrides.volumes = sharedVolumes ++ [ "${serverCfg.path.config.path}/servarr/prowlarr:/config" ];
    };

  }// lib.optionalAttrs (builtins.elem "radarr" (containerCfg.extra.modules)) {
    radarr = builder.mkContainer {
      authentik = true;
      tmpfs = true;
      subdomain = containerCfg.subdomain;
      subpath = "radarr";
      imageStream = images.radarr;
      port = 8989;
      secret = name;
      extraEnv = {
        RADARR__APP__INSTANCENAME = "Radarr";
        RADARR__AUTH__METHOD = "External";
        RADARR__SERVER__PORT = "8989";
        RADARR__SERVER__URLBASE = "/radarr";
        RADARR__LOG__ANALYTICSENABLED = "False";
      };
      extraOptions = [
        "--user=0:0"
        "--passwd-entry=root:x:0:0:root:/root:/bin/sh"
      ];
        overrides.volumes = sharedVolumes ++ [ "${serverCfg.path.config.path}/servarr/radarr:/config" ];
    };

  }// lib.optionalAttrs (builtins.elem "sonarr" (containerCfg.extra.modules)) {
    sonarr = builder.mkContainer {
      authentik = true;
      tmpfs = true;
      subdomain = containerCfg.subdomain;
      subpath = "sonarr";
      imageStream = images.sonarr;
      port = 8989;
      secret = name;
      extraEnv = {
        SONARR__APP__INSTANCENAME = "Sonarr";
        SONARR__AUTH__METHOD = "External";
        SONARR__SERVER__PORT = "8989";
        SONARR__SERVER__URLBASE = "/sonarr";
        SONARR__LOG__ANALYTICSENABLED = "False";
      };
      extraOptions = [
        "--user=0:0"
        "--passwd-entry=root:x:0:0:root:/root:/bin/sh"
      ];
        overrides.volumes = sharedVolumes ++ [ "${serverCfg.path.config.path}/servarr/sonarr:/config" ];
    };

  }// lib.optionalAttrs (builtins.elem "lidarr" (containerCfg.extra.modules )) {
    lidarr = builder.mkContainer {
      authentik = true;
      tmpfs = true;
      subdomain = containerCfg.subdomain;
      subpath = "lidarr";
      imageStream = images.lidarr;
      port = 8989;
      secret = name;
      extraEnv = {
        LIDARR__APP__INSTANCENAME = "Lidarr";
        LIDARR__AUTH__METHOD = "External";
        LIDARR__SERVER__PORT = "8989";
        LIDARR__SERVER__URLBASE = "/lidarr";
        LIDARR__LOG__ANALYTICSENABLED = "False";
      };
      extraOptions = [
        "--user=0:0"
        "--passwd-entry=root:x:0:0:root:/root:/bin/sh"
      ];
        overrides.volumes = sharedVolumes ++ [ "${serverCfg.path.config.path}/servarr/lidarr:/config" ];
    };

  }// lib.optionalAttrs (builtins.elem "readarr" (containerCfg.extra.modules)) {
    readarr = throw "Not Implemented";
  }// lib.optionalAttrs (builtins.elem "mylarr" (containerCfg.extra.modules)) {
    mylarr = throw "Not Implemented";
  }// lib.optionalAttrs (builtins.elem "bazarr" (containerCfg.extra.modules)) {
    bazarr = throw "Not Implemented";
  }// lib.optionalAttrs (builtins.elem "seerr" (containerCfg.extra.modules)) {
    seerr = throw "Not Implemented";
  }// lib.optionalAttrs (builtins.elem "flaresolverr" (containerCfg.extra.modules)) {
    flaresolverr = builder.mkContainer {
      image = "ghcr.io/flaresolverr/flaresolverr:latest";
      port = 8191;
      extraEnv = {
        CAPTCHA_SOLVER = "none";
      };
    };
  };

  setup = {
    trigger = "prowlarr"; # Triggers atomic environment verification on main controller
    envFile = config.sops.secrets."SERVARR".path;
    script = pkgs.writeShellScript "setup-servarr" ''

      ${lib.optionalString (builtins.elem "prowlarr" (containerCfg.extra.modules)) ''
        echo "Prowlarr Setup - START"
        PROWLARR_URL="https://${containerCfg.subdomain}.${serverCfg.domain}/prowlarr"
        ${lib.optionalString (builtins.elem "flaresolverr" (containerCfg.extra.modules)) ''
          PROWL_TAG=$(${pkgs.curl}/bin/curl -s -X GET "$PROWLARR_URL/api/v1/tag" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json')

          if ! echo "$PROWL_TAG" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .label == "flaresolver")' > /dev/null; then
            PROWL_TAG=$(${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/tag" \
              -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"label":"flaresolver"}' )
          fi

          PROWL_PROXY=$(${pkgs.curl}/bin/curl -s -X GET "$PROWLARR_URL/api/v1/indexerProxy" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json')

          if ! echo "$PROWL_PROXY" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "FlareSolverr")' > /dev/null; then
            echo "Prowlarr Setup - FlareSolverr"
            ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexerProxy" \
              -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"onHealthIssue":false,"supportsOnHealthIssue":false,"includeHealthWarnings":false,"name":"FlareSolverr","fields":[{"name":"host","value":"http://servarr-flaresolverr:8191/"},{"name":"requestTimeout","value":60}],"implementationName":"FlareSolverr","implementation":"FlareSolverr","configContract":"FlareSolverrSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported#flaresolverr","tags":[1]}'
          fi
        ''}
        
        PROWL_APPS=$(${pkgs.curl}/bin/curl -s -X GET "$PROWLARR_URL/api/v1/applications" \
          -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
          -H 'Accept: application/json' -H 'Content-Type: application/json')

        ${lib.optionalString (builtins.elem "sonarr" (containerCfg.extra.modules)) ''
          if ! echo "$PROWL_APPS" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Sonarr")' > /dev/null; then
            echo "Prowlarr Setup - Sonarr"
            ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/applications" \
              -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"syncLevel":"fullSync","enable":true,"fields":[{"name":"prowlarrUrl","value":"http://servarr-prowlarr:8989/prowlarr"},{"name":"baseUrl","value":"http://servarr-sonarr:8989/sonarr"},{"name":"apiKey","value":"'"$SONARR__AUTH__APIKEY"'"},{"name":"syncCategories","value":[5000,5010,5020,5030,5040,5045,5050,5090]},{"name":"animeSyncCategories","value":[5070]},{"name":"syncAnimeStandardFormatSearch","value":true},{"name":"syncRejectBlocklistedTorrentHashesWhileGrabbing","value":false}],"implementationName":"Sonarr","implementation":"Sonarr","configContract":"SonarrSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported#sonarr","tags":[],"name":"Sonarr"}'
          fi
        ''}
        ${lib.optionalString (builtins.elem "lidarr" (containerCfg.extra.modules)) ''
          if ! echo "$PROWL_APPS" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Lidarr")' > /dev/null; then
            echo "Prowlarr Setup - Lidarr"
            ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/applications" \
              -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"syncLevel":"fullSync","enable":true,"fields":[{"name":"prowlarrUrl","value":"http://servarr-prowlarr:8989/prowlarr"},{"name":"baseUrl","value":"http://servarr-lidarr:8989/lidarr"},{"name":"apiKey","value":"'"$LIDARR__AUTH__APIKEY"'"},{"name":"syncCategories","value":[3000,3010,3030,3040,3050,3060]},{"name":"syncRejectBlocklistedTorrentHashesWhileGrabbing","value":false}],"implementationName":"Lidarr","implementation":"Lidarr","configContract":"LidarrSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported#lidarr","tags":[],"name":"Lidarr"}'
          fi
        ''}
        ${lib.optionalString (builtins.elem "radarr" (containerCfg.extra.modules)) ''
          if ! echo "$PROWL_APPS" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Radarr")' > /dev/null; then
            echo "Prowlarr Setup - Radarr"
            ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/applications" \
              -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"syncLevel":"fullSync","enable":true,"fields":[{"name":"prowlarrUrl","value":"http://servarr-prowlarr:8989/prowlarr"},{"name":"baseUrl","value":"http://servarr-radarr:8989/radarr"},{"name":"apiKey","value":"'"$RADARR__AUTH__APIKEY"'"},{"name":"syncCategories","value":[2000,2010,2020,2030,2040,2045,2050,2060,2070,2080,2090]},{"name":"syncRejectBlocklistedTorrentHashesWhileGrabbing","value":false}],"implementationName":"Radarr","implementation":"Radarr","configContract":"RadarrSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported#radarr","tags":[],"name":"Radarr"}'
          fi
        ''}
        ${lib.optionalString (builtins.elem "mylarr" (containerCfg.extra.modules)) ''
          if ! echo "$PROWL_APPS" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Mylar")' > /dev/null; then
            echo "Prowlarr Setup - Mylarr"
          fi
        ''}
        ${lib.optionalString (builtins.elem "readarr" (containerCfg.extra.modules)) ''
          if ! echo "$PROWL_APPS" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Readarr")' > /dev/null; then
            echo "Prowlarr Setup - Readarr"
          fi
        ''}
        ${lib.optionalString (serverCfg.containers?transmission ) ''
          PROWL_DL=$(${pkgs.curl}/bin/curl -s -X GET "$PROWLARR_URL/api/v1/downloadclient" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json')
          
          if ! echo "$PROWL_DL" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Transmission")' > /dev/null; then
            echo "Prowlarr Setup - Transmission"
            ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/downloadclient" \
              -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"enable":true,"protocol":"torrent","priority":1,"categories":[],"supportsCategories":true,"name":"Transmission","fields":[{"name":"host","value":"transmission-server"},{"name":"port","value":9091},{"name":"useSsl","value":false},{"name":"urlBase","value":"/transmission/"},{"name":"username"},{"name":"password"},{"name":"category"},{"name":"directory"},{"name":"priority","value":0},{"name":"addPaused","value":false}],"implementationName":"Transmission","implementation":"Transmission","configContract":"TransmissionSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported#transmission","tags":[]}' 
          fi
        ''}

        echo "Prowlarr Setup - Indexers"
        PROWL_IDX=$(${pkgs.curl}/bin/curl -s -X GET "$PROWLARR_URL/api/v1/indexer" \
          -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
          -H 'Accept: application/json' -H 'Content-Type: application/json')
      
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "1337x")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://1337x.to/","https://1337x.st/","https://x1337x.ws/","https://x1337x.eu/","https://x1337x.cc/"],"legacyUrls":["https://1337x.is/","https://1337x.gd/","https://1337x.mrunblock.bond/","https://1337x.abcproxy.org/","https://1337x.so/","https://1337x.unblockit.download/","https://1337x.unblockninja.com/","https://1337x.ninjaproxy1.com/","https://1337x.proxyninja.org/","https://1337x.proxyninja.net/","https://1337x.torrentbay.st/","https://1337x.torrentsbay.org/","https://x1337x.se/"],"definitionName":"1337x","description":"1337x is a Public torrent site that offers verified torrent downloads","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":5000,"name":"TV","subCategories":[{"id":5070,"name":"TV/Anime","subCategories":[]},{"id":5040,"name":"TV/HD","subCategories":[]},{"id":5030,"name":"TV/SD","subCategories":[]},{"id":5080,"name":"TV/Documentary","subCategories":[]}]},{"id":100028,"name":"Anime/Anime","subCategories":[]},{"id":100078,"name":"Anime/Dual Audio","subCategories":[]},{"id":100079,"name":"Anime/Dubbed","subCategories":[]},{"id":100080,"name":"Anime/Subbed","subCategories":[]},{"id":100081,"name":"Anime/Raw","subCategories":[]},{"id":3000,"name":"Audio","subCategories":[{"id":3010,"name":"Audio/MP3","subCategories":[]},{"id":3040,"name":"Audio/Lossless","subCategories":[]},{"id":3020,"name":"Audio/Video","subCategories":[]},{"id":3050,"name":"Audio/Other","subCategories":[]},{"id":3030,"name":"Audio/Audiobook","subCategories":[]}]},{"id":100022,"name":"Music/MP3","subCategories":[]},{"id":100023,"name":"Music/Lossless","subCategories":[]},{"id":100024,"name":"Music/DVD","subCategories":[]},{"id":100025,"name":"Music/Video","subCategories":[]},{"id":100026,"name":"Music/Radio","subCategories":[]},{"id":100027,"name":"Music/Other","subCategories":[]},{"id":100053,"name":"Music/Album","subCategories":[]},{"id":100058,"name":"Music/Box set","subCategories":[]},{"id":100059,"name":"Music/Discography","subCategories":[]},{"id":100060,"name":"Music/Single","subCategories":[]},{"id":100068,"name":"Music/Concerts","subCategories":[]},{"id":100069,"name":"Music/AAC","subCategories":[]},{"id":2000,"name":"Movies","subCategories":[{"id":2070,"name":"Movies/DVD","subCategories":[]},{"id":2030,"name":"Movies/SD","subCategories":[]},{"id":2010,"name":"Movies/Foreign","subCategories":[]},{"id":2040,"name":"Movies/HD","subCategories":[]},{"id":2060,"name":"Movies/3D","subCategories":[]},{"id":2045,"name":"Movies/UHD","subCategories":[]}]},{"id":100001,"name":"Movies/DVD","subCategories":[]},{"id":100002,"name":"Movies/Divx/Xvid","subCategories":[]},{"id":100003,"name":"Movies/SVCD/VCD","subCategories":[]},{"id":100004,"name":"Movies/Dubs/Dual Audio","subCategories":[]},{"id":100042,"name":"Movies/HD","subCategories":[]},{"id":100054,"name":"Movies/h.264/x264","subCategories":[]},{"id":100055,"name":"Movies/Mp4","subCategories":[]},{"id":100066,"name":"Movies/3D","subCategories":[]},{"id":100070,"name":"Movies/HEVC/x265","subCategories":[]},{"id":100073,"name":"Movies/Bollywood","subCategories":[]},{"id":100076,"name":"Movies/UHD","subCategories":[]},{"id":100005,"name":"TV/DVD","subCategories":[]},{"id":100006,"name":"TV/Divx/Xvid","subCategories":[]},{"id":100007,"name":"TV/SVCD/VCD","subCategories":[]},{"id":100041,"name":"TV/HD","subCategories":[]},{"id":100071,"name":"TV/HEVC/x265","subCategories":[]},{"id":100074,"name":"TV/Cartoons","subCategories":[]},{"id":100075,"name":"TV/SD","subCategories":[]},{"id":100009,"name":"TV/Documentary","subCategories":[]},{"id":4000,"name":"PC","subCategories":[{"id":4030,"name":"PC/Mac","subCategories":[]},{"id":4070,"name":"PC/Mobile-Android","subCategories":[]},{"id":4060,"name":"PC/Mobile-iOS","subCategories":[]},{"id":4050,"name":"PC/Games","subCategories":[]},{"id":4040,"name":"PC/Mobile-Other","subCategories":[]}]},{"id":100018,"name":"Apps/PC Software","subCategories":[]},{"id":100019,"name":"Apps/Mac","subCategories":[]},{"id":100020,"name":"Apps/Linux","subCategories":[]},{"id":100021,"name":"Apps/Other","subCategories":[]},{"id":100056,"name":"Apps/Android","subCategories":[]},{"id":100057,"name":"Apps/iOS","subCategories":[]},{"id":100010,"name":"Games/PC Game","subCategories":[]},{"id":1000,"name":"Console","subCategories":[{"id":1080,"name":"Console/PS3","subCategories":[]},{"id":1020,"name":"Console/PSP","subCategories":[]},{"id":1040,"name":"Console/XBox","subCategories":[]},{"id":1050,"name":"Console/XBox 360","subCategories":[]},{"id":1090,"name":"Console/Other","subCategories":[]},{"id":1030,"name":"Console/Wii","subCategories":[]},{"id":1010,"name":"Console/NDS","subCategories":[]},{"id":1110,"name":"Console/3DS","subCategories":[]},{"id":1180,"name":"Console/PS4","subCategories":[]}]},{"id":100011,"name":"Games/PS2","subCategories":[]},{"id":100012,"name":"Games/PSP","subCategories":[]},{"id":100013,"name":"Games/Xbox","subCategories":[]},{"id":100014,"name":"Games/Xbox360","subCategories":[]},{"id":100015,"name":"Games/PS1","subCategories":[]},{"id":100016,"name":"Games/Dreamcast","subCategories":[]},{"id":100017,"name":"Games/Other","subCategories":[]},{"id":100043,"name":"Games/PS3","subCategories":[]},{"id":100044,"name":"Games/Wii","subCategories":[]},{"id":100045,"name":"Games/DS","subCategories":[]},{"id":100046,"name":"Games/GameCube","subCategories":[]},{"id":100072,"name":"Games/3DS","subCategories":[]},{"id":100077,"name":"Games/PS4","subCategories":[]},{"id":100082,"name":"Games/Switch","subCategories":[]},{"id":6000,"name":"XXX","subCategories":[{"id":6010,"name":"XXX/DVD","subCategories":[]},{"id":6060,"name":"XXX/ImageSet","subCategories":[]}]},{"id":100048,"name":"XXX/Video","subCategories":[]},{"id":100049,"name":"XXX/Picture","subCategories":[]},{"id":100050,"name":"XXX/Magazine","subCategories":[]},{"id":100051,"name":"XXX/Hentai","subCategories":[]},{"id":100067,"name":"XXX/Games","subCategories":[]},{"id":8000,"name":"Other","subCategories":[{"id":8010,"name":"Other/Misc","subCategories":[]}]},{"id":100033,"name":"Other/Emulation","subCategories":[]},{"id":7000,"name":"Books","subCategories":[{"id":7020,"name":"Books/EBook","subCategories":[]},{"id":7030,"name":"Books/Comics","subCategories":[]}]},{"id":100034,"name":"Other/Tutorial","subCategories":[]},{"id":100035,"name":"Other/Sounds","subCategories":[]},{"id":100036,"name":"Other/E-books","subCategories":[]},{"id":100037,"name":"Other/Images","subCategories":[]},{"id":100038,"name":"Other/Mobile Phone","subCategories":[]},{"id":100039,"name":"Other/Comics","subCategories":[]},{"id":100040,"name":"Other/Other","subCategories":[]},{"id":100047,"name":"Other/Nulled Script","subCategories":[]},{"id":100052,"name":"Other/Audiobook","subCategories":[]}],"supportsRawSearch":true,"searchParams":["q","q"],"tvSearchParams":["q","season","ep"],"movieSearchParams":["q"],"musicSearchParams":["q"],"bookSearchParams":["q"]},"priority":25,"downloadClientId":0,"added":"2024-12-07T14:33:52Z","sortName":"1337x","fields":[{"name":"definitionFile","value":"1337x"},{"name":"baseUrl"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":true},{"name":"uploader","value":""},{"name":"info_uploader","value":"You can filter by Uploader by entering a Case Sensitive username, or leave empty to get all results.<br>Note: this is the username of the Uploader and not the Groupname that often show up at the end of 1337x titles, eg -GalaxyRG."},{"name":"info_flaresolverr","value":"This site may use Cloudflare DDoS Protection, therefore Prowlarr requires <a href=\"https://wiki.servarr.com/prowlarr/faq#can-i-use-flaresolverr-indexers\" target=\"_blank\" rel=\"noreferrer\">FlareSolverr</a> to access it."},{"name":"downloadlink","value":1},{"name":"downloadlink2","value":0},{"name":"info_download","value":"As the iTorrents .torrent download link on this site is known to fail from time to time, we suggest using the magnet link as a fallback. The BTCache and Torrage services are not supported because they require additional user interaction (a captcha for BTCache and a download button on Torrage.)"},{"name":"disablesort","value":false},{"name":"sort","value":2},{"name":"type","value":1}],"implementationName":"Cardigann","implementation":"Cardigann","configContract":"CardigannSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#1337x","tags":[1],"name":"1337x"}'
        fi
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "EZTV")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://eztvx.to/","https://eztv.wf/","https://eztv.tf/","https://eztv.yt/","https://eztv1.xyz/"],"legacyUrls":["https://eztv.ag/","https://eztv.it/","https://eztv.ch/","https://eztv.io/","https://eztv.re/","https://eztv.li/","https://eztv.mrunblock.bond/","https://eztv.nocensor.cloud/","https://eztv.unblockninja.com/","https://eztv.ninjaproxy1.com/","https://eztv.proxyninja.org/","https://eztv.abcproxy.org/","https://eztv.unblockit.download/"],"definitionName":"eztv","description":"EZTV is a Public torrent site for TV shows","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":5000,"name":"TV","subCategories":[]}],"supportsRawSearch":false,"searchParams":["q","q"],"tvSearchParams":["q","season","ep"],"movieSearchParams":[],"musicSearchParams":[],"bookSearchParams":[]},"priority":25,"downloadClientId":0,"added":"2026-05-20T21:48:41Z","sortName":"eztv","name":"EZTV","fields":[{"name":"definitionFile","value":"eztv"},{"name":"baseUrl"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":false}],"implementationName":"Cardigann","implementation":"Cardigann","configContract":"CardigannSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#eztv","tags":[1],"name":"EZTV"}'
        fi
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "InternetArchive")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://archive.org/"],"legacyUrls":[],"definitionName":"internetarchive","description":"Internet Archive is a non-profit digital library offering free universal access to books, movies & music, as well as 406 billion archived web pages","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":3000,"name":"Audio","subCategories":[]},{"id":127296,"name":"audio","subCategories":[]},{"id":154563,"name":"etree","subCategories":[]},{"id":2000,"name":"Movies","subCategories":[]},{"id":112696,"name":"movies","subCategories":[]},{"id":5000,"name":"TV","subCategories":[]},{"id":112696,"name":"movies","subCategories":[]},{"id":8000,"name":"Other","subCategories":[{"id":8010,"name":"Other/Misc","subCategories":[]}]},{"id":130222,"name":"image","subCategories":[]},{"id":7000,"name":"Books","subCategories":[]},{"id":130440,"name":"texts","subCategories":[]},{"id":4000,"name":"PC","subCategories":[]},{"id":133225,"name":"software","subCategories":[]},{"id":133994,"name":"web","subCategories":[]},{"id":114112,"name":"collection","subCategories":[]},{"id":108394,"name":"account","subCategories":[]},{"id":131905,"name":"data","subCategories":[]},{"id":138096,"name":"other","subCategories":[]}],"supportsRawSearch":false,"searchParams":["q","q"],"tvSearchParams":["q","season","ep"],"movieSearchParams":["q"],"musicSearchParams":["q"],"bookSearchParams":["q"]},"priority":25,"downloadClientId":0,"added":"2026-05-20T21:35:20Z","sortName":"internet archive","name":"Internet Archive","fields":[{"name":"definitionFile","value":"internetarchive"},{"name":"baseUrl"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":false},{"name":"titleOnly","value":true},{"name":"noMagnet","value":false},{"name":"sort","value":2},{"name":"type","value":1}],"implementationName":"Cardigann","implementation":"Cardigann","configContract":"CardigannSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#internetarchive","tags":[],"name":"InternetArchive"}'
        fi
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Nyaa.si")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://nyaa.si/","https://nyaa.iss.ink/","https://nyaa.land/","https://nyaa.mom/","https://nyaa.unblockninja.com/"],"legacyUrls":["https://nyaa.black-mirror.xyz/","https://nyaa.unblocked.casa/","https://nyaa.proxyportal.fun/","https://nyaa.uk-unblock.xyz/","https://nyaa.ind-unblock.xyz/","https://nyaa.unblocked.bar/","https://nyaa.proxyportal.pw/","https://nyaa.uk-unblock.pro/","https://nyaa.root.yt/","https://nyaa.lol/","https://nyaa.mrunblock.bond/","https://nyaa.nocensor.cloud/"],"definitionName":"nyaasi","description":"Nyaa is a Public torrent site focused on Eastern ASIAN media including anime, manga, literature and music","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":5000,"name":"TV","subCategories":[{"id":5070,"name":"TV/Anime","subCategories":[]}]},{"id":140679,"name":"Anime","subCategories":[]},{"id":125996,"name":"Anime - Anime Music Video","subCategories":[]},{"id":127720,"name":"Anime - English-translated","subCategories":[]},{"id":131088,"name":"Anime - Non-English-translated","subCategories":[]},{"id":134634,"name":"Anime - Raw","subCategories":[]},{"id":2000,"name":"Movies","subCategories":[{"id":2020,"name":"Movies/Other","subCategories":[]}]},{"id":140679,"name":"Anime","subCategories":[]},{"id":125996,"name":"Anime - Anime Music Video","subCategories":[]},{"id":127720,"name":"Anime - English-translated","subCategories":[]},{"id":131088,"name":"Anime - Non-English-translated","subCategories":[]},{"id":134634,"name":"Anime - Raw","subCategories":[]},{"id":3000,"name":"Audio","subCategories":[]},{"id":145414,"name":"Audio","subCategories":[]},{"id":118685,"name":"Audio - Lossless","subCategories":[]},{"id":155285,"name":"Audio - Lossy","subCategories":[]},{"id":7000,"name":"Books","subCategories":[]},{"id":100920,"name":"Literature","subCategories":[]},{"id":156719,"name":"Literature  English-translated","subCategories":[]},{"id":111160,"name":"Literature - Non-English-translated","subCategories":[]},{"id":117084,"name":"Literature - Raw","subCategories":[]},{"id":129933,"name":"Live Action","subCategories":[]},{"id":101340,"name":"Live Action - English-translated","subCategories":[]},{"id":150332,"name":"Live Action - Idol/Promotional Video","subCategories":[]},{"id":150562,"name":"Live Action - Non-English-translated","subCategories":[]},{"id":150452,"name":"Live Action - Raw","subCategories":[]},{"id":8000,"name":"Other","subCategories":[]},{"id":134686,"name":"Pictures","subCategories":[]},{"id":131360,"name":"Pictures - Graphics","subCategories":[]},{"id":109829,"name":"Pictures - Photos","subCategories":[]},{"id":4000,"name":"PC","subCategories":[{"id":4020,"name":"PC/ISO","subCategories":[]},{"id":4050,"name":"PC/Games","subCategories":[]}]},{"id":117318,"name":"Software","subCategories":[]},{"id":130420,"name":"Software - Applications","subCategories":[]},{"id":103955,"name":"Software - Games","subCategories":[]}],"supportsRawSearch":true,"searchParams":["q","q"],"tvSearchParams":["q","season","ep"],"movieSearchParams":["q"],"musicSearchParams":["q"],"bookSearchParams":["q"]},"priority":25,"downloadClientId":0,"added":"2024-12-07T14:32:31Z","sortName":"nyaa si","name":"Nyaa.si","fields":[{"name":"definitionFile","value":"nyaasi"},{"name":"baseUrl"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":true},{"name":"prefer_magnet_links","value":true},{"name":"sonarr_compatibility","value":false},{"name":"strip_s01","value":false},{"name":"radarr_compatibility","value":false},{"name":"filter-id","value":0},{"name":"cat-id","value":0},{"name":"sort","value":0},{"name":"type","value":1}],"implementationName":"Cardigann","implementation":"Cardigann","configContract":"CardigannSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#nyaasi","tags":[],"name":"Nyaa.si"}'
        fi
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "ShanaProject")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://www.shanaproject.com/"],"legacyUrls":[],"definitionName":"shanaproject","description":"Shana Project is a Public torrent site for ANIME","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":5000,"name":"TV","subCategories":[{"id":5070,"name":"TV/Anime","subCategories":[]}]},{"id":146065,"name":"Anime","subCategories":[]}],"supportsRawSearch":false,"searchParams":["q","q"],"tvSearchParams":["q","season","ep"],"movieSearchParams":[],"musicSearchParams":[],"bookSearchParams":[]},"priority":25,"downloadClientId":0,"added":"2024-12-07T14:33:25Z","sortName":"shana project","name":"Shana Project","fields":[{"name":"definitionFile","value":"shanaproject"},{"name":"baseUrl"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":true},{"name":"sort","value":0},{"name":"type","value":1},{"name":"info","value":"This site only supports searching for Titles. Do not add a season or episode number, or a quality to your query."}],"implementationName":"Cardigann","implementation":"Cardigann","configContract":"CardigannSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#shanaproject","tags":[],"name":"ShanaProject"}'
        fi
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "SubsPlease")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://subsplease.org/","https://subsplease.mrunblock.bond/","https://subsplease.nocensor.click/"],"legacyUrls":["https://subsplease.nocensor.space/"],"definitionName":"SubsPlease","description":"SubsPlease - A better HorribleSubs/Erai replacement","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":5000,"name":"TV","subCategories":[{"id":5070,"name":"TV/Anime","subCategories":[]}]},{"id":2000,"name":"Movies","subCategories":[{"id":2020,"name":"Movies/Other","subCategories":[]}]}],"supportsRawSearch":false,"searchParams":["q"],"tvSearchParams":["q","season","ep"],"movieSearchParams":["q"],"musicSearchParams":[],"bookSearchParams":[]},"priority":25,"downloadClientId":0,"added":"2024-12-07T14:33:35Z","sortName":"subsplease","name":"SubsPlease","fields":[{"name":"baseUrl","value":"https://subsplease.org/"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":true}],"implementationName":"SubsPlease","implementation":"SubsPlease","configContract":"NoAuthTorrentBaseSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#subsplease","tags":[],"name":"SubsPlease"}'
        fi
        if ! echo "$PROWL_IDX" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "YTS")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$PROWLARR_URL/api/v1/indexer" \
            -H "X-Api-Key: $PROWLARR__AUTH__APIKEY" -H 'X-Prowlarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"indexerUrls":["https://yts.bz/","https://yts.ninjaproxy1.com/","https://yts.proxyninja.org/","https://yts.proxyninja.net/","https://yts.torrentbay.st/","https://yts.torrentsbay.org/"],"legacyUrls":["https://yts.am/","https://yts.ag/","https://yts.gg/","https://yts.mx/","https://yts.mrunblock.bond/","https://yts.nocensor.cloud/","https://yts.unblockit.download/","https://yts.lt/","https://yts.unblockninja.com/"],"definitionName":"yts","description":"YTS is a Public torrent site specialising in HD movies of small size","language":"en-US","encoding":"Unicode (UTF-8)","enable":true,"redirect":false,"supportsRss":true,"supportsSearch":true,"supportsRedirect":false,"supportsPagination":false,"appProfileId":1,"protocol":"torrent","privacy":"public","capabilities":{"limitsMax":100,"limitsDefault":100,"categories":[{"id":2000,"name":"Movies","subCategories":[{"id":2040,"name":"Movies/HD","subCategories":[]},{"id":2045,"name":"Movies/UHD","subCategories":[]},{"id":2060,"name":"Movies/3D","subCategories":[]}]},{"id":100045,"name":"Movies/x264/720p","subCategories":[]},{"id":100044,"name":"Movies/x264/1080p","subCategories":[]},{"id":100046,"name":"Movies/x264/2160p","subCategories":[]},{"id":100047,"name":"Movies/x264/3D","subCategories":[]}],"supportsRawSearch":false,"searchParams":["q","q"],"tvSearchParams":[],"movieSearchParams":["q","imdbId"],"musicSearchParams":[],"bookSearchParams":[]},"priority":25,"downloadClientId":0,"added":"2024-12-07T14:32:55Z","sortName":"yts","name":"YTS","fields":[{"name":"definitionFile","value":"yts"},{"name":"baseUrl"},{"name":"baseSettings.queryLimit"},{"name":"baseSettings.grabLimit"},{"name":"baseSettings.limitsUnit","value":0},{"name":"torrentBaseSettings.appMinimumSeeders"},{"name":"torrentBaseSettings.seedRatio"},{"name":"torrentBaseSettings.seedTime"},{"name":"torrentBaseSettings.packSeedTime"},{"name":"torrentBaseSettings.preferMagnetUrl","value":true},{"name":"apiurl","value":"movies-api.accel.li"}],"implementationName":"Cardigann","implementation":"Cardigann","configContract":"CardigannSettings","infoLink":"https://wiki.servarr.com/prowlarr/supported-indexers#yts","tags":[],"name":"YTS"}'
        fi


      ''}

      ${lib.optionalString (builtins.elem "sonarr" (containerCfg.extra.modules)) ''
        echo "Sonarr Setup - START"
        SONARR_URL="https://${containerCfg.subdomain}.${serverCfg.domain}/sonarr"
        
        echo "Sonarr Setup - CustomFormat"
        SONARR_CF=$(${pkgs.curl}/bin/curl -s -X GET "$SONARR_URL/api/v3/customformat" \
          -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
          -H 'Accept: application/json' -H 'Content-Type: application/json')

        if ! echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "ORIG_1080")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$SONARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"ORIG_1080","includeCustomFormatWhenRenaming":false,"specifications":[
              {"id":1,"name":"Lang_ORIG","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Language","value":-2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},
              {"id":2,"name":"1080p","implementation":"ResolutionSpecification","implementationName":"Resolution","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Resolution","value":1080,"type":"select","advanced":false,"selectOptions":[{"value":0,"name":"Unknown","order":0},{"value":360,"name":"R360p","order":360},{"value":480,"name":"R480p","order":480},{"value":540,"name":"R540p","order":540},{"value":576,"name":"R576p","order":576},{"value":720,"name":"R720p","order":720},{"value":1080,"name":"R1080p","order":1080},{"value":2160,"name":"R2160p","order":2160}],"privacy":"normal","isFloat":false}]}]}'
        fi
        if ! echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "ORIG_2160")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$SONARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"ORIG_2160","includeCustomFormatWhenRenaming":false,"specifications":[
              {"id":1,"name":"Lang_ORIG","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Language","value":-2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},
              {"id":2,"name":"2160p","implementation":"ResolutionSpecification","implementationName":"Resolution","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Resolution","value":2160,"type":"select","advanced":false,"selectOptions":[{"value":0,"name":"Unknown","order":0},{"value":360,"name":"R360p","order":360},{"value":480,"name":"R480p","order":480},{"value":540,"name":"R540p","order":540},{"value":576,"name":"R576p","order":576},{"value":720,"name":"R720p","order":720},{"value":1080,"name":"R1080p","order":1080},{"value":2160,"name":"R2160p","order":2160}],"privacy":"normal","isFloat":false}]}]}'
        fi
        if ! echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "SUBS")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$SONARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"SUBS","includeCustomFormatWhenRenaming":false,"specifications":[{"id":1,"name":"SUBS","implementation":"ReleaseTitleSpecification","implementationName":"Release Title","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Regular Expression","helpText":"Custom Format RegEx is Case Insensitive","value":"[sS][uU][bB]","type":"textbox","advanced":false,"privacy":"normal","isFloat":false}]}]}'
        fi
        if ! echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "LANG_OTHER")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$SONARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"LANG_OTHER","includeCustomFormatWhenRenaming":false,"specifications":[{"id":1,"name":"FR","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":2,"name":"EN","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":1,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":3,"name":"DE","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":4,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":4,"name":"JA","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":8,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":5,"name":"RU","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/sonarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":11,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]}]}'
        fi
        SONARR_CF=$(${pkgs.curl}/bin/curl -s -X GET "$SONARR_URL/api/v3/customformat" \
          -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
          -H 'Accept: application/json' -H 'Content-Type: application/json')

        FORMAT_ID_1080=$(echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "ORIG_1080") | .id')
        FORMAT_ID_2160=$(echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "ORIG_2160") | .id')
        FORMAT_ID_SUBS=$(echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "SUBS") | .id')
        FORMAT_ID_LANG=$(echo "$SONARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "LANG_OTHER") | .id')

        echo "Sonarr Setup - QualityProfile"
        ${pkgs.curl}/bin/curl -s -X POST "$SONARR_URL/api/v3/config/naming" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"renameEpisodes":true,"replaceIllegalCharacters":true,"colonReplacementFormat":4,"customColonReplacementFormat":"","multiEpisodeStyle":5,"standardEpisodeFormat":"{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}","dailyEpisodeFormat":"{Series Title} - {Air-Date} - {Episode Title} {Quality Full}","animeEpisodeFormat":"{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}","seriesFolderFormat":"{Series Title}","seasonFolderFormat":"Season {season}","specialsFolderFormat":"Specials","id":1}'

        SONARR_QP=$(${pkgs.curl}/bin/curl -s -X GET "$SONARR_URL/api/v3/qualityprofile" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' )

        SONARR_QP_ANY=$(echo $SONARR_QP | ${pkgs.jq}/bin/jq '.[] | select(.name == "Any") | .id')
        if [ -n "$SONARR_QP_ANY" ]; then
          ${pkgs.curl}/bin/curl -s -X PUT "$SONARR_URL/api/v3/qualityprofile/$SONARR_QP_ANY" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"Any","upgradeAllowed":false,"cutoff":0,"items":[{"quality":{"id":0,"name":"Unknown","source":"unknown","resolution":0},"items":[],"allowed":true},{"quality":{"id":1,"name":"SDTV","source":"television","resolution":480},"items":[],"allowed":false},{"name":"WEB 480p","items":[{"quality":{"id":12,"name":"WEBRip-480p","source":"webRip","resolution":480},"items":[],"allowed":false},{"quality":{"id":8,"name":"WEBDL-480p","source":"web","resolution":480},"items":[],"allowed":false}],"allowed":false,"id":1000},{"quality":{"id":2,"name":"DVD","source":"dvd","resolution":480},"items":[],"allowed":false},{"quality":{"id":13,"name":"Bluray-480p","source":"bluray","resolution":480},"items":[],"allowed":false},{"quality":{"id":22,"name":"Bluray-576p","source":"bluray","resolution":576},"items":[],"allowed":false},{"quality":{"id":4,"name":"HDTV-720p","source":"television","resolution":720},"items":[],"allowed":false},{"quality":{"id":9,"name":"HDTV-1080p","source":"television","resolution":1080},"items":[],"allowed":false},{"quality":{"id":10,"name":"Raw-HD","source":"televisionRaw","resolution":1080},"items":[],"allowed":false},{"name":"WEB 720p","items":[{"quality":{"id":14,"name":"WEBRip-720p","source":"webRip","resolution":720},"items":[],"allowed":false},{"quality":{"id":5,"name":"WEBDL-720p","source":"web","resolution":720},"items":[],"allowed":false}],"allowed":false,"id":1001},{"quality":{"id":6,"name":"Bluray-720p","source":"bluray","resolution":720},"items":[],"allowed":false},{"name":"WEB 1080p","items":[{"quality":{"id":15,"name":"WEBRip-1080p","source":"webRip","resolution":1080},"items":[],"allowed":false},{"quality":{"id":3,"name":"WEBDL-1080p","source":"web","resolution":1080},"items":[],"allowed":false}],"allowed":false,"id":1002},{"quality":{"id":7,"name":"Bluray-1080p","source":"bluray","resolution":1080},"items":[],"allowed":false},{"quality":{"id":20,"name":"Bluray-1080p Remux","source":"blurayRaw","resolution":1080},"items":[],"allowed":false},{"quality":{"id":16,"name":"HDTV-2160p","source":"television","resolution":2160},"items":[],"allowed":false},{"name":"WEB 2160p","items":[{"quality":{"id":17,"name":"WEBRip-2160p","source":"webRip","resolution":2160},"items":[],"allowed":false},{"quality":{"id":18,"name":"WEBDL-2160p","source":"web","resolution":2160},"items":[],"allowed":false}],"allowed":false,"id":1003},{"quality":{"id":19,"name":"Bluray-2160p","source":"bluray","resolution":2160},"items":[],"allowed":false},{"quality":{"id":21,"name":"Bluray-2160p Remux","source":"blurayRaw","resolution":2160},"items":[],"allowed":false}],"minFormatScore":0,"cutoffFormatScore":0,"minUpgradeFormatScore":1,"formatItems":[{"format":4,"name":"ORIG_2160","score":0},{"format":3,"name":"SUBS","score":0},{"format":2,"name":"LANG_OTHER","score":0},{"format":1,"name":"ORIG_1080","score":0}],"id":'"$SONARR_QP_ANY"'}'
        fi
        SONARR_QP_RM="SD|HD-720p|HD - 720p/1080p"
        echo "$SONARR_QP_RM" | tr '|' '\n' | while read -r name; do
          # Skip empty lines if any
          [ -z "$name" ] && continue
          QP_ID=$(echo "$SONARR_QP" | ${pkgs.jq}/bin/jq -r --arg n "$name" '.[] | select(.name == $n) | .id')
  
          if [ -n "$QP_ID" ]; then
            ${pkgs.curl}/bin/curl -s -X DELETE "$SONARR_URL/api/v3/qualityprofile/$QP_ID?" \
              -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json'
          fi
        done
        SONARR_QP_ID=$(echo $SONARR_QP | ${pkgs.jq}/bin/jq '.[] | select(.name == "HD-1080p") | .id')
        if [ -n "$SONARR_QP_ID" ]; then
          ${pkgs.curl}/bin/curl -s -X PUT "$SONARR_URL/api/v3/qualityprofile/$SONARR_QP_ID" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"HD-1080p","upgradeAllowed":false,"cutoff":9,"items":[{"quality":{"id":0,"name":"Unknown","source":"unknown","resolution":0},"items":[],"allowed":false},{"quality":{"id":1,"name":"SDTV","source":"television","resolution":480},"items":[],"allowed":false},{"name":"WEB 480p","items":[{"quality":{"id":12,"name":"WEBRip-480p","source":"webRip","resolution":480},"items":[],"allowed":false},{"quality":{"id":8,"name":"WEBDL-480p","source":"web","resolution":480},"items":[],"allowed":false}],"allowed":false,"id":1000},{"quality":{"id":2,"name":"DVD","source":"dvd","resolution":480},"items":[],"allowed":false},{"quality":{"id":13,"name":"Bluray-480p","source":"bluray","resolution":480},"items":[],"allowed":false},{"quality":{"id":22,"name":"Bluray-576p","source":"bluray","resolution":576},"items":[],"allowed":false},{"quality":{"id":4,"name":"HDTV-720p","source":"television","resolution":720},"items":[],"allowed":false},{"quality":{"id":9,"name":"HDTV-1080p","source":"television","resolution":1080},"items":[],"allowed":true},{"quality":{"id":10,"name":"Raw-HD","source":"televisionRaw","resolution":1080},"items":[],"allowed":false},{"name":"WEB 720p","items":[{"quality":{"id":14,"name":"WEBRip-720p","source":"webRip","resolution":720},"items":[],"allowed":false},{"quality":{"id":5,"name":"WEBDL-720p","source":"web","resolution":720},"items":[],"allowed":false}],"allowed":false,"id":1001},{"quality":{"id":6,"name":"Bluray-720p","source":"bluray","resolution":720},"items":[],"allowed":false},{"name":"WEB 1080p","items":[{"quality":{"id":15,"name":"WEBRip-1080p","source":"webRip","resolution":1080},"items":[],"allowed":true},{"quality":{"id":3,"name":"WEBDL-1080p","source":"web","resolution":1080},"items":[],"allowed":true}],"allowed":true,"id":1002},{"quality":{"id":7,"name":"Bluray-1080p","source":"bluray","resolution":1080},"items":[],"allowed":true},{"quality":{"id":20,"name":"Bluray-1080p Remux","source":"blurayRaw","resolution":1080},"items":[],"allowed":false},{"quality":{"id":16,"name":"HDTV-2160p","source":"television","resolution":2160},"items":[],"allowed":false},{"name":"WEB 2160p","items":[{"quality":{"id":17,"name":"WEBRip-2160p","source":"webRip","resolution":2160},"items":[],"allowed":false},{"quality":{"id":18,"name":"WEBDL-2160p","source":"web","resolution":2160},"items":[],"allowed":false}],"allowed":false,"id":1003},{"quality":{"id":19,"name":"Bluray-2160p","source":"bluray","resolution":2160},"items":[],"allowed":false},{"quality":{"id":21,"name":"Bluray-2160p Remux","source":"blurayRaw","resolution":2160},"items":[],"allowed":false}],"minFormatScore":50,"cutoffFormatScore":0,"minUpgradeFormatScore":1,"formatItems":[{"format":'$FORMAT_ID_2160',"name":"ORIG_2160","score":0},{"format":'$FORMAT_ID_SUBS',"name":"SUBS","score":15},{"format":'$FORMAT_ID_LANG',"name":"LANG_OTHER","score":10},{"format":'$FORMAT_ID_1080',"name":"ORIG_1080","score":50}],"id":'"$SONARR_QP_ID"'}'
        fi
        SONARR_QP_ID=$(echo $SONARR_QP | ${pkgs.jq}/bin/jq '.[] | select(.name == "Ultra-HD") | .id')
        if [ -n "$SONARR_QP_ID" ]; then
          ${pkgs.curl}/bin/curl -s -X PUT "$SONARR_URL/api/v3/qualityprofile/$SONARR_QP_ID" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"Ultra-HD","upgradeAllowed":false,"cutoff":16,"items":[{"quality":{"id":0,"name":"Unknown","source":"unknown","resolution":0},"items":[],"allowed":false},{"quality":{"id":1,"name":"SDTV","source":"television","resolution":480},"items":[],"allowed":false},{"name":"WEB 480p","items":[{"quality":{"id":12,"name":"WEBRip-480p","source":"webRip","resolution":480},"items":[],"allowed":false},{"quality":{"id":8,"name":"WEBDL-480p","source":"web","resolution":480},"items":[],"allowed":false}],"allowed":false,"id":1000},{"quality":{"id":2,"name":"DVD","source":"dvd","resolution":480},"items":[],"allowed":false},{"quality":{"id":13,"name":"Bluray-480p","source":"bluray","resolution":480},"items":[],"allowed":false},{"quality":{"id":22,"name":"Bluray-576p","source":"bluray","resolution":576},"items":[],"allowed":false},{"quality":{"id":4,"name":"HDTV-720p","source":"television","resolution":720},"items":[],"allowed":false},{"quality":{"id":9,"name":"HDTV-1080p","source":"television","resolution":1080},"items":[],"allowed":false},{"quality":{"id":10,"name":"Raw-HD","source":"televisionRaw","resolution":1080},"items":[],"allowed":false},{"name":"WEB 720p","items":[{"quality":{"id":14,"name":"WEBRip-720p","source":"webRip","resolution":720},"items":[],"allowed":false},{"quality":{"id":5,"name":"WEBDL-720p","source":"web","resolution":720},"items":[],"allowed":false}],"allowed":false,"id":1001},{"quality":{"id":6,"name":"Bluray-720p","source":"bluray","resolution":720},"items":[],"allowed":false},{"name":"WEB 1080p","items":[{"quality":{"id":15,"name":"WEBRip-1080p","source":"webRip","resolution":1080},"items":[],"allowed":false},{"quality":{"id":3,"name":"WEBDL-1080p","source":"web","resolution":1080},"items":[],"allowed":false}],"allowed":false,"id":1002},{"quality":{"id":7,"name":"Bluray-1080p","source":"bluray","resolution":1080},"items":[],"allowed":false},{"quality":{"id":20,"name":"Bluray-1080p Remux","source":"blurayRaw","resolution":1080},"items":[],"allowed":false},{"quality":{"id":16,"name":"HDTV-2160p","source":"television","resolution":2160},"items":[],"allowed":true},{"name":"WEB 2160p","items":[{"quality":{"id":17,"name":"WEBRip-2160p","source":"webRip","resolution":2160},"items":[],"allowed":true},{"quality":{"id":18,"name":"WEBDL-2160p","source":"web","resolution":2160},"items":[],"allowed":true}],"allowed":true,"id":1003},{"quality":{"id":19,"name":"Bluray-2160p","source":"bluray","resolution":2160},"items":[],"allowed":true},{"quality":{"id":21,"name":"Bluray-2160p Remux","source":"blurayRaw","resolution":2160},"items":[],"allowed":false}],"minFormatScore":50,"cutoffFormatScore":0,"minUpgradeFormatScore":1,"formatItems":[{"format":'$FORMAT_ID_2160',"name":"ORIG_2160","score":50},{"format":'$FORMAT_ID_SUBS',"name":"SUBS","score":0},{"format":'$FORMAT_ID_LANG',"name":"LANG_OTHER","score":10},{"format":'$FORMAT_ID_1080',"name":"ORIG_1080","score":0}],"id":'"$SONARR_QP_ID"'}'
        fi

        ${lib.optionalString (serverCfg.containers?transmission ) ''
          SONARR_DL=$(${pkgs.curl}/bin/curl -s -X GET "$SONARR_URL/api/v3/downloadclient" \
            -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json')
          
          if ! echo "$SONARR_DL" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Transmission")' > /dev/null; then
            echo "Sonarr Setup - Transmission"
            ${pkgs.curl}/bin/curl -s -X POST "$SONARR_URL/api/v3/downloadclient" \
              -H "X-Api-Key: $SONARR__AUTH__APIKEY" -H 'X-Sonarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"enable":true,"protocol":"torrent","priority":1,"categories":[],"supportsCategories":true,"name":"Transmission","fields":[{"name":"host","value":"transmission-server"},{"name":"port","value":9091},{"name":"useSsl","value":false},{"name":"urlBase","value":"/transmission/"},{"name":"username"},{"name":"password"},{"name":"category"},{"name":"directory"},{"name":"priority","value":0},{"name":"addPaused","value":false}],"implementationName":"Transmission","implementation":"Transmission","configContract":"TransmissionSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#transmission","tags":[]}' 
          fi
        ''}


      ''}

      ${lib.optionalString (builtins.elem "radarr" (containerCfg.extra.modules)) ''
        echo "Radarr Setup - START"
        RADARR_URL="https://${containerCfg.subdomain}.${serverCfg.domain}/radarr"
        
        echo "Radarr Setup - CustomFormat"
        RADARR_CF=$(${pkgs.curl}/bin/curl -s -X GET "$RADARR_URL/api/v3/customformat" \
          -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
          -H 'Accept: application/json' -H 'Content-Type: application/json')

        if ! echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "ORIG_1080")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$RADARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"ORIG_1080","includeCustomFormatWhenRenaming":false,"specifications":[
              {"id":1,"name":"Lang_ORIG","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Language","value":-2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},
              {"id":2,"name":"1080p","implementation":"ResolutionSpecification","implementationName":"Resolution","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Resolution","value":1080,"type":"select","advanced":false,"selectOptions":[{"value":0,"name":"Unknown","order":0},{"value":360,"name":"R360p","order":360},{"value":480,"name":"R480p","order":480},{"value":540,"name":"R540p","order":540},{"value":576,"name":"R576p","order":576},{"value":720,"name":"R720p","order":720},{"value":1080,"name":"R1080p","order":1080},{"value":2160,"name":"R2160p","order":2160}],"privacy":"normal","isFloat":false}]}]}'
        fi
        if ! echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "ORIG_2160")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$RADARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"ORIG_2160","includeCustomFormatWhenRenaming":false,"specifications":[
              {"id":1,"name":"Lang_ORIG","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Language","value":-2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},
              {"id":2,"name":"2160p","implementation":"ResolutionSpecification","implementationName":"Resolution","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Resolution","value":2160,"type":"select","advanced":false,"selectOptions":[{"value":0,"name":"Unknown","order":0},{"value":360,"name":"R360p","order":360},{"value":480,"name":"R480p","order":480},{"value":540,"name":"R540p","order":540},{"value":576,"name":"R576p","order":576},{"value":720,"name":"R720p","order":720},{"value":1080,"name":"R1080p","order":1080},{"value":2160,"name":"R2160p","order":2160}],"privacy":"normal","isFloat":false}]}]}'
        fi
        if ! echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "SUBS")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$RADARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"SUBS","includeCustomFormatWhenRenaming":false,"specifications":[{"id":1,"name":"SUBS","implementation":"ReleaseTitleSpecification","implementationName":"Release Title","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":true,"fields":[{"order":0,"name":"value","label":"Regular Expression","helpText":"Custom Format RegEx is Case Insensitive","value":"[sS][uU][bB]","type":"textbox","advanced":false,"privacy":"normal","isFloat":false}]}]}'
        fi
        if ! echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "LANG_OTHER")' > /dev/null; then
          ${pkgs.curl}/bin/curl -s -X POST "$RADARR_URL/api/v3/customformat" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"LANG_OTHER","includeCustomFormatWhenRenaming":false,"specifications":[{"id":1,"name":"FR","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":2,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":2,"name":"EN","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":1,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":3,"name":"DE","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":4,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":4,"name":"JA","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":8,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]},{"id":5,"name":"RU","implementation":"LanguageSpecification","implementationName":"Language","infoLink":"https://wiki.servarr.com/radarr/settings#custom-formats-2","negate":false,"required":false,"fields":[{"order":0,"name":"value","label":"Language","value":11,"type":"select","advanced":false,"selectOptions":[{"value":-2,"name":"Original","order":0},{"value":0,"name":"Unknown","order":0},{"value":26,"name":"Arabic","order":0},{"value":41,"name":"Bosnian","order":0},{"value":28,"name":"Bulgarian","order":0},{"value":38,"name":"Catalan","order":0},{"value":10,"name":"Chinese","order":0},{"value":39,"name":"Croatian","order":0},{"value":25,"name":"Czech","order":0},{"value":6,"name":"Danish","order":0},{"value":7,"name":"Dutch","order":0},{"value":1,"name":"English","order":0},{"value":42,"name":"Estonian","order":0},{"value":16,"name":"Finnish","order":0},{"value":19,"name":"Flemish","order":0},{"value":2,"name":"French","order":0},{"value":4,"name":"German","order":0},{"value":20,"name":"Greek","order":0},{"value":23,"name":"Hebrew","order":0},{"value":27,"name":"Hindi","order":0},{"value":22,"name":"Hungarian","order":0},{"value":9,"name":"Icelandic","order":0},{"value":44,"name":"Indonesian","order":0},{"value":5,"name":"Italian","order":0},{"value":8,"name":"Japanese","order":0},{"value":21,"name":"Korean","order":0},{"value":36,"name":"Latvian","order":0},{"value":24,"name":"Lithuanian","order":0},{"value":45,"name":"Macedonian","order":0},{"value":29,"name":"Malayalam","order":0},{"value":15,"name":"Norwegian","order":0},{"value":37,"name":"Persian","order":0},{"value":12,"name":"Polish","order":0},{"value":18,"name":"Portuguese","order":0},{"value":33,"name":"Portuguese (Brazil)","order":0},{"value":35,"name":"Romanian","order":0},{"value":11,"name":"Russian","order":0},{"value":40,"name":"Serbian","order":0},{"value":31,"name":"Slovak","order":0},{"value":46,"name":"Slovenian","order":0},{"value":3,"name":"Spanish","order":0},{"value":34,"name":"Spanish (Latino)","order":0},{"value":14,"name":"Swedish","order":0},{"value":43,"name":"Tamil","order":0},{"value":32,"name":"Thai","order":0},{"value":17,"name":"Turkish","order":0},{"value":30,"name":"Ukrainian","order":0},{"value":13,"name":"Vietnamese","order":0}],"privacy":"normal","isFloat":false},{"order":1,"name":"exceptLanguage","label":"Except Language","helpText":"Matches if any language other than the selected language is present","value":false,"type":"checkbox","advanced":false,"privacy":"normal","isFloat":false}]}]}'
        fi
        RADARR_CF=$(${pkgs.curl}/bin/curl -s -X GET "$RADARR_URL/api/v3/customformat" \
          -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
          -H 'Accept: application/json' -H 'Content-Type: application/json')

        FORMAT_ID_1080=$(echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "ORIG_1080") | .id')
        FORMAT_ID_2160=$(echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "ORIG_2160") | .id')
        FORMAT_ID_SUBS=$(echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "SUBS") | .id')
        FORMAT_ID_LANG=$(echo "$RADARR_CF" | ${pkgs.jq}/bin/jq -r '.[] | select(.name == "LANG_OTHER") | .id')

        echo "Radarr Setup - QualityProfile"
        ${pkgs.curl}/bin/curl -s -X POST "$RADARR_URL/api/v3/config/naming" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"renameMovies":true,"replaceIllegalCharacters":true,"colonReplacementFormat":"smart","standardMovieFormat":"{Movie Title} ({Release Year}) {Quality Full}","movieFolderFormat":"{Movie Title} ({Release Year})","id":1}'

        RADARR_QP=$(${pkgs.curl}/bin/curl -s -X GET "$RADARR_URL/api/v3/qualityprofile" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' )

        RADARR_QP_ANY=$(echo $RADARR_QP | ${pkgs.jq}/bin/jq '.[] | select(.name == "Any") | .id')
        if [ -n "$RADARR_QP_ANY" ]; then
          ${pkgs.curl}/bin/curl -s -X PUT "$RADARR_URL/api/v3/qualityprofile/$RADARR_QP_ANY" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"Any","upgradeAllowed":false,"cutoff":0,"items":[{"quality":{"id":0,"name":"Unknown","source":"unknown","resolution":0},"items":[],"allowed":true},{"quality":{"id":1,"name":"SDTV","source":"television","resolution":480},"items":[],"allowed":false},{"name":"WEB 480p","items":[{"quality":{"id":12,"name":"WEBRip-480p","source":"webRip","resolution":480},"items":[],"allowed":false},{"quality":{"id":8,"name":"WEBDL-480p","source":"web","resolution":480},"items":[],"allowed":false}],"allowed":false,"id":1000},{"quality":{"id":2,"name":"DVD","source":"dvd","resolution":480},"items":[],"allowed":false},{"quality":{"id":13,"name":"Bluray-480p","source":"bluray","resolution":480},"items":[],"allowed":false},{"quality":{"id":22,"name":"Bluray-576p","source":"bluray","resolution":576},"items":[],"allowed":false},{"quality":{"id":4,"name":"HDTV-720p","source":"television","resolution":720},"items":[],"allowed":false},{"quality":{"id":9,"name":"HDTV-1080p","source":"television","resolution":1080},"items":[],"allowed":false},{"quality":{"id":10,"name":"Raw-HD","source":"televisionRaw","resolution":1080},"items":[],"allowed":false},{"name":"WEB 720p","items":[{"quality":{"id":14,"name":"WEBRip-720p","source":"webRip","resolution":720},"items":[],"allowed":false},{"quality":{"id":5,"name":"WEBDL-720p","source":"web","resolution":720},"items":[],"allowed":false}],"allowed":false,"id":1001},{"quality":{"id":6,"name":"Bluray-720p","source":"bluray","resolution":720},"items":[],"allowed":false},{"name":"WEB 1080p","items":[{"quality":{"id":15,"name":"WEBRip-1080p","source":"webRip","resolution":1080},"items":[],"allowed":false},{"quality":{"id":3,"name":"WEBDL-1080p","source":"web","resolution":1080},"items":[],"allowed":false}],"allowed":false,"id":1002},{"quality":{"id":7,"name":"Bluray-1080p","source":"bluray","resolution":1080},"items":[],"allowed":false},{"quality":{"id":20,"name":"Bluray-1080p Remux","source":"blurayRaw","resolution":1080},"items":[],"allowed":false},{"quality":{"id":16,"name":"HDTV-2160p","source":"television","resolution":2160},"items":[],"allowed":false},{"name":"WEB 2160p","items":[{"quality":{"id":17,"name":"WEBRip-2160p","source":"webRip","resolution":2160},"items":[],"allowed":false},{"quality":{"id":18,"name":"WEBDL-2160p","source":"web","resolution":2160},"items":[],"allowed":false}],"allowed":false,"id":1003},{"quality":{"id":19,"name":"Bluray-2160p","source":"bluray","resolution":2160},"items":[],"allowed":false},{"quality":{"id":21,"name":"Bluray-2160p Remux","source":"blurayRaw","resolution":2160},"items":[],"allowed":false}],"minFormatScore":0,"cutoffFormatScore":0,"minUpgradeFormatScore":1,"formatItems":[{"format":4,"name":"ORIG_2160","score":0},{"format":3,"name":"SUBS","score":0},{"format":2,"name":"LANG_OTHER","score":0},{"format":1,"name":"ORIG_1080","score":0}],"id":'"$RADARR_QP_ANY"'}'
        fi
        RADARR_QP_RM="SD|HD-720p|HD - 720p/1080p"
        echo "$RADARR_QP_RM" | tr '|' '\n' | while read -r name; do
          # Skip empty lines if any
          [ -z "$name" ] && continue
          QP_ID=$(echo "$RADARR_QP" | ${pkgs.jq}/bin/jq -r --arg n "$name" '.[] | select(.name == $n) | .id')
  
          if [ -n "$QP_ID" ]; then
            ${pkgs.curl}/bin/curl -s -X DELETE "$RADARR_URL/api/v3/qualityprofile/$QP_ID?" \
              -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json'
          fi
        done
        RADARR_QP_ID=$(echo $RADARR_QP | ${pkgs.jq}/bin/jq '.[] | select(.name == "HD-1080p") | .id')
        if [ -n "$RADARR_QP_ID" ]; then
          ${pkgs.curl}/bin/curl -s -X PUT "$RADARR_URL/api/v3/qualityprofile/$RADARR_QP_ID" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"HD-1080p","upgradeAllowed":false,"cutoff":9,"items":[{"quality":{"id":0,"name":"Unknown","source":"unknown","resolution":0},"items":[],"allowed":false},{"quality":{"id":1,"name":"SDTV","source":"television","resolution":480},"items":[],"allowed":false},{"name":"WEB 480p","items":[{"quality":{"id":12,"name":"WEBRip-480p","source":"webRip","resolution":480},"items":[],"allowed":false},{"quality":{"id":8,"name":"WEBDL-480p","source":"web","resolution":480},"items":[],"allowed":false}],"allowed":false,"id":1000},{"quality":{"id":2,"name":"DVD","source":"dvd","resolution":480},"items":[],"allowed":false},{"quality":{"id":13,"name":"Bluray-480p","source":"bluray","resolution":480},"items":[],"allowed":false},{"quality":{"id":22,"name":"Bluray-576p","source":"bluray","resolution":576},"items":[],"allowed":false},{"quality":{"id":4,"name":"HDTV-720p","source":"television","resolution":720},"items":[],"allowed":false},{"quality":{"id":9,"name":"HDTV-1080p","source":"television","resolution":1080},"items":[],"allowed":true},{"quality":{"id":10,"name":"Raw-HD","source":"televisionRaw","resolution":1080},"items":[],"allowed":false},{"name":"WEB 720p","items":[{"quality":{"id":14,"name":"WEBRip-720p","source":"webRip","resolution":720},"items":[],"allowed":false},{"quality":{"id":5,"name":"WEBDL-720p","source":"web","resolution":720},"items":[],"allowed":false}],"allowed":false,"id":1001},{"quality":{"id":6,"name":"Bluray-720p","source":"bluray","resolution":720},"items":[],"allowed":false},{"name":"WEB 1080p","items":[{"quality":{"id":15,"name":"WEBRip-1080p","source":"webRip","resolution":1080},"items":[],"allowed":true},{"quality":{"id":3,"name":"WEBDL-1080p","source":"web","resolution":1080},"items":[],"allowed":true}],"allowed":true,"id":1002},{"quality":{"id":7,"name":"Bluray-1080p","source":"bluray","resolution":1080},"items":[],"allowed":true},{"quality":{"id":20,"name":"Bluray-1080p Remux","source":"blurayRaw","resolution":1080},"items":[],"allowed":false},{"quality":{"id":16,"name":"HDTV-2160p","source":"television","resolution":2160},"items":[],"allowed":false},{"name":"WEB 2160p","items":[{"quality":{"id":17,"name":"WEBRip-2160p","source":"webRip","resolution":2160},"items":[],"allowed":false},{"quality":{"id":18,"name":"WEBDL-2160p","source":"web","resolution":2160},"items":[],"allowed":false}],"allowed":false,"id":1003},{"quality":{"id":19,"name":"Bluray-2160p","source":"bluray","resolution":2160},"items":[],"allowed":false},{"quality":{"id":21,"name":"Bluray-2160p Remux","source":"blurayRaw","resolution":2160},"items":[],"allowed":false}],"minFormatScore":50,"cutoffFormatScore":0,"minUpgradeFormatScore":1,"formatItems":[{"format":'$FORMAT_ID_2160',"name":"ORIG_2160","score":0},{"format":'$FORMAT_ID_SUBS',"name":"SUBS","score":15},{"format":'$FORMAT_ID_LANG',"name":"LANG_OTHER","score":10},{"format":'$FORMAT_ID_1080',"name":"ORIG_1080","score":50}],"id":'"$RADARR_QP_ID"'}'
        fi
        RADARR_QP_ID=$(echo $RADARR_QP | ${pkgs.jq}/bin/jq '.[] | select(.name == "Ultra-HD") | .id')
        if [ -n "$RADARR_QP_ID" ]; then
          ${pkgs.curl}/bin/curl -s -X PUT "$RADARR_URL/api/v3/qualityprofile/$RADARR_QP_ID" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json' \
            -d '{"name":"Ultra-HD","upgradeAllowed":false,"cutoff":16,"items":[{"quality":{"id":0,"name":"Unknown","source":"unknown","resolution":0},"items":[],"allowed":false},{"quality":{"id":1,"name":"SDTV","source":"television","resolution":480},"items":[],"allowed":false},{"name":"WEB 480p","items":[{"quality":{"id":12,"name":"WEBRip-480p","source":"webRip","resolution":480},"items":[],"allowed":false},{"quality":{"id":8,"name":"WEBDL-480p","source":"web","resolution":480},"items":[],"allowed":false}],"allowed":false,"id":1000},{"quality":{"id":2,"name":"DVD","source":"dvd","resolution":480},"items":[],"allowed":false},{"quality":{"id":13,"name":"Bluray-480p","source":"bluray","resolution":480},"items":[],"allowed":false},{"quality":{"id":22,"name":"Bluray-576p","source":"bluray","resolution":576},"items":[],"allowed":false},{"quality":{"id":4,"name":"HDTV-720p","source":"television","resolution":720},"items":[],"allowed":false},{"quality":{"id":9,"name":"HDTV-1080p","source":"television","resolution":1080},"items":[],"allowed":false},{"quality":{"id":10,"name":"Raw-HD","source":"televisionRaw","resolution":1080},"items":[],"allowed":false},{"name":"WEB 720p","items":[{"quality":{"id":14,"name":"WEBRip-720p","source":"webRip","resolution":720},"items":[],"allowed":false},{"quality":{"id":5,"name":"WEBDL-720p","source":"web","resolution":720},"items":[],"allowed":false}],"allowed":false,"id":1001},{"quality":{"id":6,"name":"Bluray-720p","source":"bluray","resolution":720},"items":[],"allowed":false},{"name":"WEB 1080p","items":[{"quality":{"id":15,"name":"WEBRip-1080p","source":"webRip","resolution":1080},"items":[],"allowed":false},{"quality":{"id":3,"name":"WEBDL-1080p","source":"web","resolution":1080},"items":[],"allowed":false}],"allowed":false,"id":1002},{"quality":{"id":7,"name":"Bluray-1080p","source":"bluray","resolution":1080},"items":[],"allowed":false},{"quality":{"id":20,"name":"Bluray-1080p Remux","source":"blurayRaw","resolution":1080},"items":[],"allowed":false},{"quality":{"id":16,"name":"HDTV-2160p","source":"television","resolution":2160},"items":[],"allowed":true},{"name":"WEB 2160p","items":[{"quality":{"id":17,"name":"WEBRip-2160p","source":"webRip","resolution":2160},"items":[],"allowed":true},{"quality":{"id":18,"name":"WEBDL-2160p","source":"web","resolution":2160},"items":[],"allowed":true}],"allowed":true,"id":1003},{"quality":{"id":19,"name":"Bluray-2160p","source":"bluray","resolution":2160},"items":[],"allowed":true},{"quality":{"id":21,"name":"Bluray-2160p Remux","source":"blurayRaw","resolution":2160},"items":[],"allowed":false}],"minFormatScore":50,"cutoffFormatScore":0,"minUpgradeFormatScore":1,"formatItems":[{"format":'$FORMAT_ID_2160',"name":"ORIG_2160","score":50},{"format":'$FORMAT_ID_SUBS',"name":"SUBS","score":0},{"format":'$FORMAT_ID_LANG',"name":"LANG_OTHER","score":10},{"format":'$FORMAT_ID_1080',"name":"ORIG_1080","score":0}],"id":'"$RADARR_QP_ID"'}'
        fi

        ${lib.optionalString (serverCfg.containers?transmission ) ''
          RADARR_DL=$(${pkgs.curl}/bin/curl -s -X GET "$RADARR_URL/api/v3/downloadclient" \
            -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
            -H 'Accept: application/json' -H 'Content-Type: application/json')
          
          if ! echo "$RADARR_DL" | ${pkgs.jq}/bin/jq -e 'any(.[]? ; .name == "Transmission")' > /dev/null; then
            echo "Radarr Setup - Transmission"
            ${pkgs.curl}/bin/curl -s -X POST "$RADARR_URL/api/v3/downloadclient" \
              -H "X-Api-Key: $RADARR__AUTH__APIKEY" -H 'X-Radarr-Client: true' \
              -H 'Accept: application/json' -H 'Content-Type: application/json' \
              -d '{"enable":true,"protocol":"torrent","priority":1,"categories":[],"supportsCategories":true,"name":"Transmission","fields":[{"name":"host","value":"transmission-server"},{"name":"port","value":9091},{"name":"useSsl","value":false},{"name":"urlBase","value":"/transmission/"},{"name":"username"},{"name":"password"},{"name":"category"},{"name":"directory"},{"name":"priority","value":0},{"name":"addPaused","value":false}],"implementationName":"Transmission","implementation":"Transmission","configContract":"TransmissionSettings","infoLink":"https://wiki.servarr.com/radarr/supported#transmission","tags":[]}' 
          fi
          ''}


      ''}

      ${lib.optionalString (builtins.elem "lidarr" (containerCfg.extra.modules)) ''
      ''}
      ${lib.optionalString (builtins.elem "readarr" (containerCfg.extra.modules)) ''
      ''}
      ${lib.optionalString (builtins.elem "mylarr" (containerCfg.extra.modules)) ''
      ''}
      ${lib.optionalString (builtins.elem "bazarr" (containerCfg.extra.modules)) ''
      ''}
      ${lib.optionalString (builtins.elem "seerr" (containerCfg.extra.modules)) ''
      ''}

    '';
  };
  };
}
