{{/*
Common resource name: uses Helm release name
*/}}
{{- define "jupyterlab.name" -}}
{{- .Release.Name }}
{{- end }}

{{/*
ConfigMap name: <release-name>-scripts
*/}}
{{- define "jupyterlab.configMapName" -}}
{{- printf "%s-scripts" .Release.Name }}
{{- end }}

{{/*
Home volume PVC name: pvc-<release-name>-home
*/}}
{{- define "jupyterlab.homePvcName" -}}
{{- printf "pvc-%s-home" .Release.Name }}
{{- end }}

{{/*
Data volume PVC name: pvc-<release-name>-data
*/}}
{{- define "jupyterlab.dataPvcName" -}}
{{- printf "pvc-%s-data" .Release.Name }}
{{- end }}

{{/*
Resolved home directory path (mountPath or default /home/jovyan)
*/}}
{{- define "jupyterlab.homeDir" -}}
{{- .Values.homeVolume.mountPath | default "/home/jovyan" }}
{{- end }}

{{/*
Resolved data volume mount path: <homeDir>/<mountPath or "data">
*/}}
{{- define "jupyterlab.dataDir" -}}
{{- $home := include "jupyterlab.homeDir" . }}
{{- $sub := .Values.dataVolume.mountPath | default "data" }}
{{- printf "%s/%s" $home $sub }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "jupyterlab.labels" -}}
app: {{ include "jupyterlab.name" . }}
app.kubernetes.io/name: jupyterlab
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
{{- end }}

{{/*
Full image reference: [registry/]repository[:tag]
If repository already contains a tag (e.g. "repo:tag"), tag field is ignored.
*/}}
{{- define "jupyterlab.image" -}}
{{- $repo := .Values.image.repository }}
{{- $tag := .Values.image.tag }}
{{- $hasTag := contains ":" $repo }}
{{- if .Values.image.registry }}
{{- if $hasTag }}
{{- printf "%s/%s" .Values.image.registry $repo }}
{{- else if $tag }}
{{- printf "%s/%s:%s" .Values.image.registry $repo $tag }}
{{- else }}
{{- printf "%s/%s" .Values.image.registry $repo }}
{{- end }}
{{- else }}
{{- if $hasTag }}
{{- $repo }}
{{- else if $tag }}
{{- printf "%s:%s" $repo $tag }}
{{- else }}
{{- $repo }}
{{- end }}
{{- end }}
{{- end }}

{{/*
JupyterLab startup command
*/}}
{{- define "jupyterlab.command" -}}
{{- $token := .Values.jupyterConfig.token }}
{{- $homeDir := include "jupyterlab.homeDir" . }}
- /bin/bash
- -c
- |
  start.sh jupyter lab \
    --core-mode \
    {{- if $token }}
    --ServerApp.password='sha256::{{ $token | sha256sum | trimSuffix "  -" }}' \
    --ServerApp.token='' \
    {{- else }}
    --ServerApp.token='' \
    --ServerApp.password='' \
    {{- end }}
    --ServerApp.ip='0.0.0.0' \
    --ServerApp.allow_origin='*' \
    --IdentityProvider.cookie_options="max-age=28800" \
    --ServerApp.disable_check_xsrf=True \
    --ServerApp.quit_button=False \
    --ServerApp.allow_root=False \
    --ServerApp.terminals_enabled=True \
    --ServerApp.root_dir={{ $homeDir }}
{{- end }}
