#!/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 "$ARTIFACTORY_USERNAME" ] && [ -z "$ARTIFACTORY_PASSWORD" ]; then 
        echo "You will now be asked to enter Artifactory credentials:"
    fi

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

    if [ -z "$ARTIFACTORY_PASSWORD" ]; then
        echo "Please enter Artifactory password:"
        local ARTIFACTORY_PASSWORD=""
        read -sp "Password: " ARTIFACTORY_PASSWORD
        if [ -z "${ARTIFACTORY_PASSWORD}" ]; then
            echo "Sorry, but a password 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 "hclproducts-sofy-docker.jfrog.io" -u $ARTIFACTORY_USERNAME -p $ARTIFACTORY_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=hclproducts-sofy-docker.jfrog.io --docker-username=$ARTIFACTORY_USERNAME --docker-password=$ARTIFACTORY_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
    else 
        echo "Sorry, your Artifactory 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
    



