#!/usr/bin/env sh

set -eu

if command -v helm >/dev/null ; then
    exec helm "$@"
fi

helmversion=v3.18.4
bindir=$( cd "${0%/*}" && pwd )
targetbin=$( cd "$bindir"/.. && pwd )/target/bin
helmbin=$targetbin/helm-$helmversion

if [ ! -f "$helmbin" ]; then
    if [ "$(uname -s)" = Darwin ]; then
        os=darwin
        arch=amd64
    else
        os=linux
        case $(uname -m) in
            x86_64) arch=amd64 ;;
            arm) dpkg --print-architecture | grep -q arm64 && arch=arm64 ;;
        esac
    fi
    helmcurl=https://get.helm.sh/helm-$helmversion-$os-$arch.tar.gz
    targetdir=$os-$arch
    tmp=$(mktemp -d -t helm.XXX)
    mkdir -p "$targetbin"
    (
        cd "$tmp"
        "$bindir"/scurl -o "./helm.tar.gz" "$helmcurl"
        tar zf "./helm.tar.gz" -x "$targetdir"
        chmod +x "$targetdir/helm"
    )
    mv "$tmp/$targetdir/helm" "$helmbin"
    rm -rf "$tmp"
fi

"$helmbin" "$@"
