#!/bin/bash
set -e

binpath={{client_bin_dir}}
arch=linux-amd64
changed=0

# this is needed for the tools to work (even to return the version)
export KUBECONFIG={{admin_conf}}

install_tool() {
    toolpath=$1
    url=$2
    tarpath=$3
    
    echo "Downloading release from $url"
    curl -L $url > /tmp/tool.tar.gz
    tar -xvf /tmp/tool.tar.gz -C /tmp

    if [ ! -e "/tmp/$tarpath" ]; then
        echo "the /tmp/tool.tar.gz archive don't contain $tarpath. Exit"
        exit 1
    fi

    if [ -e "$toolpath" ]; then rm $toolpath; fi

    cp "/tmp/$tarpath" $toolpath
    chmod +x $toolpath
    changed=1
}

########################
## Install kubectl
toolpath=$binpath/kubectl
desired_version={{client_kubectl_version}}
installed_version=
if [ -f $toolpath ]; then

    rawversion=$($toolpath version --short | head -n1)
    
    if [[ "$rawversion" =~ ^'Client Version: '(.+)$ ]]; then 
        installed_version=${BASH_REMATCH[1]}; 
    else
        echo "$toolpath version unexpected output"
        exit 1
    fi
fi

if [ "$desired_version" != "$installed_version" ]; then
    install_tool $toolpath "https://dl.k8s.io/$desired_version/kubernetes-client-$arch.tar.gz" kubernetes/client/bin/kubectl
fi

echo "kubectl $desired_version available at $toolpath "

########################
## Install helm3
toolpath=$binpath/helm3
desired_version={{client_helm_version}}
installed_version=
if [ -f $toolpath ]; then

    rawversion=$($toolpath version --short)
    installed_version=
    
    if [[ "$rawversion" =~ (.+)(\+) ]]; then 
        installed_version=${BASH_REMATCH[1]}; 
    else
        echo "$toolpath version unexpected output"
        exit 1
    fi
fi

if [ "$desired_version" != "$installed_version" ]; then
    install_tool $toolpath "https://get.helm.sh/helm-$desired_version-linux-amd64.tar.gz" $arch/helm
fi

echo "helm3 $desired_version available at $toolpath "

{% if trident.enabled %}
########################
## Install tridentctl
toolpath=$binpath/tridentctl
desired_version={{trident.version}}
if [ "$desired_version" != "" ]; then
    installed_version=
    if [ -f $toolpath ]; then

        rawversion=$($toolpath version --client -o yaml)
        installed_version=
        
        if [[ "$rawversion" =~ 'version: '(.+)$ ]]; then 
            installed_version=${BASH_REMATCH[1]}; 
        else
            echo "$toolpath version unexpected output"
            exit 1
        fi
    fi

    if [ "$desired_version" != "$installed_version" ]; then
        url=https://github.com/NetApp/trident/releases/download/v$desired_version/trident-installer-$desired_version.tar.gz
        echo "Downloading release from $url to /tmp/trident-installer.tar.gz"
        curl -L $url > /tmp/trident-installer.tar.gz

        echo "Extract to {{local_path}}/trident-installer"
        tar -xvf /tmp/trident-installer.tar.gz -C {{local_path}}

        if [ ! -e "{{local_path}}/trident-installer/tridentctl" ]; then
            echo "cannot find {{local_path}}/trident-installer/tridentctl. Exit"
            exit 1
        fi

        if [ -e "$toolpath" ]; then rm $toolpath; fi

        echo "symlinking {{local_path}}/trident-installer/tridentctl to $toolpath"
        ln -s -T {{local_path}}/trident-installer/tridentctl $toolpath
        changed=1
    fi

    echo "tridentctl $desired_version available at $toolpath "

fi
{% endif %}

if [ $changed == "1" ]; then
    exit 0
else
    exit 255
fi
