#!/bin/bash

# Slackware build script to repackage JDK binaries

# Copyright 2026  Geoff Lewis, Victoria, AU
#
# 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, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if [ $(id -u) -ne 0 ]; then
    echo "Must be root in order to run this SlackBuild."
    exit 1
fi

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

BUILD=${BUILD:-1}

# Ensure TMP isn't a relative path.
TMP=${TMP:-/tmp}
TMP=$(realpath $TMP)

PKG=$TMP/package-java

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

# If a Java archive was given as an argument to this script, use it.
# Otherwise, we'll use an archive found in the current directory if
# there is exactly one such archive.
if [ ! -z "$1" ]; then
  if [ -r "$(readlink -f $1)" ]; then
    SOURCETGZ="$(readlink -f $1)"
  else
    echo "JDK distribution $1 was not found."
    exit 1
  fi
else
  for file in $CWD/*jdk-*.tar.gz ; do
    if [ -n "$SOURCETGZ" ]; then
      ERR="Multiple JDK distributions found!"
      break
    elif [ -r $file ]; then
      SOURCETGZ=$file
    fi
  done
  if [ -z "$SOURCETGZ" ]; then
    ERR="No JDK distribution found!"
  fi
  if [ -n "$ERR" ]; then
    echo "$ERR"
    echo
    echo "Please make sure there is exactly one JDK archive in this"
    echo "directory and then run this script again, or else specify a Java"
    echo "archive on the command line like this:"
    echo
    echo "  ./java.SlackBuild /path/to/jdk-25_linux-x64_bin.tar.gz"
    echo
    exit 1
  fi
fi

# Oracle  -> jdk
# OpenJDK -> openjdk
PKGNAM=$(basename $SOURCETGZ | cut -f 1 -d -)

if [ "$PKGNAM" = "jdk" ]; then
    # Oracle JDK
    # Get the actual version from the directory name of the first file
    # in the tar ball.
    VERSION=$(tar tf $SOURCETGZ | head -1 | cut -f 1 -d / | cut -f 2 -d -)
    JAVA_ARCH=$(basename $SOURCETGZ | cut -f 3 -d - | cut -f 1 -d _)
elif [ "$PKGNAM" = "openjdk" ]; then
    # OpenJDK
    # Get the version from the filename.
    # openjdk-<version>_linux-<arch>_bin.tar.gz
    VERSION=$(basename $SOURCETGZ | cut -f 2 -d - | cut -f 1 -d _)
    JAVA_ARCH=$(basename $SOURCETGZ | cut -f 3 -d - | cut -f 1 -d _)
else
    echo "Unexpected package name: $PKGNAM"
    echo "Package name must be 'jdk' or 'openjdk'"
    exit 1
fi

if [ "$JAVA_ARCH" = "x64" ]; then
  ARCH=x86_64
  LIBDIRSUFFIX="64"
elif [ "$JAVA_ARCH" = "aarch64" ]; then
  ARCH=aarch64
  LIBDIRSUFFIX="64"
else
  echo "Oracle & OpenJDK only provide x64 and aarch64 Linux archives"
  exit 1
fi

set -e

cd $PKG
mkdir -p usr/lib${LIBDIRSUFFIX}
cd usr/lib${LIBDIRSUFFIX}
tar xvf $SOURCETGZ || exit 1

# All variants unpack to the directory named "jdk-<version>".
# Replace possible version underscore with plus.
JDKDIR=jdk-${VERSION/_/+}

# Sanity check before continuing.
if [ ! -d "$JDKDIR" ]; then
    echo "Expected ${SOURCETGZ##*/} to unpack to $JDKDIR"
    exit 1
fi

# Create /usr/lib64/java symlink to $JDKDIR for use as JAVA_HOME.
ln -s $JDKDIR java

## In $PKG/usr/lib64
chown -R root:root $JDKDIR
find -L $JDKDIR \
  \( -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 {} \;

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

## In $PKG/usr/lib64
find $JDKDIR/man -type f -exec gzip -9q {} \; 2> /dev/null || true

## In $PKG/usr/lib64
ln -sf $JDKDIR/lib/libjawt.so
ln -sf $JDKDIR/lib/server/libjvm.so
ln -sf $JDKDIR/lib/libjava.so
ln -sf $JDKDIR/lib/libawt.so
ln -sf $JDKDIR/lib/libawt_xawt.so
ln -sf $JDKDIR/lib/libverify.so

cd $PKG

## In $PKG
# /usr/lib64/java/man is a default path in /etc/man_db.conf so we shouldn't
# need to set MANPATH.
mkdir -p etc/profile.d
cat > etc/profile.d/$PKGNAM.sh << EOF
#!/bin/sh
export JAVA_HOME=/usr/lib${LIBDIRSUFFIX}/java
export PATH=\${PATH}:\${JAVA_HOME}/bin
EOF

cat > etc/profile.d/$PKGNAM.csh << EOF
#!/bin/csh
setenv JAVA_HOME /usr/lib${LIBDIRSUFFIX}/java
setenv PATH \${PATH}:\${JAVA_HOME}/bin
EOF

chmod 755 etc/profile.d/*

## In $PKG
mkdir install
cat $CWD/slack-desc.$PKGNAM > install/slack-desc

## In $PKG
# If openjdk, docs go in /usr/doc/openjdk-VERSION
DOCDIR=${JDKDIR/jdk/$PKGNAM}
mkdir -p usr/doc/$DOCDIR
cd usr/doc/$DOCDIR
ln -s ../../lib${LIBDIRSUFFIX}/$JDKDIR/release
ln -s ../../lib${LIBDIRSUFFIX}/$JDKDIR/legal
# Only Oracle JDK has README
if [ "$PKGNAM" = "jdk" ]; then
    ln -s ../../lib${LIBDIRSUFFIX}/$JDKDIR/README
fi
cat $CWD/$(basename $0) > java.SlackBuild
cp ../../../install/slack-desc .

cd $PKG

rm -f $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz
/sbin/makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz
