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

{{- $adv := list -}}
{{- $serverURL := "" }}
{{- if not (eq .Values.service.main.type "ClusterIP") }}
  {{- $serverURL = (printf "%v://%v:%v" (.Values.service.main.ports.main.protocol | lower) .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" . }}
