{{ $ENABLE_AUTH := .Env.ENABLE_AUTH | default "0" | toBool }}
{{ $ENABLE_SCTP := .Env.ENABLE_SCTP | default "0" | toBool }}
{{ $AUTH_TYPE := .Env.AUTH_TYPE | default "internal" }}
{{ $ENABLE_RECORDING := .Env.ENABLE_RECORDING | default "0" | toBool }}
{{ $ENABLE_OCTO := .Env.ENABLE_OCTO | default "0" | toBool }}
{{ $ENABLE_AUTO_LOGIN := .Env.ENABLE_AUTO_LOGIN | default "1" | toBool }}
{{ $JICOFO_AUTH_USER := .Env.JICOFO_AUTH_USER | default "focus" -}}
{{ $JIBRI_BREWERY_MUC := .Env.JIBRI_BREWERY_MUC | default "jibribrewery" -}}
{{ $JIGASI_BREWERY_MUC := .Env.JIGASI_BREWERY_MUC | default "jigasibrewery" -}}
{{ $JVB_BREWERY_MUC := .Env.JVB_BREWERY_MUC | default "jvbbrewery" -}}
{{ $JIBRI_PENDING_TIMEOUT := .Env.JIBRI_PENDING_TIMEOUT | default 90 -}}
{{ $XMPP_AUTH_DOMAIN := .Env.XMPP_AUTH_DOMAIN | default "auth.meet.jitsi" -}}
{{ $XMPP_MUC_DOMAIN := .Env.XMPP_MUC_DOMAIN | default "muc.meet.jitsi" -}}
{{ $XMPP_INTERNAL_MUC_DOMAIN := .Env.XMPP_INTERNAL_MUC_DOMAIN | default "internal-muc.meet.jitsi" -}}
{{ $XMPP_DOMAIN := .Env.XMPP_DOMAIN | default "meet.jitsi" -}}
{{ $XMPP_RECORDER_DOMAIN := .Env.XMPP_RECORDER_DOMAIN | default "recorder.meet.jitsi" -}}
{{ $XMPP_PORT := .Env.XMPP_PORT | default "5222" -}}
{{ $XMPP_SERVER := .Env.XMPP_SERVER | default "xmpp.meet.jitsi" -}}

