#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# google-go-lang.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Written by Eric Schultz eric at schultzter.ca
# Taken over by Vincent Batts vbatts@hashbangbash.com
# Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018  Vincent Batts
# Copyright (c) 2012 Eric Schultz
# Taken over by Patrick Volkerding <volkerdi@slackware.com>
# Copyright 2025  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Temporary build locations:
shm_tmp # Use /dev/shm if >8GB RAM is available & not mounted 'noexec'
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Architecture name for AArch64:
case $ARCH in
   aarch64) GOARCH="arm64" ;;
esac

# If we already have a Go compiler installed, use it to compile Go:
if [ -d "$(/bin/ls -dt /usr/lib${LIBDIRSUFFIX}/go*/go 2> /dev/null | head -n 1)" ]; then
  export GOROOT_BOOTSTRAP="$(/bin/ls -dt /usr/lib${LIBDIRSUFFIX}/go*/go 2> /dev/null | head -n 1)"
else # we need to use a binary bootstrap:
  if [ ! -r "$(/bin/ls -t $CWD/go*.linux-${GOARCH}.tar.gz 2> /dev/null | head -n 1)" ]; then
    echo "Downloading bootstrap binary.."
    # We need to download a binary bootstrap:
    ( cd $CWD ; ./fetch-google-go-lang-binary-bootstrap.sh || exit 1) || exit 1
  fi
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/go-bootstrap
  ( cd $PKG/usr/lib${LIBDIRSUFFIX}/go-bootstrap
    echo "Installing binary bootstrap $(/bin/ls -t $CWD/go*.linux-${GOARCH}.tar.gz | head -n 1)..."
    tar xf $(/bin/ls -t $CWD/go*.linux-${GOARCH}.tar.gz | head -n 1) || exit 1
  ) || exit 1
  export GOROOT_BOOTSTRAP="$PKG/usr/lib${LIBDIRSUFFIX}/go-bootstrap/go"
fi

# Extract source:
echo "Extracting Go source.."
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/go${VERSION}
cd $PKG/usr/lib${LIBDIRSUFFIX}/go${VERSION}
tar xf $CWD/go${VERSION}.src.tar.?z || failextract
slackhousekeeping
cd go

# Apply patches:
# It wants ld.gold on aarch64, which we no longer provide:
#auto_apply_patch $PORTCWD/sources/0006-Default-to-ld.bfd-on-ARM64.patch || failpatch

# Switch to "mold" linker, as Slackware no longer used gold.
grep -Flr -- '-fuse=gold' . | xargs sed -i 's?-fuse=gold?-fuse=mold?g'
pwd

# as of >= go1.5, the cross-compiler does not need to be bootstrap, so the
# compiler need only be compiled for the host OS, which it detects
unset GOBIN GOPATH GOOS GOARCH

# Not sure why, but this causes run.bash to fail --rworkman
unset BASH_ENV

# Default GOROOT is the parent directory of all.bash,
# which is the current directory we're in.
# Defining it here means we can use it below when building
# shared libraries
GOROOT="$PWD"
export GOROOT

# The value of GOROOT once the package is installed
export GOROOT_FINAL="/usr/lib${LIBDIRSUFFIX}/go${VERSION}/go"

cd src

if [[ "${RUN_TEST}" = "true" ]] ; then
  LC_ALL=C \
    ./all.bash || failmake
else
  LC_ALL=C \
    ./make.bash || failmake
fi

# As of go1.5, golang supports shared libraries, so generate them for use.
$GOROOT/bin/go install -buildmode=shared std || failinstall

# Ensure libraries have the proper permissions:
find $PKG/$GOROOT_FINAL -type f -name "*.so" -exec chmod +x {} \; 2> /dev/null || true

cd ..

# remove Go build cache:
rm -rf pkg/obj/go-build/*

# remove the doc Makefile:
rm -f doc/Makefile

# remove tests, nothing needs these at runtime:
find $PKG/$GOROOT_FINAL/src -type d -name "testdata" -exec rm -rf {} +
find $PKG/$GOROOT_FINAL/src -type f -name "*_test.go" -exec rm -rf {} +

mkdir -p $PKG$GOROOT_FINAL

# remove binary bootstrap (if it exists):
rm -rf $PKG/usr/lib$LIBDIRSUFFIX/go-bootstrap

# Create the profile scripts for setting PATH and env variables:
mkdir -p $PKG/etc/profile.d
cat > $PKG/etc/profile.d/go.csh << EOF
#!/bin/csh
setenv GOROOT ${GOROOT_FINAL}
setenv PATH \${GOROOT}/bin:\${PATH}
EOF

cat > $PKG/etc/profile.d/go.sh << EOF
#!/bin/sh
export GOROOT="${GOROOT_FINAL}"
export PATH="\${GOROOT}/bin:\${PATH}"
EOF

chmod 755 $PKG/etc/profile.d/go.csh
chmod 755 $PKG/etc/profile.d/go.sh

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  CONTRIBUTING* LICENSE* PATENTS* README* SECURITY* VERSION* \
  $PKG/usr/doc/$PKGNAM-$VERSION
#changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
