#!/bin/bash

echo "Installing cert-manager"
#Even though this check is already done in the setup script, it's done as a safeguard just in case a user tries to run this script in isolation.
if [ $(kubectl get pods -A -l app=cert-manager 2>&1 | grep -c cert-manager) -gt 0 ]; then
    echo "Cert manager already exists. It may be a previous version. If so, it is recommended that you uninstall using 'helm delete cert-manager -n <namespace where cert-manager is installed>', wait a few minutes, and then reinstall using this script."
    exit 1
fi

#This is done in case the user installed a previous verison of cert-manager in which the CRDS were applied independently, but uninstalled using helm delete or helm uninstall because there is an error where the crds are not being uninstalled by helm in this case
#The 6 different if statements are because we don't know the state of the user's cluster, thus anywhere between none or all of these may be present, and I don't like writing functions in bash/shell scripts.
if [ $(kubectl get crd -A 2>&1 | grep -c 'certificaterequests.cert-manager.io') -gt 0 ] || [ $(kubectl get crd -A 2>&1 | grep -c 'challenges.acme.cert-manager.io') -gt 0 ] || [ $(kubectl get crd -A 2>&1 | grep -c 'clusterissuers.cert-manager.io') -gt 0 ] || [ $(kubectl get crd -A 2>&1 | grep -c 'issuers.cert-manager.io') -gt 0 ] || [ $(kubectl get crd -A 2>&1 | grep -c 'orders.acme.cert-manager.io') -gt 0 ] || [ $(kubectl get crd -A 2>&1 | grep -c 'certificates.cert-manager.io') -gt 0 ]; then
    echo "Previous crds from an earlier version installation still exist on your cluster. Would you like to delete them? (note that this is required to proceed with the installation)"
    #next two lines are to get the first character of the response and convert it to uppercase in case the user enters something like 'yes' or 'yeah'
    read -p "(Y/n)? " DELETION
    DELETION=${DELETION:0:1}
    if [ ${DELETION^^} == 'Y' ]; then
        if [ $(kubectl get crd -A 2>&1 | grep -c 'certificaterequests.cert-manager.io') -gt 0 ]; then
        kubectl delete crd 'certificaterequests.cert-manager.io'
        fi 
        if [ $(kubectl get crd -A 2>&1 | grep -c 'challenges.acme.cert-manager.io') -gt 0 ]; then
            kubectl delete crd 'challenges.acme.cert-manager.io'
        fi 
        if [ $(kubectl get crd -A 2>&1 | grep -c 'clusterissuers.cert-manager.io') -gt 0 ]; then
            kubectl delete crd 'clusterissuers.cert-manager.io'
        fi 
        if [ $(kubectl get crd -A 2>&1 | grep -c 'issuers.cert-manager.io') -gt 0 ]; then
            kubectl delete crd 'issuers.cert-manager.io'
        fi 
        if [ $(kubectl get crd -A 2>&1 | grep -c 'orders.acme.cert-manager.io') -gt 0 ]; then
            kubectl delete crd 'orders.acme.cert-manager.io'
        fi 
        if [ $(kubectl get crd -A 2>&1 | grep -c 'certificates.cert-manager.io') -gt 0 ]; then
            kubectl delete crd 'certificates.cert-manager.io'
        fi 
    else 
        echo "Sorry, but cleaning up these old crds is necessary to proceed"
        exit 1
    fi
fi




if helm repo add jetstack https://charts.jetstack.io && helm repo update; then 
    if [ $(kubectl get namespace -A 2>&1 | grep -c 'cert-manager') -eq 0 ]; then
        kubectl create namespace 'cert-manager'
    else 
        echo "Namespace 'cert-manager' exists, not creating new namespace. "
    fi

    if helm install cert-manager jetstack/cert-manager --namespace cert-manager --version v1.8.0 --set installCRDs=true; then
        echo "Install Complete!"
    else 
        echo "Error installing cert-manager."
    fi
else 
    echo "Error adding repository."
fi
