{{/* Make sure all variables are set properly */}}
{{- include "tc.v1.common.loader.init" . -}}

{{- $adv := list -}}
{{- $serverURL := "" -}}
{{- $protocol := "http" -}}
{{- if .Values.plex.requireHTTPS -}}
  {{- $protocol = "https" -}}
{{- end -}}

{{- $_ := set .Values.workload.main.podSpec.containers.main.probes.liveness "scheme" $protocol -}}
{{- $_ := set .Values.workload.main.podSpec.containers.main.probes.readiness "scheme" $protocol -}}
{{- $_ := set .Values.workload.main.podSpec.containers.main.probes.startup "scheme" $protocol -}}
{{- $_ := set .Values.service.main.ports.main "protocol" $protocol -}}

{{- if not (eq .Values.service.main.type "ClusterIP") -}}
  {{- $serverURL = (printf "%v://%v:%v" $protocol .Values.plex.serverIP .Values.service.main.ports.main.port) -}}
{{- end -}}

{{- $serverIngressURL := "" -}}
{{- $host := "" -}}
{{- if .Values.ingress.main.enabled -}}
  {{- with (first .Values.ingress.main.hosts) -}}
    {{- $host = .host -}}
    {{- $serverIngressURL = (printf "https://%v" .host) -}}
  {{- end -}}
{{- end -}}

{{- $serverIngressURLPort := "" -}}
{{- if $host -}}
  {{- if not (eq .Values.service.main.type "ClusterIP") -}}
    {{- $port := .Values.service.main.ports.main.port -}}
    {{- $serverIngressURLPort = printf "https://%v:%v" $host $port -}}
  {{- end -}}
{{- end -}}

{{- with $serverURL -}}
  {{- $adv = append $adv . -}}
{{- end -}}
{{- with $serverIngressURL -}}
  {{- $adv = append $adv . -}}
{{- end -}}
{{- with $serverIngressURLPort -}}
  {{- $adv = append $adv . -}}
{{- end -}}
{{- with .Values.plex.additionalAdvertiseURL -}}
  {{- $adv = append $adv . -}}
{{- end -}}

{{- $_ := set .Values.workload.main.podSpec.containers.main.env "ADVERTISE_IP" (join "," $adv) -}}

{{/* Render the templates */}}
{{- include "tc.v1.common.loader.apply" . -}}
