apiVersion: v1
kind: ConfigMap
metadata:
  name: helm-shell-script
data:
  helm.sh: |
    #!/bin/sh

    set -e

    # $1 - Reliza Instance API Id
    # $2 - Reliza Instance API Key
    # $3 - Project Id of Helm Chart Project on Reliza Hub
    # $4 - Name/path of values file to use, relative to the base of the helm chart
    # $5 - Helm release name
    # $6 - Namespace to install release
    
    if [ "$#" -lt 6 ]
    then
      echo "Usage: reliza_instance_api_id reliza_instance_api_id helm_chart_project_id name_path_of_values_file helm_release_name namespace"
      exit 1
    fi
    
    mkdir -p work-values
    
    
    # Obtain latest approved version of helm chart
    reliza_hub_latestrelease=$(reliza-cli getlatestrelease -i $1 -k $2 --project $3)

    helm_chart_version=$(echo $reliza_hub_latestrelease | jq -r ".version")
    helm_chart_identifier=$(echo $reliza_hub_latestrelease | jq -r ".artifactDetails[0].identifier")
    helm_chart_name=$(echo $helm_chart_identifier | awk -F "/" '{print $NF}')
    helm_chart_uri=$(echo $helm_chart_identifier | awk -F "/$helm_chart_name" '{print $1}')
    
    helm repo add repo $helm_chart_uri
    helm repo update
    
    # compare version with the one recorded in work-values if any
    last_version=$(cat work-values/last_version || echo "none")
    
    # proceed with chart pull if the version is new
    if [ "$helm_chart_version" != "$last_version" ]
    then
      rm -rf work-values/$helm_chart_name*
      helm pull repo/$helm_chart_name --version $helm_chart_version -d work-values/
      cd work-values && tar -xzvf *.tgz && cd ..
      # helm pull repo/$helm_chart_name --version $helm_chart_version --untar --untardir work-values/
    fi

    cp work-values/values.yaml work-values/values-prev.yaml || echo "no prev values file present yet" > work-values/values-prev.yaml
    reliza-cli replacetags -i $1 -k $2 --infile work-values/$helm_chart_name/$4 --outfile work-values/values.yaml

    values_diff=$(diff work-values/values.yaml work-values/values-prev.yaml | wc -l)

    # check if release already deployed to figure out whether we do install or upgrade
    helm_command="upgrade"
    cur_releases=$(helm list -f "^$5$" -n $6 | wc -l)
    if [ $cur_releases -lt 2 ]
    then
      helm_command="install"
    fi

    if [ $values_diff -gt 0 ] || [ "install" == "$helm_command" ] || [ "$helm_chart_version" != "$last_version" ]
    then
      echo "Update required, performing..."
      helm $helm_command $5 -n $6 -f work-values/values.yaml repo/$helm_chart_name
      
      # stream updated helm chart version to Reliza Hub if there was a version update
      if [ "$helm_chart_version" != "$last_version" ]
      then
        # compute sha256sum 
        sha256digest="sha256:$(sha256sum work-values/*.tgz | cut -f 1 -d ' ')"
        # stream
        reliza-cli instdata -i $1 -k $2 --images "$sha256digest" --namespace $6 --sender helmsender$6
        # save version for future use
        echo $helm_chart_version > work-values/last_version
      fi
    else
      echo "No diff in values, no upgrade will be performed"
    fi