jicofo {
    {{ if $ENABLE_AUTH }}
    authentication {
      enabled = true
      // The type of authentication. Supported values are XMPP, JWT or SHIBBOLETH (default).
      {{ if eq $AUTH_TYPE "jwt" }}
      type = JWT
      {{ else if eq $AUTH_TYPE "shibboleth" }}
      type = SHIBBOLETH
      {{ else }}
      type = XMPP
      {{ end }}
      {{ if eq $AUTH_TYPE "shibboleth" }}
      login-url = "shibboleth:default"
      logout-url = "shibboleth:default"
      {{ else }}
      login-url = "{{ $XMPP_DOMAIN }}"
      {{ end }}
      enable-auto-login={{ $ENABLE_AUTO_LOGIN }}
    }
    {{ end }}

    // Configuration related to jitsi-videobridge
    bridge {
      {{ if .Env.MAX_BRIDGE_PARTICIPANTS }}
      max-bridge-participants = "{{ .Env.MAX_BRIDGE_PARTICIPANTS }}"
      {{ end }}

      {{ if .Env.BRIDGE_AVG_PARTICIPANT_STRESS }}
      // The assumed average stress per participant. default is 0.01
      average-participant-stress = "{{ .Env.BRIDGE_AVG_PARTICIPANT_STRESS }}"
      {{ end }}

      {{ if .Env.BRIDGE_STRESS_THRESHOLD }}
      // The stress level above which a bridge is considered overstressed. 0.8 is the default value
      stress-threshold = "{{ .Env.BRIDGE_STRESS_THRESHOLD }}"
      {{ end }}

      {{ if .Env.OCTO_BRIDGE_SELECTION_STRATEGY }}
      selection-strategy = "{{ .Env.OCTO_BRIDGE_SELECTION_STRATEGY }}"
      {{ end }}

      {{ if .Env.JICOFO_ENABLE_BRIDGE_HEALTH_CHECKS }}
      health-checks {
        enabled = {{ .Env.JICOFO_ENABLE_BRIDGE_HEALTH_CHECKS | toBool }}
      }
      {{ end }}

      brewery-jid = "{{ $JVB_BREWERY_MUC }}@{{ $XMPP_INTERNAL_MUC_DOMAIN }}"
    }
    // Configure the codecs and RTP extensions to be used in the offer sent to clients.
    codec {
      video {
        {{ if .Env.ENABLE_CODEC_VP8 }}
        vp8 {
          enabled = "{{ .Env.ENABLE_CODEC_VP8 }}"
        }
        {{ end }}
        {{ if .Env.ENABLE_CODEC_VP9 }}
        vp9 {
          enabled = "{{ .Env.ENABLE_CODEC_VP9 }}"
        }
        {{ end }}
        {{ if .Env.ENABLE_CODEC_H264 }}
        h264 {
          enabled = "{{ .Env.ENABLE_CODEC_H264 }}"
        }
        {{ end }}
      }
    }

    conference {
      {{ if .Env.ENABLE_AUTO_OWNER }}
      enable-auto-owner = {{ .Env.ENABLE_AUTO_OWNER | toBool }}
      {{ end }}

      {{ if .Env.JICOFO_CONF_INITIAL_PARTICIPANT_WAIT_TIMEOUT }}
      initial-timeout = "{{ .Env.JICOFO_CONF_INITIAL_PARTICIPANT_WAIT_TIMEOUT }}"
      {{ end }}

      {{ if .Env.JICOFO_CONF_SINGLE_PARTICIPANT_TIMEOUT }}
      single-participant-timeout = "{{ .Env.JICOFO_CONF_SINGLE_PARTICIPANT_TIMEOUT }}"
      {{ end }}
    }

    {{ if .Env.JICOFO_ENABLE_HEALTH_CHECKS }}
    // Configuration for the internal health checks performed by jicofo.
    health {
      // Whether to perform health checks.
      enabled = {{ .Env.JICOFO_ENABLE_HEALTH_CHECKS | toBool }}
    }
    {{ end }}

    {{ if $ENABLE_RECORDING }}
    jibri {
      brewery-jid = "{{ $JIBRI_BREWERY_MUC }}@{{ $XMPP_INTERNAL_MUC_DOMAIN }}"
      {{ if .Env.JIBRI_REQUEST_RETRIES }}
      num-retries = "{{ .Env.JIBRI_REQUEST_RETRIES }}"
      {{ end }}
      pending-timeout = "{{ $JIBRI_PENDING_TIMEOUT }}"
    }
    {{ end }}

    {{ if and .Env.JIGASI_SIP_URI $JIGASI_BREWERY_MUC }}
    jigasi {
      brewery-jid = "{{ $JIGASI_BREWERY_MUC }}@{{ $XMPP_INTERNAL_MUC_DOMAIN }}"
    }
    {{ end }}

    octo {
      // Whether or not to use Octo. Note that when enabled, its use will be determined by
      // $jicofo.bridge.selection-strategy. There's a corresponding flag in the JVB and these
      // two MUST be in sync (otherwise bridges will crash because they won't know how to
      // deal with octo channels).
      enabled = {{ $ENABLE_OCTO }}

      id = "{{ .Env.JICOFO_SHORT_ID | default "1" }}"
    }

    sctp {
      enabled = {{ $ENABLE_SCTP }}
    }

    xmpp {
      client {
        enabled = true
        hostname = "{{ $XMPP_SERVER }}"
        port = "{{ $XMPP_PORT }}"
        domain = "{{ $XMPP_AUTH_DOMAIN }}"
        username = "{{ $JICOFO_AUTH_USER }}"
        password = "{{ .Env.JICOFO_AUTH_PASSWORD }}"
        conference-muc-jid = "{{ $XMPP_MUC_DOMAIN }}"
        client-proxy = "focus.{{ $XMPP_DOMAIN }}"
        disable-certificate-verification = true
      }
      {{ if $ENABLE_RECORDING }}
      trusted-domains = [ "{{ $XMPP_RECORDER_DOMAIN }}" ]
      {{ end }}
    }
}
