#!/bin/bash
#
# Slackware build script for helm
#
# Copyright 2026 r1w1s1
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR  AS IS  AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=helm
VERSION=${VERSION:-4.3.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
ARCH=${ARCH:-$(uname -m)}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    x86_64) ARCH=x86_64 ;;
         *) ARCH=unsupported ;;
  esac
fi

if [ "$ARCH" != "x86_64" ]; then
  echo "helm does not support the $ARCH architecture."
  exit 1
fi

if [[ -n ${PRINT_PACKAGE_NAME:-} ]]; then
  echo $PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
  exit 0
fi

set -e

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
tar xf $CWD/$PRGNAM-$VERSION-vendor.tar.gz

chown -R root:root .
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +

# Do not download modules or a newer Go toolchain during the build.
export GOTOOLCHAIN=local
export GOPROXY=off
export GOSUMDB=off
export GOWORK=off
export GOPATH=$PWD/.gopath
export GOCACHE=$PWD/.gocache
export GOFLAGS=-mod=vendor
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
export GOAMD64=v1

go build -v -trimpath -buildvcs=false \
  -ldflags='-s -w -X helm.sh/helm/v4/internal/version.version=v'$VERSION \
  -o helm ./cmd/helm

[[ $(./helm version --template '{{.Version}}') = v$VERSION ]]

install -D -m 0755 helm $PKG/usr/bin/helm
mkdir -p $PKG/usr/share/bash-completion/completions \
         $PKG/usr/share/zsh/site-functions \
         $PKG/usr/share/fish/vendor_completions.d
         ./helm completion bash > $PKG/usr/share/bash-completion/completions/helm
         ./helm completion zsh > $PKG/usr/share/zsh/site-functions/_helm
         ./helm completion fish > $PKG/usr/share/fish/vendor_completions.d/helm.fish
         chmod 644 $PKG/usr/share/bash-completion/completions/helm \
         $PKG/usr/share/zsh/site-functions/_helm \
         $PKG/usr/share/fish/vendor_completions.d/helm.fish

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE README.md $PKG/usr/doc/$PRGNAM-$VERSION
install -m 0644 $CWD/$PRGNAM.SlackBuild \
  $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

install -D -m 0644 $CWD/slack-desc $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
