#!/bin/sh

#================================================================================
# This script fetches and builds the latest OpenJDK9 and OpenJFX (currently 8u40)
# and installs OpenJFX into the OpenJDK j2sdk image.
#
# It requires an existing 1.8 JDK installation to bootstrap the OpenJDK build
# (I used Oracle jdk1.8.0_20).
#
# Tested on Debian x86_64 (Squeeze and Wheezy)
#
# You might want to run this one step at a time rather than as an unattended
# script until you're happy it works correctly on your system.
#
# Copyright (c) 2014 Chris Newland.
# Licensed under https://github.com/AdoptOpenJDK/jitwatch/blob/master/LICENSE-BSD
#
#================================================================================
export HOME=~

export OPENJDK9=$HOME/openjdk9
export OPENJFX=$HOME/openjfx

mkdir $OPENJDK9
mkdir $OPENJFX

#===========================================
# Set up bootstrap JDK (1.8 needed for JDK9)
#===========================================
export JDK_HOME=$HOME/jdk1.8.0_20
export JAVA_HOME=$JDK_HOME

#==============================================
# Install required Debian packages with apt-get
#==============================================
sudo apt-get install build-essential bison flex gperf libasound2-dev libgl1-mesa-dev \
   libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libjpeg-dev \
   libpng-dev libx11-dev libxml2-dev libxslt1-dev libxt-dev \
   libxxf86vm-dev pkg-config qt4-qmake x11proto-core-dev \
   x11proto-xf86vidmode-dev libavcodec-dev mercurial libgtk2.0-dev \
   ksh libxtst-dev libudev-dev zip unzip libcups2-dev

#=================
# Fetch OpenJDK 9
#=================
hg clone http://hg.openjdk.java.net/jdk9/dev $OPENJDK9
cd $OPENJDK9
chmod u+x get_source.sh
./get_source.sh

#===========================================
# Update OpenJDK9 source (if already cloned)
#===========================================
cd $OPENJDK9
hg pull
hg update

#=================
# Build OpenJDK 9
#=================
cd $OPENJDK9
chmod u+x configure
./configure
make clean
make images
export OPENJDK9_J2SDK=$OPENJDK9/build/linux-x86_64-normal-server-release/images/j2sdk-image

#=================
# Fetch OpenJFX
#=================
cd $HOME
hg clone http://hg.openjdk.java.net/openjfx/8u-dev/rt $OPENJFX

#==========================================
# Update OpenJFX source (if already cloned)
#==========================================
cd $OPENJFX
hg pull
hg update

#========================================
# Fetch Gradle (if not already installed)
#========================================
cd $HOME
wget http://services.gradle.org/distributions/gradle-1.8-all.zip
unzip -o gradle-1.8-all.zip
export PATH=$PATH:$HOME/gradle-1.8/bin

#=================
# Build OpenJFX
#=================
cd $OPENJFX
gradle build

#========================================
# Copy OpenJFX into the OpenJDK SDK image
#========================================
cp $OPENJFX/build/sdk/rt/lib/javafx.properties $OPENJDK9_J2SDK/jre/lib
cp $OPENJFX/modules/fxpackager/build/tmp/tests/linuxapp/BareMinimum/TestPackager/runtime/jre/lib/jfr.jar $OPENJDK9_J2SDK/jre/lib/ext/

export BINARY_SOURCE=$OPENJFX/build/sdk/rt/lib/amd64/
export BINARY_TARGET=$OPENJDK9_J2SDK/jre/lib/amd64

cp $BINARY_SOURCE/libdecora_sse.so $BINARY_TARGET
cp $BINARY_SOURCE/libglass.so $BINARY_TARGET
cp $BINARY_SOURCE/libjavafx_iio.so $BINARY_TARGET
cp $BINARY_SOURCE/libprism_common.so $BINARY_TARGET
cp $BINARY_SOURCE/libprism_sw.so $BINARY_TARGET
cp $BINARY_SOURCE/libprism_es2.so $BINARY_TARGET
cp $BINARY_SOURCE/libjavafx_font.so $BINARY_TARGET
cp $BINARY_SOURCE/libjavafx_font_pango.so $BINARY_TARGET
cp $BINARY_SOURCE/libjavafx_font_freetype.so $BINARY_TARGET

cp $OPENJFX/build/sdk/rt/lib/ext/jfxrt.jar $OPENJDK9_J2SDK/jre/lib/ext

#=====================
# Test OpenJDK version
#=====================
$OPENJDK9_J2SDK/jre/bin/java -version