{{- if ne (printf "%s" .Values.minecraftServer.eula) "FALSE" }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "minecraft.fullname" . }}
  {{- if .Values.deploymentAnnotations }}
  annotations:
    {{- range $key, $value := .Values.deploymentAnnotations }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
  {{- end }}
  labels:
    app: {{ template "minecraft.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  strategy:
    type: {{ .Values.strategyType }}
  selector:
    matchLabels:
      app: {{ template "minecraft.fullname" . }}
  template:
    metadata:
      labels:
        app: {{ template "minecraft.fullname" . }}
      {{- if .Values.podAnnotations }}
      annotations:
        {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
      {{- end }}
    spec:
      securityContext:
        runAsUser: {{ .Values.securityContext.runAsUser }}
        fsGroup: {{ .Values.securityContext.fsGroup }}
      containers:
      - name: {{ template "minecraft.fullname" . }}
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: Always
        resources:
{{ toYaml .Values.resources | indent 10 }}
        readinessProbe:
          exec:
            command: 
{{ toYaml .Values.readinessProbe.command | indent 14 }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
        livenessProbe:
          exec:
            command:
{{ toYaml .Values.livenessProbe.command | indent 14 }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          successThreshold: {{ .Values.livenessProbe.successThreshold }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
        env:
        - name: EULA
          value: {{ .Values.minecraftServer.eula | quote }}
        - name: TYPE
          value: {{ default "" .Values.minecraftServer.type | quote }}
        {{- if eq .Values.minecraftServer.type "FORGE" }}
        {{- if .Values.minecraftServer.forgeInstallerUrl }}
        - name: FORGE_INSTALLER_URL
          value: {{ .Values.minecraftServer.forgeInstallerUrl | quote }}
        {{- else }}
        - name: FORGEVERSION
          value: {{ .Values.minecraftServer.forgeVersion | quote }}
        {{- end }}
        {{- else if eq .Values.minecraftServer.type "SPIGOT" }}
        - name: SPIGOT_DOWNLOAD_URL
          value: {{ .Values.minecraftServer.spigotDownloadUrl | quote }}
        {{- else if eq .Values.minecraftServer.type "BUKKIT" }}
        - name: BUKKIT_DOWNLOAD_URL
          value: {{ .Values.minecraftServer.bukkitDownloadUrl | quote }}
        {{- else if eq .Values.minecraftServer.type "PAPER" }}
        - name: PAPER_DOWNLOAD_URL
          value: {{ .Values.minecraftServer.paperDownloadUrl | quote }}
        {{- else if eq .Values.minecraftServer.type "FTB" }}
        - name: FTB_SERVER_MOD
          value: {{ .Values.minecraftServer.ftbServerMod | quote }}
        - name: FTB_LEGACYJAVAFIXER
          value: {{ default false .Values.minecraftServer.ftbLegacyJavaFixer | quote }}
        {{- end }}
        - name: VERSION
          value: {{ .Values.minecraftServer.version | quote }}
        - name: DIFFICULTY
          value: {{ .Values.minecraftServer.difficulty | quote }}
        - name: WHITELIST
          value: {{ default "" .Values.minecraftServer.whitelist | quote }}
        - name: OPS
          value: {{ default "" .Values.minecraftServer.ops | quote }}
        - name: ICON
          value: {{ default "" .Values.minecraftServer.icon | quote }}
        - name: MAX_PLAYERS
          value: {{ .Values.minecraftServer.maxPlayers | quote }}
        - name: MAX_WORLD_SIZE
          value: {{ .Values.minecraftServer.maxWorldSize | quote }}
        - name: ALLOW_NETHER
          value: {{ .Values.minecraftServer.allowNether | quote }}
        - name: ANNOUNCE_PLAYER_ACHIEVEMENTS
          value: {{ .Values.minecraftServer.announcePlayerAchievements | quote }}
        - name: ENABLE_COMMAND_BLOCK
          value: {{ .Values.minecraftServer.enableCommandBlock | quote }}
        - name: FORCE_gameMode
          value: {{ .Values.minecraftServer.forcegameMode | quote }}
        {{- if .Values.minecraftServer.forceReDownload }}
        - name: FORCE_REDOWNLOAD
          value: "TRUE"
        {{- end }}
        - name: GENERATE_STRUCTURES
          value: {{ .Values.minecraftServer.generateStructures | quote }}
        - name: HARDCORE
          value: {{ .Values.minecraftServer.hardcore | quote }}
        - name: MAX_BUILD_HEIGHT
          value: {{ .Values.minecraftServer.maxBuildHeight | quote }}
        - name: MAX_TICK_TIME
          value: {{ .Values.minecraftServer.maxTickTime | quote }}
        - name: SPAWN_ANIMALS
          value: {{ .Values.minecraftServer.spawnAnimals | quote }}
        - name: SPAWN_MONSTERS
          value: {{ .Values.minecraftServer.spawnMonsters | quote }}
        - name: SPAWN_NPCS
          value: {{ .Values.minecraftServer.spawnNPCs | quote }}
        - name: VIEW_DISTANCE
          value: {{ .Values.minecraftServer.viewDistance | quote }}
        - name: SEED
          value: {{ default "" .Values.minecraftServer.levelSeed | quote }}
        - name: MODE
          value: {{ .Values.minecraftServer.gameMode | quote }}
        - name: MOTD
          value: {{ .Values.minecraftServer.motd | quote }}
        - name: PVP
          value: {{ .Values.minecraftServer.pvp | quote }}
        - name: LEVEL_TYPE
          value: {{ .Values.minecraftServer.levelType | quote }}
        - name: GENERATOR_SETTINGS
          value: {{ default "" .Values.minecraftServer.generatorSettings | quote }}
        - name: LEVEL
          value: {{ .Values.minecraftServer.worldSaveName | quote }}
        {{- if .Values.minecraftServer.downloadWorldUrl }}
        - name: WORLD
          value: {{ .Values.minecraftServer.downloadWorldUrl | quote }}
        {{- end }}
        {{- if .Values.minecraftServer.downloadModpackUrl }}
        - name: MODPACK
          value: {{ .Values.minecraftServer.downloadModpackUrl | quote }}
        {{- if .Values.minecraftServer.removeOldMods }}
        - name: REMOVE_OLD_MODS
          value: "TRUE"
        {{- end }}
        {{- end }}
        - name: ONLINE_MODE
          value: {{ .Values.minecraftServer.onlineMode | quote }}
        - name: MEMORY
          value: {{ .Values.minecraftServer.memory | quote }}
        - name: JVM_OPTS
          value: {{ .Values.minecraftServer.jvmOpts | quote }}
        - name: JVM_XX_OPTS
          value: {{ .Values.minecraftServer.jvmXXOpts | quote }}

        {{- if .Values.minecraftServer.rcon.enabled }}
        - name: ENABLE_RCON
          value: "true"
        - name: RCON_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "minecraft.fullname" . }}
              key: rcon-password
        {{- end }}

        {{- if .Values.minecraftServer.query.enabled }}
        - name: ENABLE_QUERY
          value: "true"
        - name: QUERY_PORT
          value: {{ .Values.minecraftServer.query.port }}
        {{- end }}

        {{- range $key, $value := .Values.extraEnv }}
        - name: {{ $key }}
          value: {{ $value }}
        {{- end }}

        ports:
        - name: minecraft
          containerPort: 25565
          protocol: TCP
        {{- if .Values.minecraftServer.rcon.enabled }}
        - name: rcon
          containerPort: {{ .Values.minecraftServer.rcon.port }}
          protocol: TCP
        {{- end }}
        volumeMounts:
        - name: datadir
          mountPath: /data
      volumes:
      - name: datadir
      {{- if .Values.persistence.dataDir.enabled }}
        persistentVolumeClaim:
        {{- if .Values.persistence.dataDir.existingClaim }}
          claimName: {{ .Values.persistence.dataDir.existingClaim }}
        {{- else }}
          claimName: {{ template "minecraft.fullname" . }}-datadir
        {{- end }}
      {{- else }}
        emptyDir: {}
      {{- end }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
{{ end }}
