#!/bin/bash

# Slackware build script for tenv

# Copyright 2026 Andrew Clemons, Tokyo Japan
# 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=tenv
VERSION=${VERSION:-4.15.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

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

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

set -e

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

# Vendor the Go module dependencies offline.
mapfile -t GO_MODULES < <(go mod edit -json go.mod | python3 -c "
import json
import sys
for r in json.load(sys.stdin).get('Require', []):
    print(r['Path'])
")
mapfile -t GO_TARBALLS < <(grep -oE 'https?://[^ ]+\.tar\.gz' $CWD/$PRGNAM.info | sed 's#.*/##')

GO_PAIRS=()
for i in "${!GO_MODULES[@]}"; do
  GO_PAIRS+=("${GO_MODULES[$i]} ${GO_TARBALLS[$(( i + 1 ))]}")
done

mapfile -t GO_PAIRS < <(printf '%s\n' "${GO_PAIRS[@]}" | sort)

rm -rf vendor
mkdir -p vendor

for PAIR in "${GO_PAIRS[@]}"; do
  MODULE="${PAIR% *}"
  TARBALL="${PAIR##* }"

  MODTMP="$(mktemp -d)"
  tar xf $CWD/$TARBALL -C "$MODTMP"
  MODTOP="$(find "$MODTMP" -mindepth 1 -maxdepth 1 -type d | head -1)"

  MODSRC="$(grep -rlxF "module $MODULE" --include=go.mod "$MODTOP" 2>/dev/null | head -1 || true)"
  if [ -n "$MODSRC" ]; then
    MODSRC="$(dirname "$MODSRC")"
  else
    MODSRC="$MODTOP"
  fi

  find "$MODSRC" -mindepth 2 -name go.mod -printf '%h\n' | sort -ru | xargs -r rm -rf

  mkdir -p "vendor/$(dirname "$MODULE")"
  rm -rf "vendor/$MODULE"
  mv "$MODSRC" "vendor/$MODULE"

  rm -rf "$MODTMP"
done

cat > vendor/modules.txt << "EOF"
# github.com/BurntSushi/toml v1.6.0
## explicit; go 1.18
github.com/BurntSushi/toml
github.com/BurntSushi/toml/internal
# github.com/ProtonMail/go-crypto v1.4.1
## explicit; go 1.23.0
github.com/ProtonMail/go-crypto/bitcurves
github.com/ProtonMail/go-crypto/brainpool
github.com/ProtonMail/go-crypto/eax
github.com/ProtonMail/go-crypto/internal/byteutil
github.com/ProtonMail/go-crypto/ocb
github.com/ProtonMail/go-crypto/openpgp
github.com/ProtonMail/go-crypto/openpgp/aes/keywrap
github.com/ProtonMail/go-crypto/openpgp/armor
github.com/ProtonMail/go-crypto/openpgp/clearsign
github.com/ProtonMail/go-crypto/openpgp/ecdh
github.com/ProtonMail/go-crypto/openpgp/ecdsa
github.com/ProtonMail/go-crypto/openpgp/ed25519
github.com/ProtonMail/go-crypto/openpgp/ed448
github.com/ProtonMail/go-crypto/openpgp/eddsa
github.com/ProtonMail/go-crypto/openpgp/elgamal
github.com/ProtonMail/go-crypto/openpgp/errors
github.com/ProtonMail/go-crypto/openpgp/internal/algorithm
github.com/ProtonMail/go-crypto/openpgp/internal/ecc
github.com/ProtonMail/go-crypto/openpgp/internal/encoding
github.com/ProtonMail/go-crypto/openpgp/packet
github.com/ProtonMail/go-crypto/openpgp/s2k
github.com/ProtonMail/go-crypto/openpgp/x25519
github.com/ProtonMail/go-crypto/openpgp/x448
# github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f
## explicit; go 1.12
github.com/ProtonMail/go-mime
# github.com/ProtonMail/gopenpgp/v2 v2.10.0
## explicit; go 1.23.0
github.com/ProtonMail/gopenpgp/v2/armor
github.com/ProtonMail/gopenpgp/v2/constants
github.com/ProtonMail/gopenpgp/v2/crypto
github.com/ProtonMail/gopenpgp/v2/internal
# github.com/PuerkitoBio/goquery v1.12.0
## explicit; go 1.25.0
github.com/PuerkitoBio/goquery
# github.com/agext/levenshtein v1.2.3
## explicit
github.com/agext/levenshtein
# github.com/andybalholm/cascadia v1.3.3
## explicit; go 1.16
github.com/andybalholm/cascadia
# github.com/apparentlymart/go-textseg/v15 v15.0.0
## explicit; go 1.16
github.com/apparentlymart/go-textseg/v15/textseg
# github.com/atotto/clipboard v0.1.4
## explicit
github.com/atotto/clipboard
# github.com/aymanbagabas/go-osc52/v2 v2.0.1
## explicit; go 1.16
github.com/aymanbagabas/go-osc52/v2
# github.com/bradleyfalzon/ghinstallation/v2 v2.19.0
## explicit; go 1.25.0
github.com/bradleyfalzon/ghinstallation/v2
# github.com/charmbracelet/bubbles v1.0.0
## explicit; go 1.24.2
github.com/charmbracelet/bubbles/cursor
github.com/charmbracelet/bubbles/help
github.com/charmbracelet/bubbles/key
github.com/charmbracelet/bubbles/list
github.com/charmbracelet/bubbles/paginator
github.com/charmbracelet/bubbles/runeutil
github.com/charmbracelet/bubbles/spinner
github.com/charmbracelet/bubbles/textinput
# github.com/charmbracelet/bubbletea v1.3.10
## explicit; go 1.24.0
github.com/charmbracelet/bubbletea
# github.com/charmbracelet/colorprofile v0.4.1
## explicit; go 1.24.2
github.com/charmbracelet/colorprofile
# github.com/charmbracelet/lipgloss v1.1.0
## explicit; go 1.18
github.com/charmbracelet/lipgloss
# github.com/charmbracelet/x/ansi v0.11.6
## explicit; go 1.24.2
github.com/charmbracelet/x/ansi
github.com/charmbracelet/x/ansi/parser
# github.com/charmbracelet/x/cellbuf v0.0.15
## explicit; go 1.24.2
github.com/charmbracelet/x/cellbuf
# github.com/charmbracelet/x/term v0.2.2
## explicit; go 1.24.0
github.com/charmbracelet/x/term
# github.com/clipperhouse/displaywidth v0.9.0
## explicit; go 1.18
github.com/clipperhouse/displaywidth
# github.com/clipperhouse/stringish v0.1.1
## explicit; go 1.18
github.com/clipperhouse/stringish
# github.com/clipperhouse/uax29/v2 v2.5.0
## explicit; go 1.18
github.com/clipperhouse/uax29/v2/graphemes
# github.com/cloudflare/circl v1.6.3
## explicit; go 1.22.0
github.com/cloudflare/circl/dh/x25519
github.com/cloudflare/circl/dh/x448
github.com/cloudflare/circl/ecc/goldilocks
github.com/cloudflare/circl/internal/conv
github.com/cloudflare/circl/internal/sha3
github.com/cloudflare/circl/math
github.com/cloudflare/circl/math/fp25519
github.com/cloudflare/circl/math/fp448
github.com/cloudflare/circl/math/mlsbset
github.com/cloudflare/circl/sign
github.com/cloudflare/circl/sign/ed25519
github.com/cloudflare/circl/sign/ed448
# github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
## explicit
github.com/davecgh/go-spew/spew
# github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f
## explicit; go 1.16
github.com/erikgeiser/coninput
# github.com/fatih/color v1.19.0
## explicit; go 1.25.0
github.com/fatih/color
# github.com/go-test/deep v1.1.0
## explicit; go 1.16
# github.com/golang-jwt/jwt/v4 v4.5.2
## explicit; go 1.16
github.com/golang-jwt/jwt/v4
# github.com/google/go-github/v88 v88.0.0
## explicit; go 1.25.0
github.com/google/go-github/v88/github
# github.com/google/go-querystring v1.2.0
## explicit; go 1.13
github.com/google/go-querystring/query
# github.com/hashicorp/go-hclog v1.6.3
## explicit; go 1.13
github.com/hashicorp/go-hclog
# github.com/hashicorp/go-version v1.9.0
## explicit
github.com/hashicorp/go-version
# github.com/hashicorp/hcl/v2 v2.24.0
## explicit; go 1.23.0
github.com/hashicorp/hcl/v2
github.com/hashicorp/hcl/v2/ext/customdecode
github.com/hashicorp/hcl/v2/hclparse
github.com/hashicorp/hcl/v2/hclsyntax
github.com/hashicorp/hcl/v2/json
# github.com/inconshreveable/mousetrap v1.1.0
## explicit; go 1.18
github.com/inconshreveable/mousetrap
# github.com/kr/pretty v0.2.1
## explicit; go 1.12
# github.com/lucasb-eyer/go-colorful v1.3.0
## explicit; go 1.12
github.com/lucasb-eyer/go-colorful
# github.com/mattn/go-colorable v0.1.14
## explicit; go 1.18
github.com/mattn/go-colorable
# github.com/mattn/go-isatty v0.0.20
## explicit; go 1.15
github.com/mattn/go-isatty
# github.com/mattn/go-localereader v0.0.1
## explicit
github.com/mattn/go-localereader
# github.com/mattn/go-runewidth v0.0.19
## explicit; go 1.20
github.com/mattn/go-runewidth
# github.com/mitchellh/go-wordwrap v1.0.1
## explicit; go 1.14
github.com/mitchellh/go-wordwrap
# github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6
## explicit; go 1.17
github.com/muesli/ansi
github.com/muesli/ansi/compressor
# github.com/muesli/cancelreader v0.2.2
## explicit; go 1.17
github.com/muesli/cancelreader
# github.com/muesli/termenv v0.16.0
## explicit; go 1.17
github.com/muesli/termenv
# github.com/pkg/errors v0.9.1
## explicit
github.com/pkg/errors
# github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
## explicit
github.com/pmezard/go-difflib/difflib
# github.com/rivo/uniseg v0.4.7
## explicit; go 1.18
github.com/rivo/uniseg
# github.com/sahilm/fuzzy v0.1.1
## explicit
github.com/sahilm/fuzzy
# github.com/spf13/cobra v1.10.2
## explicit; go 1.15
github.com/spf13/cobra
# github.com/spf13/pflag v1.0.10
## explicit; go 1.12
github.com/spf13/pflag
# github.com/stretchr/testify v1.11.1
## explicit; go 1.17
github.com/stretchr/testify/assert
github.com/stretchr/testify/assert/yaml
github.com/stretchr/testify/require
# github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e
## explicit; go 1.19
github.com/xo/terminfo
# github.com/zclconf/go-cty v1.18.1
## explicit; go 1.25
github.com/zclconf/go-cty/cty
github.com/zclconf/go-cty/cty/convert
github.com/zclconf/go-cty/cty/ctymarks
github.com/zclconf/go-cty/cty/ctystrings
github.com/zclconf/go-cty/cty/function
github.com/zclconf/go-cty/cty/function/stdlib
github.com/zclconf/go-cty/cty/gocty
github.com/zclconf/go-cty/cty/json
github.com/zclconf/go-cty/cty/set
# golang.org/x/crypto v0.49.0
## explicit; go 1.25.0
golang.org/x/crypto/argon2
golang.org/x/crypto/blake2b
golang.org/x/crypto/cast5
golang.org/x/crypto/cryptobyte
golang.org/x/crypto/cryptobyte/asn1
golang.org/x/crypto/hkdf
golang.org/x/crypto/sha3
# golang.org/x/mod v0.33.0
## explicit; go 1.24.0
golang.org/x/mod/semver
# golang.org/x/net v0.52.0
## explicit; go 1.25.0
golang.org/x/net/html
golang.org/x/net/html/atom
# golang.org/x/sync v0.20.0
## explicit; go 1.25.0
golang.org/x/sync/errgroup
# golang.org/x/sys v0.42.0
## explicit; go 1.25.0
golang.org/x/sys/cpu
golang.org/x/sys/unix
golang.org/x/sys/windows
# golang.org/x/text v0.35.0
## explicit; go 1.25.0
golang.org/x/text/encoding
golang.org/x/text/encoding/charmap
golang.org/x/text/encoding/htmlindex
golang.org/x/text/encoding/internal
golang.org/x/text/encoding/internal/identifier
golang.org/x/text/encoding/japanese
golang.org/x/text/encoding/korean
golang.org/x/text/encoding/simplifiedchinese
golang.org/x/text/encoding/traditionalchinese
golang.org/x/text/encoding/unicode
golang.org/x/text/internal/language
golang.org/x/text/internal/language/compact
golang.org/x/text/internal/tag
golang.org/x/text/internal/utf8internal
golang.org/x/text/language
golang.org/x/text/runes
golang.org/x/text/transform
golang.org/x/text/unicode/norm
# golang.org/x/tools v0.42.0
## explicit; go 1.24.0
golang.org/x/tools/cmd/stringer
golang.org/x/tools/go/ast/edge
golang.org/x/tools/go/ast/inspector
golang.org/x/tools/go/gcexportdata
golang.org/x/tools/go/packages
golang.org/x/tools/go/types/objectpath
golang.org/x/tools/go/types/typeutil
golang.org/x/tools/internal/aliases
golang.org/x/tools/internal/event
golang.org/x/tools/internal/event/core
golang.org/x/tools/internal/event/keys
golang.org/x/tools/internal/event/label
golang.org/x/tools/internal/gcimporter
golang.org/x/tools/internal/gocommand
golang.org/x/tools/internal/packagesinternal
golang.org/x/tools/internal/pkgbits
golang.org/x/tools/internal/stdlib
golang.org/x/tools/internal/typeparams
golang.org/x/tools/internal/typesinternal
golang.org/x/tools/internal/versions
# gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127
## explicit
# gopkg.in/yaml.v3 v3.0.1
## explicit
gopkg.in/yaml.v3
EOF

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

for prg in tenv terraform terragrunt terramate tf tofu atmos ; do
  go build -mod=vendor -ldflags "-s -w -X main.version=v$VERSION" -o "build/$prg" "./cmd/$prg"
done
install -Dm755 -t "$PKG/usr/bin" build/*

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

install -D -m644 <($PKG/usr/bin/tenv completion bash) $PKG/usr/share/bash-completion/completions/$PRGNAM
install -D -m644 <($PKG/usr/bin/tenv completion fish) $PKG/usr/share/fish/vendor_completions.d/$PRGNAM.fish
install -D -m644 <($PKG/usr/bin/tenv completion zsh) $PKG/usr/share/zsh/site-functions/_$PRGNAM

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md LICENSE README.md SECURITY.md TENV_AS_LIB.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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