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

# nodejs.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2022-2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Thanks to Audrius Kažukauskas, Ryan P.C. McQuen, and Willy Sudiarto Raharjo
# for the slackbuilds.org version of this script.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, with the following exception:
# the text of the GPL license may be omitted.
#
# You may have received a copy of the GNU General Public License
# along with this program (most likely, a file named COPYING). If
# not, see <https://www.gnu.org/licenses/>.

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

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

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/node-?$VERSION.tar.!(*sign|*asc|*sig)
cd node*/ || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

## Fix libdir for 64-bit:
sed -i "s|lib/|lib${LIBDIRSUFFIX}/|g" tools/install.py
sed -i "s|'lib'|'lib${LIBDIRSUFFIX}'|g" lib/module.js
sed -i "s|'lib'|'lib${LIBDIRSUFFIX}'|g" deps/npm/lib/npm.js

# Choose a compiler (gcc/g++ or clang/clang++):
export CC=${CC:-gcc}
export CXX=${CXX:-g++}

# Not sure if this one is really needed.
#sed -i "s|math.h|cmath|" src/node_crypto.cc

# Configure:
slack_autotoolsprep
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export LDFLAGS="-Wl,--copy-dt-needed-entries"
./configure \
   --prefix=/usr \
   --with-intl=system-icu \
   --shared-openssl \
   --shared-zlib \
   --shared-libuv \
   --experimental-http-parser \
   --shared-nghttp2 \
   --shared-nghttp3 \
   --shared-cares \
   --shared-brotli || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# Relocate manual pages:
if [ -d $PKG/usr/share/man ]; then
  mv $PKG/usr/share/man $PKG/usr
  # Copy these, we'll link them later:
  rsync -lprvt $PKG/usr/lib${LIBDIRSUFFIX}/node_modules/npm/man/ $PKG/usr/man/
fi

# Compress manual pages:
find $PKG/usr/man $PKG/usr/lib${LIBDIRSUFFIX}/node_modules/npm/man -type f -exec gzip -9 {} \+
for i in $( find $PKG/usr/man -type l ) ; do
  ln -s $( readlink $i ).gz $i.gz
  rm $i
done

# Symlink npm man pages:
( cd $PKG/usr/lib${LIBDIRSUFFIX}/node_modules/npm/man
  for dir in man? ; do
    pushd $dir
      for file in *.gz ; do
        ln -sf ../../../../../man/$dir/$file .
      done
    popd
  done
)

# Relocate /usr/share/doc/node, but leave a link since the contents look like
# scripts that might be called at the default path?
if [ -d $PKG/usr/share/doc/node ]; then
  mkdir -p $PKG/usr/doc/${PKGNAM}-$VERSION
  mv -fv $PKG/usr/share/doc/node/* $PKG/usr/doc/${PKGNAM}-$VERSION
  rm -rf $PKG/usr/share/doc
  ln -vsf ${PKGNAM}-$VERSION $PKG/usr/doc/node
fi

# Remove unneeded files:
find $PKG/usr \( -name '.git*' \) -exec rm -f {} \;

# Set NODE_PATH to look for globally installed modules:
mkdir -p $PKG/etc/profile.d
cat > $PKG/etc/profile.d/${PKGNAM}.csh << EOF
#!/bin/csh
setenv NODE_PATH /usr/lib${LIBDIRSUFFIX}/node_modules
EOF
cat > $PKG/etc/profile.d/${PKGNAM}.sh << EOF
#!/bin/sh
export NODE_PATH=/usr/lib${LIBDIRSUFFIX}/node_modules
EOF
chmod 0755 $PKG/etc/profile.d/*

# Symlink bash-completion if needed:
mkdir -p $PKG/usr/share/bash-completion/completions
if [ ! -r $PKG/usr/share/bash-completion/completions/npm -a -r $PKG/usr/lib${LIBDIRSUFFIX}/node_modules/npm/lib/utils/completion.sh ]; then
   ( cd $PKG/usr/share/bash-completion/completions ; ln -sf ../../../lib${LIBDIRSUFFIX}/node_modules/npm/lib/utils/completion.sh npm )
fi

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  AUTHORS* BUILDING* CHANGELOG* CODE_OF_CONDUCT* CONTRIBUTING* GOVERNANCE* LICENSE* README* SECURITY* \
  $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
