#!/bin/bash
#  Copyright (C) 2010, 2011  Matias A. Fonzo, <selk@dragora.org>
#  Copyright (C) 2012  Matias A. Fonzo, <selk@dragora.org>
#
#  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 3 of the License, 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/>.

set -e

CWD=$(pwd)

TMP=${TMP:-/tmp/sources}
OUT=${OUT:-/tmp/packages}

V=1.10.6
ARCH=${ARCH:-i486}
if [[ $ARCH = i486 ]]; then
  PKG_ARCH=i586
else
  PKG_ARCH=$ARCH
fi
B=1

PKG=${TMP}/package-icedtea6

rm -rf $PKG
mkdir -p $PKG $OUT

echo "Uncompressing the tarball..."
rm -rf ${TMP}/icedtea6-${V}
tar -xvf ${CWD}/icedtea6-${V}.tar.lz -C $TMP

cd ${TMP}/icedtea6-${V}

# Set sane ownerships and permissions:
chown -R 0:0 .
find . \
 \( -perm 2777 -o \
    -perm 777  -o \
    -perm 775  -o \
    -perm 711  -o \
    -perm 555  -o \
    -perm 511     \
 \) -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 autogen.sh
./autogen.sh

# Add OpenJDK sources:
mkdir -p drops
cp -v ${CWD}/sources/*.zip drops/
cp -v ${CWD}/sources/openjdk-6-*.tar.gz .

# Avoid download OpenJDK:
( mkdir -p stamps
  cd stamps
  touch hgforest.stamp download-openjdk.stamp download-jaxws-drop.stamp
)

cp ${CWD}/patches/free.luxi.fonts.path.diff patches/
export DISTRIBUTION_PATCHES="patches/free.luxi.fonts.path.diff"

unset CFLAGS CXXFLAGS MAKEFLAGS
unset JAVA_HOME ANT_HOME

./configure \
 --disable-docs \
 --disable-bootstrap \
 --with-jdk-home=/opt/jdk \
 --with-ecj-jar=/usr/share/java/ecj.jar \
 --with-ant-home=/usr/share/java/apache-ant \
 --with-xerces2-jar=/usr/share/java/xercesImpl.jar \
 --with-xalan2-jar=/usr/share/java/xalan.jar \
 --with-xalan2-serializer-jar=/usr/share/java/xalan-j2-serializer.jar \
 --with-rhino=/usr/share/java/js.jar \
 --with-tzdata-dir=/opt/icedtea6/jre/lib/zi \
 --with-pkgversion="Dragora GNU Linux" \
 --build=${ARCH}-dragora-linux-gnu

make

# Time to build the the package:
install -d ${PKG}/opt/icedtea6
cd openjdk.build
cp -a \
 j2sdk-image/* \
 ${PKG}/opt/icedtea6/
cd ..
# Add icons:
cd openjdk/jdk/src/solaris/classes/sun/awt/X11
install -m 644 java-icon16.png -D ${PKG}/usr/share/icons/hicolor/16x16/apps/java.png
install -m 644 java-icon24.png -D ${PKG}/usr/share/icons/hicolor/24x24/apps/java.png
install -m 644 java-icon32.png -D ${PKG}/usr/share/icons/hicolor/32x32/apps/java.png
install -m 644 java-icon48.png -D ${PKG}/usr/share/icons/hicolor/48x48/apps/java.png
# Add .desktop entries:
cd -
install -d ${PKG}/usr/share/applications
install -m 644 *.desktop ${PKG}/usr/share/applications/
rm -f ${PKG}/usr/share/applications/visualvm.desktop
sed -i 's|/usr/bin/||g' ${PKG}/usr/share/applications/*.desktop

# Strip binaries & libraries:
( cd $PKG
  find . -type f | xargs file | awk '/ELF/ && /executable/ || /shared object/' | \
   cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Include our general java profile:
install -d ${PKG}/etc/profile.d
zcat ${CWD}/java.profile.gz > ${PKG}/etc/profile.d/java.new
chmod 755 ${PKG}/etc/profile.d/java.new

# Compress & link manpages:
( cd ${PKG}/opt/icedtea6/man
  find . -type f -exec gzip -9N '{}' +
  find . -type l | while read file ; do
    ln -sf $(readlink $file).gz ${file}.gz
    rm $file
  done
)

# Copy the documentation:
mkdir -p ${PKG}/usr/doc/icedtea6-${V}
cp -a \
 AUTHORS COPYING ChangeLog HACKING NEWS README THANKYOU \
 ${PKG}/usr/doc/icedtea6-${V} || :

# Copy the description files:
mkdir -p ${PKG}/description
cp ${CWD}/description/* ${PKG}/description

# With the post-install script:
mkdir -p ${PKG}/install
zcat ${CWD}/post-install.gz > ${PKG}/install/post-install

cd $PKG
makepkg -l ${OUT}/icedtea6-${V}-${PKG_ARCH}-${B}.tlz

