#!/bin/bash

installHelm3() {
    echo "Installing Helm 3"
    #If a file named get_helm.sh already exists in the user's current directory, and that file does something completely different, it will be problematic as the script that is pulled will be name get_helm (1).sh
    #Therefore, said file is removed, along with other copies of the file, should this be a second run.
    if [ $(find ./ -name "get_helm*" | wc -l) -gt 0 ]
    then
        rm get_helm*
    fi
    if ! curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3; then
        echo "Issue with downloading get-helm-3. Please check your internet connection. If this is working properly, the repository for the script is down, so please try again later."
        return
    fi
    sudo chmod 700 get_helm.sh
    # The get_helm.sh script itself checks for the install dir being in the right path. 
    if ! ./get_helm.sh; then
        echo "Error installing most recent version of Helm 3"
        return
    fi
    echo "Warnings regarding the 'stable' repository or the helm push plugin can be ignored. These issues do not affect helm function."
    echo "However, if you would like to remove said warnings, you can remove one of these duplicate plugin directories at the above mentioned location with 'rm -rf' and/or add the helm 'stable' repo using the command given in the warning."
    if ! helm version; then
        echo "Error running the helm verison command, please check the permissions of the default install location /usr/local/bin"
    fi
}

installHelm3