#Packager: Dimitris Tzemos <dijemos~at~gmail~dot~com>

pkgname=openjdk
_majorver=17
_minorver=0
_securityver=3
_updatever=7
pkgver=${_majorver}.${_minorver}.${_securityver}
pkgrel=1dj
_git_tag=jdk-${_majorver}.${_minorver}.${_securityver}+${_updatever}
url='https://openjdk.java.net/'
source=("https://github.com/openjdk/jdk${_majorver}u/archive/${_git_tag}.tar.gz" 
		"https://people.salixos.org/djemos/salix/openjdk-bootstraps/OpenJDK17-17.0.3-$arch.tar.gz")

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"openjdk (open implementation of JDK 18)"
"openjdk is an open source implementation of pkgver 18 of"
"the Java Development Kit, Standard Edition. It includes tools for"
"developing, testing, and running programs written in Java."
""
"homepage: https://OpenJDK.java.net/"
)

build() {

cd $SRC

# Building openjdk from source requires bootstrapping from either a
# current or previous pkgver of the (open)jdk binary installation.
# Extract the OpenJDK17 binary to bootstrap
export BSDIR="OpenJDK17-17.0.3-$arch"
export BOOT_JAVA=$SRC/$BSDIR

cd jdk${_majorver}u-jdk-${pkgver}-${_updatever}

unset JAVA_HOME # recommended by upstream

# By default, the openjdk build uses all available cpu cores.
# We can override that here with the CORES= switch.
if [ "${CORES:-""}" ]; then
  JVAL="$(echo $CORES | grep -o "[0-9]")" || true
  [ -n "$JVAL" ] && SJOBS="--with-jobs=$JVAL"
  TJOBS=$JVAL
else
  SJOBS=""
  TJOBS="$(expr $(nproc) + 1)"
fi

sh configure \
  --with-boot-jdk=${BOOT_JAVA} \
  --with-extra-cflags="$SLKCFLAGS" \
  --with-extra-cxxflags="$SLKCFLAGS" \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$pkgname-$pkgver \
  --with-giflib=system \
  --with-harfbuzz=system \
  --with-lcms=system \
  --with-libjpeg=system \
  --with-libpng=system \
  --with-zlib=system \
  --disable-precompiled-headers \
  --enable-unlimited-crypto \
  --disable-warnings-as-errors \
  --with-native-debug-symbols=none \
  $SJOBS \
  --build=$arch-slackware-linux

unset MAKEFLAGS  # causes the build to fail if set
make bootcycle-images

# make install does not respect DESTDIR, so we must move the image:
mkdir -p $PKG/usr/lib$LIBDIRSUFFIX/java
cp -a build/*/images/jdk/* $PKG/usr/lib$LIBDIRSUFFIX/java

for s in 16 24 32 48; do
  install -vDm644 src/java.desktop/unix/classes/sun/awt/X11/java-icon${s}.png \
   $PKG/usr/share/icons/hicolor/${s}x${s}/apps/java.png
done

# Create some necessary symlinks:
( cd $PKG/usr/lib$LIBDIRSUFFIX
ln -sf java $pkgname-$pkgver
ln -sf java/lib/libjawt.so
ln -sf java/lib/server/libjvm.so
ln -sf java/lib/libjava.so
ln -sf java/lib/libawt.so
ln -sf java/lib/libawt_xawt.so
ln -sf java/lib/libverify.so
)

mkdir -p $PKG/usr/doc/$pkgname-$pkgver
cp -a \
  LICENSE ADDITIONAL_LICENSE_INFO ASSEMBLY_EXCEPTION README.md \
  $PKG/usr/doc/$pkgname-$pkgver
( cd $PKG/usr/doc/$pkgname-$pkgver
ln -s ../../lib${LIBDIRSUFFIX}/$pkgname-$pkgver/legal
ln -s ../../lib${LIBDIRSUFFIX}/$pkgname-$pkgver/release
)

# Create desktop entries (Thanks BLFS):
mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/openjdk-java.desktop << EOF
[Desktop Entry]
Name=OpenJDK Java ${pkgver} Runtime
Comment=OpenJDK Java ${pkgver} Runtime
Exec=/usr/lib${LIBDIRSUFFIX}/java/bin/java -jar
Terminal=false
Type=Application
Icon=java
MimeType=application/x-java-archive;application/java-archive;application/x-jar;
NoDisplay=true
EOF

cat > $PKG/usr/share/applications/openjdk-jconsole.desktop << EOF
[Desktop Entry]
Name=OpenJDK Java ${pkgver} Console
Comment=OpenJDK ${pkgver} Console
Keywords=java;console;monitoring
Exec=/usr/lib${LIBDIRSUFFIX}/java/bin/jconsole
Terminal=false
Type=Application
Icon=java
Categories=Application;System;
EOF

# Create /etc/profile.d scripts:
mkdir -p $PKG/etc/profile.d
cat > $PKG/etc/profile.d/jdk${_majorver}.sh << EOF
export JAVA_HOME=/usr/lib${LIBDIRSUFFIX}/java
export PATH=\${PATH}:\${JAVA_HOME}/bin
EOF

cat > $PKG/etc/profile.d/jdk${_majorver}.csh << EOF
setenv JAVA_HOME /usr/lib${LIBDIRSUFFIX}/java
setenv PATH \${PATH}:\${JAVA_HOME}/bin
EOF
chmod 755 $PKG/etc/profile.d/*

} 

doinst() {
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1
fi

if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database usr/share/mime >/dev/null 2>&1
fi

}