#!/bin/bash
if [ $(find ./ -name "cert-manager-setup*" | wc -l) -gt 0 ]
then
    rm cert-manager-setup*
fi
wget -q "https://hclcr.io/files/sofy/scripts/cert-manager-setup.sh"

#Function is used in order to use local variables.
setup_scripts() {

    if kubectl version; then
        echo ""
    else 
        echo "There are issues connecting to your kubernetes cluster. Please resolve these before proceeding with the script."
        return
    fi
    
    if [ -z "$HARBOR_USERNAME" ] && [ -z "$HARBOR_PASSWORD" ]; then 
        echo "You will now be asked to enter Harbor credentials:"
    fi

    if [ -z "$HARBOR_USERNAME" ]; then
        echo "Please enter Harbor username"
        local HARBOR_USERNAME=""
        read -p "Username: " HARBOR_USERNAME
        if [ -z "${HARBOR_USERNAME}" ]; then
            echo "Sorry, but a username is required. Please re-run the script and try again"
            return
        fi
    fi

    if [ -z "$HARBOR_PASSWORD" ]; then
        echo "Please enter Harbor CLI secret:"
        local HARBOR_PASSWORD=""
        read -sp "Password: " HARBOR_PASSWORD
        if [ -z "${HARBOR_PASSWORD}" ]; then
            echo "Sorry, but a CLI secret is required. Please re-run the script and try again"
            return
        fi
    fi

    if [ -z "$NAMESPACE" ]; then
        echo "Please enter desired namespace name. Or you can just press enter and the default namespace will be used"
        local NAMESPACE=""
        read -p "Namespace: " NAMESPACE
        if [ -z "${NAMESPACE}" ]; then
            NAMESPACE="default"
        fi
    fi

    if [ $(kubectl get namespace -A 2>&1 | grep -c $NAMESPACE) -eq 0 ]; then
        kubectl create namespace $NAMESPACE
    else 
        echo "Namespace $NAMESPACE exists, not creating new namespace."
    fi

    if docker login "hclcr.io" -u $HARBOR_USERNAME -p $HARBOR_PASSWORD
    then
        if [ $(kubectl get secret -n $NAMESPACE 2>&1 | grep -c '^sandbox-secret ') -eq 0 ]; then
            kubectl create secret docker-registry sandbox-secret --docker-server=hclcr.io --docker-username=$HARBOR_USERNAME --docker-password=$HARBOR_PASSWORD --namespace=$NAMESPACE
            echo "Image pull secret: sandbox-secret created in namespace: $NAMESPACE"
        else 
            echo "Secret 'sandbox-secret' already exists in namespace $NAMESPACE. If you have errors installing charts, please delete this secret manually and then re-run the script."
        fi
        #Install Cert-manager if necessary.
        if [ $(kubectl get pods -A -l app=cert-manager 2>&1 |grep -c cert-manager) -gt 0 ]; then
            echo "Cert manager already exists. Skipping installation."
        else 
            if /bin/bash cert-manager-setup.sh; then
                echo ""
            else 
                echo "Cert-manager installation failed"
                return
            fi
        fi
		
		#Install Emissary CRDs
        if [ $(kubectl get pods -A -l app=emissary-system 2>&1 |grep -c emissary-apiext) -gt 0 ]; then
            echo "Emissary CRDs already exists. Skipping installation."
        else
           kubectl apply -f https://app.getambassador.io/yaml/emissary/2.2.2/emissary-crds.yaml
           
        fi
    else 
        echo "Sorry, your Harbor credentials are incorrect. Please re-run the script to try again."
        return
    fi

    echo "Install a helm chart to your cluster using the following command: "
    echo "helm install <your-chart-name>.tgz"
}
    
setup_scripts


