# CogStack Community Edition is installed

# 1. AnonCAT Service port forward
export ANONCAT_POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name=anoncat-service,app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $ANONCAT_POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
kubectl --namespace {{ .Release.Namespace }} port-forward $ANONCAT_POD_NAME 5001:$CONTAINER_PORT --pod-running-timeout=5m0s & 

# 2.Medcat service port forward
export MEDCAT_POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name=medcat-service,app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $MEDCAT_POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
kubectl --namespace {{ .Release.Namespace }} port-forward $MEDCAT_POD_NAME 5000:$CONTAINER_PORT --pod-running-timeout=5m0s &

# 3. MedCAT Trainer Port forward
export TRAINER_POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name=medcat-trainer,app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $TRAINER_POD_NAME -o jsonpath="{.spec.containers[?(@.name==\"nginx\")].ports[0].containerPort}")
export CONTAINER_PORT_API=$(kubectl get pod --namespace {{ .Release.Namespace }} $TRAINER_POD_NAME -o jsonpath="{.spec.containers[?(@.name==\"medcat-trainer\")].ports[0].containerPort}")
kubectl --namespace {{ .Release.Namespace }} port-forward $TRAINER_POD_NAME 8080:$CONTAINER_PORT 8001:$CONTAINER_PORT_API &

{{- if .Values.opensearch.enabled }}
# 4. Opensearch
export OPENSEARCH_SERVICE={{ template "opensearch.serviceName" .Subcharts.opensearch }}
export OPENSEARCH_PORT={{ .Values.opensearch.httpPort }}
kubectl --namespace {{ .Release.Namespace }} port-forward svc/$OPENSEARCH_SERVICE 9200:$OPENSEARCH_PORT &
{{- end }}

{{- if index .Values "opensearch-dashboards" "enabled" }}
export OPENSEARCH_DASHBOARD_SERVICE={{ include "opensearch-dashboards.fullname" (index .Subcharts "opensearch-dashboards" ) }}
export OPENSEARCH_DASHBOARD_PORT={{ index .Values "opensearch-dashboards" "service" "port"  }}
kubectl --namespace {{ .Release.Namespace }} port-forward svc/$OPENSEARCH_DASHBOARD_SERVICE 5601:$OPENSEARCH_DASHBOARD_PORT &
{{- end }}

{{- if index .Values "cogstack-jupyterhub" "enabled" }}
# 5. Jupyterhub
export JUPYTERHUB_PROXY_PUBLIC_SERVICE=proxy-public
export JUPYTERHUB_PROXY_SERVICE_PORT=$(kubectl get svc $JUPYTERHUB_PROXY_PUBLIC_SERVICE -o jsonpath="{.spec.ports[0].port}" )
kubectl --namespace {{ .Release.Namespace }} port-forward svc/$JUPYTERHUB_PROXY_PUBLIC_SERVICE 8000:$JUPYTERHUB_PROXY_SERVICE_PORT &
{{- end }}

echo "Visit http://127.0.0.1:5000 to use MedCAT Service"
echo "Visit http://127.0.0.1:5001 to use AnonCAT"
echo "Visit http://127.0.0.1:8080 to use MedCAT Trainer"
{{ if .Values.opensearch.enabled }}echo "Visit https://127.0.0.1:9200 to use OpenSearch"{{- end }}
{{ if index .Values "opensearch-dashboards" "enabled" }}echo "Visit http://127.0.0.1:5601 to use OpenSearch Dashboards"{{- end }}
{{ if index .Values "cogstack-jupyterhub" "enabled" }}echo "Visit http://127.0.0.1:8000 to use jupyterhub"{{- end }}
{{ if index .Values "cogstack-jupyterhub" "enabled" }}echo "Visit http://127.0.0.1:8000/user/admin/notebooks/medcat-service-tutorial.ipynb to get started with a tutorial"{{- end }}

# Setup Complete
# Run this command line to setup port-forwarding and access services
# `helm get notes {{ .Release.Name }} | bash`
