#!/bin/sh

# Copyright 2008, 2009, 2010, 2011, 2012, 2018  Patrick J. Volkerding, Sebeka, MN, USA
#
# 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/>.

cd $(dirname $0) ; CWD=$(pwd)
TMP=${TMP:-/tmp}
VERSION=1.8.0_242
MAINVERSION=$(echo $VERSION | cut -f1 -d_)
ICEDTEA_VERSION=${ICEDTEA_VERSION:-$(\ls icedtea-*.tar.* | grep -v web | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
ICEDTEAWEB_VERSION=${ICEDTEAWEB_VERSION:-$(echo icedtea-web-*.tar.* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
RHINO_VERSION=${RHINO_VERSION:-$(echo rhino-*.zip | rev | cut -f 2- -d . | cut -f 1 -d - | rev)}
BUILD=${BUILD:-1}
TMP_BUILD=${TMP_BUILD:-$TMP/jdk-build}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i586 ;;
    arm*) export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) export ARCH=$( uname -m ) ;;
  esac
fi

NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}

if [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  LIBDIRSUFFIX="64"
else
  LIBDIRSUFFIX=${LIBDIRSUFFIX:-""}
fi

if [ "$ARCH" = "arm" ]; then
  EXTRA_CONF="LDFLAGS=-latomic" # Fix missing native atomic functions on armv5
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 jdk-$(echo $VERSION | tr - _)-$ARCH-$BUILD.txz
  echo jre-$(echo $VERSION | tr - _)-$ARCH-$BUILD.txz
  echo jdk-src-$(echo $VERSION | tr - _)-noarch-$BUILD.txz
  echo jdk-docs-$(echo $VERSION | tr - _)-noarch-$BUILD.txz
  echo icedtea-web-$(echo $ICEDTEAWEB_VERSION | tr - _)-$ARCH-$BUILD.txz
  exit 0
fi

rm -rf $TMP_BUILD
mkdir -p $TMP $TMP_BUILD

cd $TMP
rm -rf icedtea-$ICEDTEA_VERSION

tar xvf $CWD/icedtea-$ICEDTEA_VERSION.tar.* || exit 1

cd $TMP_BUILD

unzip $CWD/rhino-$RHINO_VERSION.zip || exit 1
RHINO_JAR=$TMP_BUILD/$(echo rhino$RHINO_VERSION/lib/rhino*jar);


GCJ=""
JDK_HOME=$JAVA_HOME

JAVACMD=`which javac 2> /dev/null`

if [ ! -x "$JAVACMD" ]
then
  # JavaC not found, try with gcj
  GCJ="--with-gcj=/usr/bin/gcj"
  JDK_HOME=/usr/lib${LIBDIRSUFFIX}/jvm
  export PATH=$PATH:$JDK_HOME/bin
  JAVACMD=`which ecj 2> /dev/null`
  if [ ! -x "$JAVACMD" ]
  then
    echo "No java compiler found (javac or ecj). Please set path to javac/ecj path"
    exit 1
  fi
fi

DOWNLOAD="--disable-downloading"
cp -pv $CWD/src/*.tar.?z . # Copy could fail, it will download packages automatically
if [ $? -ne 0 ]
then
  DOWNLOAD=""
else
  cp -pv $CWD/src/*.map . # Copy could fail, it will download packages automatically
  if [ $? -ne 0 ]
  then
    DOWNLOAD=""
  else
    if [[ `echo "$ARCH" | cut -c 1-3` = "arm" ]]
    then
      \rm -f hotspot.tar.?z
      cp -pv $CWD/src/hotspot.tar.?z.aarch32 . # Copy could fail, it will download packages automatically
      if [ $? -ne 0 ]
      then
        DOWNLOAD=""
      else
        rename .aarch32 "" hotspot*
      fi
    fi
  fi
fi

# Configure:
$TMP/icedtea-$ICEDTEA_VERSION/configure \
  --prefix=/usr/lib${LIBDIRSUFFIX}/java \
  --with-jdk-home=/usr/lib${LIBDIRSUFFIX}/java \
  $GCJ \
  --disable-system-kerberos \
  --disable-system-pcsc \
  --disable-system-sctp \
  $DOWNLOAD \
  $EXTRA_CONF \
  --build=$ARCH-slackware-linux || exit 1

# Build:
make $NUMJOBS || make || exit 1

for PKGNAM in jdk jre
do

if [ "$PKGNAM" = "jdk" ]; then
  JRE_PREFIX="jre/"
  IMAGE=j2sdk-image
else
  JRE_PREFIX=""
  IMAGE=j2re-image
fi

PKG=$TMP/package-$PKGNAM

rm -rf $PKG
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}
cd $PKG/usr/lib${LIBDIRSUFFIX} || exit 1
cp -av $TMP_BUILD/openjdk.build/images/$IMAGE java || exit 1

# Strip binaries:
echo "Strip binaries..."
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

if [ -e $PKG/usr/lib${LIBDIRSUFFIX}/java/src.zip ] ; then
  echo "Creating $PKGNAM-src package..."
  # Move src.zip to "-src" package
  mkdir -p $PKG-src/usr/lib${LIBDIRSUFFIX}/java
  mv $PKG/usr/lib${LIBDIRSUFFIX}/java/src.zip $PKG-src/usr/lib${LIBDIRSUFFIX}/java/src.zip
  mkdir -p $PKG-src/install
  cat $CWD/slack-desc.${PKGNAM}-src > $PKG-src/install/slack-desc
  cd $PKG-src
  /sbin/makepkg -l y -c n $TMP/${PKGNAM}-src-$(echo $VERSION | tr - _)-noarch-$BUILD.txz
fi

# Moving man manpages
mv $PKG/usr/lib${LIBDIRSUFFIX}/java/man $PKG/usr

# Compress and link manpages, if any:
echo "Compress manpages..."
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1 | grep -v '\.gz$') ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi


mkdir -p $PKG/etc/profile.d
for file in $(ls $CWD/profile.d/${PKGNAM}*) ; do
  cat $file | sed -e "s#lib/java#lib${LIBDIRSUFFIX}/java#" \
    > $PKG/etc/profile.d/$(basename $file)
done
chown -R root.root $PKG
find $PKG \
  \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 -o -perm 700 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;
chmod 755 $PKG/etc/profile.d/*


LIB_ARCH=$( cd $PKG/usr/lib${LIBDIRSUFFIX}/java/$JRE_PREFIX ; echo lib/*/*/libjvm.so | cut -f2 -d/)

( cd $PKG/usr/lib${LIBDIRSUFFIX}
  ln -sf java ${PKGNAM}${VERSION}
  ln -sf ./java/${JRE_PREFIX}lib/*/*/libjvm.so .
) || exit 1

mkdir -p $PKG/install
cat $CWD/slack-desc.${PKGNAM} > $PKG/install/slack-desc
cat << EOF > $PKG/install/doinst.sh
# Clean up a potential upgrade mess caused by changing the
# installation directory to /usr/lib${LIBDIRSUFFIX}/java/:
if [ -L usr/lib${LIBDIRSUFFIX}/java ]; then
  rm -rf usr/lib${LIBDIRSUFFIX}/java
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/man
EOF
if [ "$PKGNAM" = "jdk" ]; then
cat << EOF >> $PKG/install/doinst.sh
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/jre/lib/${LIB_ARCH}/server
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/jre/lib/${LIB_ARCH}/client
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/jre/javaws
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/jre/bin
EOF
else
cat << EOF >> $PKG/install/doinst.sh
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/lib/${LIB_ARCH}/server
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/lib/${LIB_ARCH}/client
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/javaws
EOF
fi
cat << EOF >> $PKG/install/doinst.sh
  mkdir -p usr/lib${LIBDIRSUFFIX}/java/bin
fi
if [ -L ControlPanel ]; then
  rm -f ControlPanel
fi
if [ -L ja ]; then
  rm -f ja
fi
if [ -L javaws ]; then
  rm -f javaws
fi
if [ -L libjsig.so ]; then
  rm -f libjsig.so
fi
# End cleanup.

EOF

# Install the desktop/icons files:
mkdir -p $PKG/usr/share/applications
for desktop in jconsole policytool
do
  echo Installing $PKG/usr/share/applications/$desktop.desktop ...
  cat $TMP_BUILD/$desktop-$MAINVERSION.desktop | sed "s/java-$MAINVERSION/java/" \
    > $PKG/usr/share/applications/$desktop.desktop
done

for size in 16 24 32 48
do 
  echo Installing $PKG/usr/share/icons/hicolor/${size}x${size}/apps/java.png ...
  mkdir -p $PKG/usr/share/icons/hicolor/${size}x${size}/apps
cp $TMP_BUILD/openjdk/jdk/src/solaris/classes/sun/awt/X11/java-icon${size}.png \
  $PKG/usr/share/icons/hicolor/${size}x${size}/apps/java.png
done

cd $PKG
rm -f $TMP/${PKGNAM}-$(echo $VERSION | tr - _)-$ARCH-$BUILD.txz
/sbin/makepkg -l y -c n $TMP/${PKGNAM}-$(echo $VERSION | tr - _)-$ARCH-$BUILD.txz

done


# Making doc package:
PKGNAM=jdk-docs
PKG=$TMP/package-$PKGNAM

rm -rf $PKG
mkdir -p $PKG/usr/doc
cd $PKG/usr/doc || exit 1
cp -av $TMP_BUILD/openjdk.build/docs $PKGNAM-$VERSION || exit 1

mkdir -p $PKG/install/
cat $CWD/slack-desc.${PKGNAM} > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $TMP/${PKGNAM}-$(echo $VERSION | tr - _)-noarch-$BUILD.txz


# Making mozilla plugin:
$CWD/icedtea-web.SlackBuild