#!/bin/bash
# CUSTOM_BUILD_HARDENED
export LDFLAGS="${LDFLAGS:-} -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,ibt -Wl,-z,shstk"
# CUSTOM_BUILD_NUMJOBS_PATCHED

# Copyright 2026 ANNE Media
# 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=monero
VERSION=${VERSION:-0.18.5.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_amos}

PKGTYPE=${PKGTYPE:-txz}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i686 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

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

NUMJOBS=${NUMJOBS:-"-j 6"}

if [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686 -fPIC -fhardened"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -march=x86-64 -mtune=generic -fPIC -fhardened"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2 -fPIC -fhardened"
  LIBDIRSUFFIX=""
fi


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

rm -rf $PKG
mkdir -p $TMP $PKG

cd $TMP

if [ -n "${MONERO_REUSE_CHECKOUT:-}" ] && [ -d "$PRGNAM-$VERSION" ]; then
    echo ">>> Reusing existing checkout at $TMP/$PRGNAM-$VERSION (MONERO_REUSE_CHECKOUT set)"
else
    echo ">>> Cloning $PRGNAM v$VERSION (recursive, shallow)..."
    rm -rf "$PRGNAM-$VERSION"
    git clone \
      --depth 1 \
      --branch "v${VERSION}" \
      --recurse-submodules \
      --shallow-submodules \
      "https://github.com/monero-project/monero.git" \
      "$PRGNAM-$VERSION" || exit 1
fi

cd "$PRGNAM-$VERSION" || exit 1

# Drop git metadata.
rm -rf .git

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 {} \+

# ---- Submodule sanity check ------------------------------------------------
# The clone should have populated every external/ entry the tag declares.
# If any is empty, something failed silently — fail fast before CMake
# wastes half an hour.
missing=""
for d in external/randomx external/rapidjson external/supercop external/trezor-common; do
    if [ ! -d "$d" ] || [ -z "$(ls -A "$d" 2>/dev/null)" ]; then
        missing="$missing $d"
    fi
done
if [ -n "$missing" ]; then
    echo "ERROR: submodule directories are empty after clone:" >&2
    for d in $missing; do echo "  $d" >&2; done
    exit 1
fi
echo ">>> Submodules present."

# ---- CMake configure -------------------------------------------------------
mkdir -p build
cd build

cmake \
  -DCMAKE_INSTALL_PREFIX=/usr \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_GUI_DEPS=OFF \
  -DUSE_DEVICE_TREZOR=OFF \
  -DBUILD_TESTS=OFF \
  -DBUILD_DOCUMENTATION=OFF \
  -DUSE_CCACHE=OFF \
  -DSTRIP_TARGETS=OFF \
  -DCMAKE_C_FLAGS="$SLKCFLAGS" \
  -DCMAKE_CXX_FLAGS="$SLKCFLAGS" \
  -DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS" \
  -DCMAKE_SHARED_LINKER_FLAGS="$LDFLAGS" \
  -DCMAKE_MODULE_LINKER_FLAGS="$LDFLAGS" \
  .. || exit 1

# ---- Build -----------------------------------------------------------------
# Monero is large. Allow 2 GB of RAM per parallel job. If the build dies
# with an OOM kill or an internal compiler error, reduce NUMJOBS.
make $NUMJOBS || exit 1

# ---- Install ---------------------------------------------------------------
make DESTDIR=$PKG install || exit 1

cd ..

# ---- Strip binaries --------------------------------------------------------
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

# ---- Man pages -------------------------------------------------------------
if [ -d $PKG/usr/share/man ]; then
  mv $PKG/usr/share/man $PKG/usr/man
fi
if [ -d $PKG/usr/man ]; then
  find $PKG/usr/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
fi

# ---- Documentation ---------------------------------------------------------
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  LICENSE README.md docs/ANONYMITY_NETWORKS.md docs/BOUNTY.md \
  docs/DEVELOPMENT.md docs/RELEASE_CHECKLIST.md docs/ZMQ.md \
  $PKG/usr/doc/$PRGNAM-$VERSION 2>/dev/null || true
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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

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