#!/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:-" -j7 "}

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

mkdir -p $TMP $TMP_BUILD

cd $TMP_BUILD
RHINO_JAR=$TMP_BUILD/$(echo rhino$RHINO_VERSION/lib/rhino*jar);

if [ ! -f $RHINO_JAR ]
then
  unzip $CWD/rhino-$RHINO_VERSION.zip || exit 1
  RHINO_JAR=$TMP_BUILD/$(echo rhino$RHINO_VERSION/lib/rhino*jar);
fi


JDK_HOME=$TMP_BUILD/openjdk.build/images/j2sdk-image
# JavaC not found, try with the one in icedtea package
export PATH=$JDK_HOME/bin:$PATH
JAVACMD=`which javac 2> /dev/null`

if [ ! -x "$JAVACMD" ]
then
  JDK_HOME=$JAVA_HOME
  # Just compiled JavaC not found, try with the one in the system
  export PATH=$PATH:$JDK_HOME/bin
  JAVACMD=`which javac 2> /dev/null`
  if [ ! -x "$JAVACMD" ]
  then
    echo "No java compiler found (javac). Please install jdk"
    exit 1
  fi
fi

# Making mozilla plugin:
PKGNAM=icedtea-web
PKG=$TMP/package-$PKGNAM
VERSION=$ICEDTEAWEB_VERSION

rm -rf $PKG
rm -rf $PKGNAM-$VERSION

mkdir -p $PKG

cd $TMP

tar xvf $CWD/$PKGNAM-$VERSION.tar.* || exit 1

cd $PKGNAM-$VERSION || exit 1

tagsoupjar=$(echo /usr/share/xml/docbook/xsl-stylesheets/extensions-*/tagsoup*jar)
if [ ! -f "$tagsoupjar" ]
then
  # Extract tagsoup jar from linuxdoc-tools:
  mkdir -p tagsoup
  cd tagsoup || exit 1
  rpm2cpio $CWD/docbook*rpm | cpio -idm
  tar xvf docbook-xsl-nons-*.tar.* --wildcards '*/extensions/tagsoup*jar'
  mkdir -p $PKG/usr/share/icedtea-web
  cp -p `\ls */extensions/tagsoup*jar` $PKG/usr/share/icedtea-web || exit 1
  tagsoupjar=$(echo $PKG/usr/share/icedtea-web/tagsoup*jar)
  cd -
fi

# Fixing configure to point to seamonkey-plugin:
sed -i 's/mozilla-plugin/seamonkey-plugin/g' configure

./configure \
  --prefix=/usr/lib${LIBDIRSUFFIX}/java \
  --with-jdk-home=$JDK_HOME \
  --with-rhino=$RHINO_JAR \
  --docdir=/usr/doc/icedtea-web \
  --mandir=/usr/man \
  --with-browser-tests=no \
  --with-tagsoup=$tagsoupjar \
  --with-itw-libs=BUNDLED \
  --build=$ARCH-slackware-linux || exit 1

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

make install DESTDIR=$PKG

# 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
)

# Make link to mozilla plugins directory
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/mozilla/plugins
( cd $PKG/usr/lib${LIBDIRSUFFIX}/mozilla/plugins
  ln -sf /usr/lib${LIBDIRSUFFIX}/java/lib/IcedTeaPlugins.so .
)

# Compress and link manpages, if any:
if [ -d $PKG/usr/lib$LIBDIRSUFFIX/java/man ]; then
  ( cd $PKG/usr/lib$LIBDIRSUFFIX/java/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/usr/doc/

mv $PKG/usr/lib$LIBDIRSUFFIX/java/doc/icedtea-web $PKG/usr/doc/$PKGNAM-$VERSION

cp -a \
  AUTHORS COPYING* ChangeLog NEWS README* \
  $PKG/usr/doc/$PKGNAM-$VERSION/

# If there's a ChangeLog, installing at least part of the recent history
# is useful, but don't let it get totally out of control:
if [ -r ChangeLog ]; then
  DOCSDIR=$(echo $PKG/usr/doc/${PKGNAM}-$VERSION)
  cat ChangeLog | head -n 1000 > $DOCSDIR/ChangeLog
  touch -r ChangeLog $DOCSDIR/ChangeLog
fi

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

# Build the package:
cd $PKG
/sbin/makepkg -l y -c n $TMP/${PKGNAM}-$(echo $VERSION | tr - _)-$ARCH-$BUILD.txz