{{/* Make sure all variables are set properly */}}
{{- include "common.values.setup" . }}

{{/* Append the hardcoded settings */}}
{{- define "qbittorrent.harcodedValues" -}}
{{ if and .Values.settings.automaticPortSetup }}
persistence:
  qbittorrent-scripts:
    enabled: "true"
    mountPath: "/config/custom-cont-init.d"
    type: "custom"
    volumeSpec:
      configMap:
        name: {{ include "common.names.fullname" . }}-scripts
        defaultMode: 511
{{ end }}

{{ if .Values.metrics.enabled }}
additionalContainers:
  exporter:
    name: exporter
    image: "{{ .Values.metrics.exporter.image.repository }}:{{ .Values.metrics.exporter.image.tag }}"
    imagePullPolicy: {{ .Values.metrics.exporter.image.pullPolicy }}
    env:
      - name: QBITTORRENT_HOST
        value: "http://localhost"
      - name: QBITTORRENT_PORT
        value: "{{ .Values.service.main.ports.http.port }}"
      - name: QBITTORRENT_USER
        value: "{{ .Values.metrics.exporter.env.user }}"
      - name: QBITTORRENT_PASS
        value: "{{ .Values.metrics.exporter.env.password }}"
      - name: EXPORTER_PORT
        value: "{{ .Values.metrics.exporter.env.port }}"
      - name: EXPORTER_LOG_LEVEL
        value: "{{ .Values.metrics.exporter.env.logLevel }}"
    ports:
      - name: metrics
        containerPort: {{ .Values.metrics.exporter.env.port }}

service:
  metrics:
    enabled: true
    ports:
      metrics:
        enabled: true
        protocol: TCP
        port: {{ .Values.metrics.exporter.env.port }}
{{ end }}
{{- end -}}
{{- $_ := mergeOverwrite .Values (include "qbittorrent.harcodedValues" . | fromYaml) -}}

{{/* Render the templates */}}
{{ include "common.all" . }}
