# ============================================================================
# Browser setup
# ============================================================================
# Try to find browser executable
checkBrowserLocation() {
    # Define defaults
    # Known chrome locations
    WEB_BROWSER_LOCATION_A=/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe
    WEB_BROWSER_LOCATION_B=/mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe
    WEB_BROWSER_LOCATION_C=/mnt/c/Users/${USER}/AppData/Local/Google/Chrome/Application/chrome.exe
    WEB_BROWSER_LOCATION_D=/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe
    WEB_BROWSER_LOCATION_E=/c/Program\ Files/Google/Chrome/Application/chrome.exe
    WEB_BROWSER_LOCATION_F=/c/Users/${USER}/AppData/Local/Google/Chrome/Application/chrome.exe
    # Known edge locations
    WEB_BROWSER_LOCATION_G=/c/Program\ Files\ \(x86\)/Microsoft/Edge/Application/msedge.exe
    WEB_BROWSER_LOCATION_H=/mnt/c/Program\ Files\ \(x86\)/Microsoft/Edge/Application/msedge.exe

    if [[ -e $WEB_BROWSER_LOCATION_A ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_A}
    elif [[ -e $WEB_BROWSER_LOCATION_B ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_B}
    elif [[ -e $WEB_BROWSER_LOCATION_C ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_C}
    elif [[ -e $WEB_BROWSER_LOCATION_D ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_D}
    elif [[ -e $WEB_BROWSER_LOCATION_E ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_E}
    elif [[ -e $WEB_BROWSER_LOCATION_F ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_F}
    elif [[ -e $WEB_BROWSER_LOCATION_G ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_G}
    elif [[ -e $WEB_BROWSER_LOCATION_H ]] ; then
        WEB_BROWSER=${WEB_BROWSER_LOCATION_H}
    elif [[ open ]] ; then
        WEB_BROWSER=open
    else
        warning "Unable to find Chrome or Edge executable! Redirect for OpenShift login not possible."
    fi
}

checkBrowserLocation

# Speed up MC startup
alias mc='mc --nosubshell'
alias mcedit='mcedit --nosubshell'

# Alias for activating venv in folder
alias activate='source .venv/bin/activate'
alias pipx='python /etc/pipx/pipx.pyz'
alias pip='pip3'

#alias docker-compose='docker-compose.exe'
alias kihub='cd /Users/u241100/workspaces/KI_KIHUB'
alias priv='cd /Users/u241100/workspaces/private'
#alias oc='/mnt/c/devsbb/eaio-ng/oc/oc.exe'
#alias oc='/Users/u241100/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit/oc'
#alias oc='~/.local/bin/oc'

# OC AWS aliases
alias ocLoginAWS-Test1='oc login --token=${OPENSHIFT_TOKEN_AWS_TEST} --server=https://api.aws01t.sbb-aws-test.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.aws01t.sbb-aws-test.net/oauth/token/request'
alias ocLoginAWS-Test2='oc login --token=${OPENSHIFT_TOKEN_AWS_TEST} --server=https://api.aws02t.sbb-aws-test.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.aws02t.sbb-aws-test.net/oauth/token/request'
alias ocLoginAWS-Lisa='oc login --token=${OPENSHIFT_TOKEN_AWS_PROD} --server=https://api.lisa.sbb-aws.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.lisa.sbb-aws.net/oauth/token/request'
alias ocLoginAWS-Maggie='oc login --token=${OPENSHIFT_TOKEN_AWS_PROD} --server=https://api.maggie.sbb-aws.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.maggie.sbb-aws.net/oauth/token/request'
alias ocLoginAWS-WZU='oc login --token=${OPENSHIFT_TOKEN_WZU} --server=https://api.wzu01t.ocp-test.sbb-aws.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.wzu01t.ocp-test.sbb-aws.net/oauth/token/request'
# Note: ocAWS-* smart context-switch functions now defined in ~/.bash_oc_smart_aliases.sh

# OC SC aliases
#alias ocLoginSC01t='oc login --token=${OPENSHIFT_TOKEN_SC01} --server=https://api.sc01t.otc-test.sbb.ch:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.sc01t.otc-test.sbb.ch/oauth/token/request'
alias ocLoginSC02t='oc login --token=${OPENSHIFT_TOKEN_SC02} --server=https://api.sc02t.otc-test.sbb.ch:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.sc02t.otc-test.sbb.ch/oauth/token/request'
alias ocLoginSCSputnix='oc login --token=${OPENSHIFT_TOKEN_SCSputnix} --server=https://api.sputnix.otc.sbb.ch:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.sputnix.otc.sbb.ch/oauth/token/request'
#alias ocLoginSCMiraculix='oc login --token=${OPENSHIFT_TOKEN_SCMiraculix} --server=https://api.miraculix.otc.sbb.ch:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.miraculix.otc.sbb.ch/oauth/token/request'
# Note: ocSC-* smart context-switch functions now defined in ~/.bash_oc_smart_aliases.sh

alias ocLoginCUS-nonProd='oc login --token=${OPENSHIFT_TOKEN_SCSputnix} --server=https://api.cus01t.sbb-aws-test.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.cus01t.sbb-aws-test.net/oauth/token/request'
alias ocLoginCUS-prod='oc login --token=${OPENSHIFT_TOKEN_SCSputnix} --server=https://api.cus01p.sbb-aws.net:6443 || ${WEB_BROWSER} https://oauth-openshift.apps.cus01p.sbb-aws.net/oauth/token/request'

# SC02t
#alias ocArgoCD='oc config use-context kihub-argocd/api-sc02t-otc-test-sbb-ch:6443/u241100'
#alias ocKihubCI='oc config use-context kihub-ci/api-sc02t-otc-test-sbb-ch:6443/u241100'
#alias ocKihubDev='oc config use-context kihub-u241100-dev/api-sc02t-otc-test-sbb-ch:6443/u241100'
#alias ocKihubRT='oc config use-context kihub-rt/api-sc02t-otc-test-sbb-ch:6443/u241100'
#alias ocKihubTest='oc config use-context kihub-test/api-sc02t-otc-test-sbb-ch:6443/u241100'

# Sputnix
#alias ocArgoCDProd='oc config use-context kihub-argocd/api-sputnix-otc-sbb-ch:6443/u241100'
#alias ocKihubInt='oc config use-context kihub-int/api-sputnix-otc-sbb-ch:6443/u241100'
#alias ocKihubProd='oc config use-context kihub-prod/api-sputnix-otc-sbb-ch:6443/u241100'

# AWS01t
#alias ocKihubTekton='oc config use-context kihub-tekton/api-aws01t-sbb-aws-test-net:6443/u241100'
#alias ocKihubTestAWS='oc config use-context kihub-test/api-aws01t-sbb-aws-test-net:6443/u241100'

# Lisa
#alias ocKihubIntAWS='oc config use-context kihub-int/api-lisa-sbb-aws-net:6443/u241100'
#alias ocKihubProdAWS='oc config use-context kihub-prod/api-lisa-sbb-aws-net:6443/u241100'

# Some helpers
alias ocHelper='oc rsh $(oc get pods | grep openshift-helper | cut -d " " -f 1) bash'
alias ocJavaPod='oc rsh $(oc get pods | grep java | cut -d " " -f 1 | tail -n 1) bash'
alias ocP2='oc rsh $(oc get pods | grep p2-updateserver | cut -d " " -f 1 | tail -n 1) bash'
alias ocPods='oc get pods'
alias ocFw2DB='oc port-forward $(oc get pods -o custom-columns=POD:.metadata.name --no-headers -n kihub-database-dev | grep kihub-database-$USER) 1521:1521'
alias dc='docker-compose'
#alias setTI8M='cd ~/.m2 && rm -f settings.xml settings-security.xml && cp settings.xml.ti8m settings.xml && cp settings-security.xml.ti8m settings-security.xml && cd -'
#alias setSBB='cd ~/.m2  && rm -f settings.xml settings-security.xml && cp settings.xml.sbb  settings.xml && cp settings-security.xml.sbb  settings-security.xml && cd -'

#alias j8='. ~/j8o.sh'
#alias j11='. ~/j11o.sh'
#alias j17='. ~/j17o.sh'
#alias j21='. ~/j21o.sh'
#alias m38='. ~/mvn3.8.sh'
#alias m39='. ~/mvn3.9.sh'
alias hist='history | less'
alias awx-cli='awx-cli --conf.host https://deploy.sbb.ch --conf.username u241100 --conf.token ${ANSIBLE_TOWER_PASS} --conf.insecure'
alias bfg='java -jar ~/Downloads/bfg-1.14.0.jar'
alias gpp='git pull --prune'
alias gs='git status'

alias gw='./gradlew'

#alias vault=/mnt/c/Users/u241100/AppData/Local/Microsoft/WindowsApps/Vault.exe

#alias kubectl='minikube kubectl --'
#alias mk='minikube'
#alias mkd='minikube dashboard &'

alias dive='docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock docker.bin.sbb.ch/wagoodman/dive:latest $*'

alias installHooks='git archive \
    --remote=ssh://git@codessh.sbb.ch:7999/ki_kihub/pre-commit.git \
    master \
    install.sh | tar --to-stdout -xf - | bash'

alias docker-compose='docker compose'
alias lg='lazygit'
alias jk='j KI_KIHUB'

envDevLocation=/Users/u241100/workspaces/KI_KIHUB/cus-env-dev
alias dev-deploy='cd ${envDevLocation} && ./deploy2Dev.sh -d'
alias dev-deploy-update='cd ${envDevLocation} && git pull --prune && ./deploy2Dev.sh -df'
alias dev-deploy-force='cd ${envDevLocation} && ./deploy2Dev.sh -df'
alias dev-undeploy='cd ${envDevLocation} && ./deploy2Dev.sh -r'
alias dev-tools-deploy='cd ${envDevLocation} && ./deploy2Dev.sh -dt'
alias dev-tools-deploy-update='cd ${envDevLocation} && git pull --prune && ./deploy2Dev.sh -dft'
alias dev-tools-deploy-force='cd ${envDevLocation} && ./deploy2Dev.sh -dft'
alias dev-tools-undeploy='cd ${envDevLocation} && ./deploy2Dev.sh -rt'

# Sprintometer
alias sprintometerChart='cd /Users/u241100/workspaces/private/architeq/architeq/sprintometer-chart && helm package . && helm push sprintometer-chart-*.tgz oci://harbor.int.architeq.cloud/charts && rm -f sprintometer-chart-*.tgz && cd - >/dev/null'
alias sprintometerUmbrella='cd /Users/u241100/workspaces/private/architeq/architeq/sprintometer-umbrella-dev && helm package . && helm push sprintometer-umbrella-dev-*.tgz oci://harbor.int.architeq.cloud/charts && rm -f sprintometer-umbrella-dev-*.tgz && cd - >/dev/null'

# Helm deploy
docker_e2e_2015_dbris() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-e2e-2015-dbris
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_e2e_2017() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-e2e-2017
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_e2e_siri_kihub() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-e2e-siri-kihub
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_kihub() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-kihub
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_siri() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-siri
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_dbris() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-v2015a-aus-inbound-dbris
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_ans_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-ans-inbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_ans_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-ans-outbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_aus_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-aus-inbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_aus_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-aus-outbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_dfi_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-dfi-inbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_dfi_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-dfi-outbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_refaus_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-refaus-inbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_refaus_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-refaus-outbound
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}
docker_webservice() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-webservice
    helm install $(pwd | sed "s#.*/##") -f chart/values.yaml -f chart/values-docker.yaml chart
}

# Helm undeploy
rm_docker_e2e_2015_dbris() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-e2e-2015-dbris
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_e2e_2017() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-e2e-2017
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_e2e_siri_kihub() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-e2e-siri-kihub
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_kihub() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-kihub
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_siri() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-siri
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_dbris() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-v2015a-aus-inbound-dbris
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_ans_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-ans-inbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_ans_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-ans-outbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_aus_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-aus-inbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_aus_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-aus-outbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_dfi_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-dfi-inbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_dfi_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-dfi-outbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_refaus_in() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-refaus-inbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_refaus_out() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-vdv-xsd2017-refaus-outbound
    helm uninstall $(pwd | sed "s#.*/##")
}
rm_docker_webservice() {
    cd /mnt/c/devsbb/workspaces/KI_KIHUB/kihub-test-app/kihub-test-wrapper-webservice
    helm uninstall $(pwd | sed "s#.*/##")
}



rm_RT_jobs() {
    rm_docker_e2e_2015_dbris
    rm_docker_e2e_2017
    rm_docker_e2e_siri_kihub
    rm_docker_kihub
    rm_docker_siri
    rm_docker_dbris
    rm_docker_ans_in
    rm_docker_ans_out
    rm_docker_aus_in
    rm_docker_aus_out
    rm_docker_dfi_in
    rm_docker_dfi_out
    rm_docker_refaus_in
    rm_docker_refaus_out
    rm_docker_webservice
}

# Tekton-Lint aka 'tl'
tl() {
    if [ $# -lt 1 ] ; then
	echo "You need to pass path to chart"
    else
        docker run \
	    -it \
	    --rm \
	    -v $(pwd):/workspace --workdir=/workspace \
	    esta.docker.bin.sbb.ch/esta/esta-tekton-pipeline-helm-builderimage:latest \
	    python3 /opt/scripts/validate.py --verbose --no-kube-linter --no-kubeconform $1
    fi
}

yaml_lint() {
    if [ $# -lt 2 ] ; then
	echo "You need to pass configuration file and helm template"
    else
	docker run \
	    --rm \
	    --name yaml-linter \
	    -it \
	    -v $(pwd)/$1:/.yamllint.yaml \
	    -v $(pwd)/$2:/dirToCheck \
	    remote.docker.bin.sbb.ch/cytopia/yamllint:latest \
		-c /.yamllint.yaml /dirToCheck
    fi
}

kube_lint() {
    if [ $# -lt 2 ] ; then
	echo "You need to pass configuration file and helm template"
    else
	docker run \
	    --rm \
	    --name kube-linter \
	    -it \
	    -v $(pwd)/$1:/.kube-linter.yaml \
	    -v $(pwd)/$2:/.helm-template-output.yaml \
	    remote.docker.bin.sbb.ch/stackrox/kube-linter:v0.6.8-alpine \
		lint --config .kube-linter.yaml .helm-template-output.yaml
    fi
}

kube_score() {
    if [ $# -lt 1 ] ; then
	echo "You need to pass helm template"
    else
	docker run \
	    --rm \
	    --name kube-linter \
	    -it \
	    -v $(pwd)/$1:/.helm-template-output.yaml \
	    remote.docker.bin.sbb.ch/zegl/kube-score:v1.18.0 \
		score \
		    --ignore-test pod-networkpolicy,container-security-context,container-image-pull-policy,container-security-context-user-group-id,container-security-context-privileged,container-security-context-readonlyrootfilesystem,container-ephemeral-storage-request-and-limit,deployment-has-host-podantiaffinity,statefulset-has-host-podantiaffinity \
		    --output-format ci \
		    /.helm-template-output.yaml
    fi
}

export cusTools='kihub-aohaltdurchfahrtwatch kihub-biniwatch kihub-cusmdm-apex kihub-fahrzeugziel kihub-logtool2 kihub-sektorfilter kihub-snowflake kihub-sollgleisfilter kihub-status kihub-vmbeziehungstatus kihub-vmname kihub-zugliste'
