#!/bin/bash
# CUSTOM_BUILD_HARDENED
export CFLAGS="${CFLAGS:--O2} -fhardened -fPIC"
export CXXFLAGS="${CXXFLAGS:--O2} -fhardened -fPIC"
export LDFLAGS="${LDFLAGS:-} -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,ibt -Wl,-z,shstk"

# AMOS build script for OpenJDK 25, built from source.
# Hardening flags are passed via OpenJDK's --with-extra-*flags configure
# options, which propagate to both the JDK native libraries and HotSpot.

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

PRGNAM=openjdk
VERSION=${VERSION:-25.0.5+7}
BUILD=${BUILD:-1}
PKGTYPE=${PKGTYPE:-txz}

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

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD.$PKGTYPE"
  exit 0
fi

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

if [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
else
  LIBDIRSUFFIX=""
fi

# ---- Hardening flags -----------------------------------------------------
# -fhardened is the GCC 14+ umbrella; the explicit -fstack-protector-strong
# is repeated so OpenJDK's internal CFLAGS_JDK cannot override it.
HARDEN_CFLAGS="-fhardened -fstack-protector-strong -fstack-clash-protection -fcf-protection=full"
HARDEN_LDFLAGS="-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,ibt -Wl,-z,shstk"

BOOT_JDK="${BOOT_JDK:-/usr/lib64/java}"

set -e

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

cd $TMP
rm -rf jdk25u-* jdk-*

TARBALL="$CWD/jdk-${VERSION}.tar.gz"
[ -f "$TARBALL" ] || { echo "ERROR: $TARBALL not found" >&2; exit 1; }

tar xvf "$TARBALL"

# The tarball filename and the extracted top-level directory do not
# always agree. A GitHub tag archive for repo "jdk25u", tag
# "jdk-25.0.5+7", extracts to "jdk25u-jdk-25.0.5-7" -- the repo name is
# prefixed and the "+" is replaced with "-". Derive the directory from
# the archive contents rather than assuming.
SRCDIR=$(tar tf "$TARBALL" | head -n1 | cut -d/ -f1)
[ -n "$SRCDIR" ] || { echo "ERROR: cannot determine extracted top-level dir" >&2; exit 1; }
[ -d "$SRCDIR" ] || { echo "ERROR: expected $SRCDIR after extraction" >&2; exit 1; }

cd "$SRCDIR"

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

export ALSA_NOT_NEEDED=yes
export X11_NOT_NEEDED=yes
export CUPS_NOT_NEEDED=yes
export PULSE_NOT_NEEDED=yes

# ---- Configure with hardening flags --------------------------------------
bash configure \
  --with-boot-jdk="$BOOT_JDK" \
  --prefix=/usr/lib${LIBDIRSUFFIX}/$PRGNAM \
  --enable-unlimited-crypto \
  --with-jvm-variants=server \
  --with-debug-level=release \
  --with-vendor-name="AMOS" \
  --with-vendor-url="https://anne.media" \
  --with-vendor-bug-url="https://anne.media" \
  --with-vendor-vm-bug-url="https://anne.media" \
  --with-extra-cflags="$HARDEN_CFLAGS" \
  --with-extra-cxxflags="$HARDEN_CFLAGS" \
  --with-extra-ldflags="$HARDEN_LDFLAGS" \
  --disable-warnings-as-errors \
  --enable-headless-only \
  --with-freetype=bundled \
  --with-harfbuzz=bundled \
  --with-libpng=bundled \
  --with-libjpeg=bundled \
  --with-giflib=bundled \
  --with-zlib=bundled

# ---- Build ----------------------------------------------------------------
make JOBS=6 images

# ---- Install into package ------------------------------------------------
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}
cp -a build/*/images/jdk $PKG/usr/lib${LIBDIRSUFFIX}/$PRGNAM
cd $PKG/usr/lib${LIBDIRSUFFIX}
ln -s $PRGNAM java

# ---- Profile scripts ------------------------------------------------------
mkdir -p $PKG/etc/profile.d
cat > $PKG/etc/profile.d/$PRGNAM.sh << EOF
#!/bin/sh
export JAVA_HOME=/usr/lib${LIBDIRSUFFIX}/java
export PATH=\${PATH}:\${JAVA_HOME}/bin
EOF
cat > $PKG/etc/profile.d/$PRGNAM.csh << EOF
#!/bin/csh
setenv JAVA_HOME /usr/lib${LIBDIRSUFFIX}/java
setenv PATH \${PATH}:\${JAVA_HOME}/bin
EOF
chmod 755 $PKG/etc/profile.d/*

# ---- slack-desc -----------------------------------------------------------
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.$PKGTYPE
