# Contributor: Timo Teras <timo.teras@iki.fi>
# Maintainer: Timo Teras <timo.teras@iki.fi>

setvar gccver = $(shell {source ../../main/gcc/APKBUILD ; echo $pkgver})
setvar gccpkgrel = $(shell {source ../../main/gcc/APKBUILD ; echo $pkgrel})

setvar pkgname = 'java-gcj-compat'
setvar pkgver = "$gccver"
setvar _mypkgrel = '3'
setvar pkgrel = $(( $_mypkgrel + $gccpkgrel ))
setvar pkgdesc = ""JDK like environment using GCJ""
setvar url = ""http://git.alpinelinux.org/aports.git""
setvar arch = ""all""
setvar license = ""GPL""
setvar depends = ""fastjar gcc-java=$gccver-r$gccpkgrel java-common""
setvar makedepends = ""gcc-java=$gccver-r$gccpkgrel zlib-dev paxmark""
setvar install = """"
setvar subpackages = """"
setvar source = """"

setvar builddir = ""$srcdir/$pkgname-$pkgver""

proc build {
	mkdir -p $builddir
	cd $builddir
	gcj -Wl,-Bsymbolic -findirect-dispatch -o ecj \
		--main=org.eclipse.jdt.internal.compiler.batch.Main \
		/usr/share/java/ecj.jar -lgcj
	paxmark -msp ecj
}

proc check {
	$builddir/ecj > /dev/null
}

proc package {
	setvar JVM_DIR = "$pkgdir/usr/lib/jvm"
	setvar JDK_DIR = "$JVM_DIR/java-1.5-gcj"
	setvar JDK_BIN_DIR = "$JDK_DIR/bin"
	setvar JRE_DIR = "$JDK_DIR/jre"
	setvar JRE_BIN_DIR = "$JRE_DIR/bin"
	setvar JRE_LIB_DIR = "$JRE_DIR/lib"

	setvar CPU = $(uname -m | sed -e 's/i.86/i386/g' -e 's/x86_64/amd64/g')

	mkdir -p $JDK_BIN_DIR
	ln -sf ../../../../bin/gij $JDK_BIN_DIR/java
	ln -sf ../../../../bin/fastjar $JDK_BIN_DIR/fastjar
	ln -sf ../../../../bin/sinjdoc $JDK_BIN_DIR/javadoc
	ln -sf ../../../../bin/grmic $JDK_BIN_DIR/rmic
	ln -sf ../../../../bin/gjavah $JDK_BIN_DIR/javah
	install -s $builddir/ecj $JDK_BIN_DIR/javac
	ln -sf ../../../../bin/gappletviewer $JDK_BIN_DIR/appletviewer
	ln -sf ../../../../bin/gjarsigner $JDK_BIN_DIR/jarsigner
	ln -sf ../../../../bin/grmiregistry $JDK_BIN_DIR/rmiregistry
	ln -sf ../../../../bin/gkeytool $JDK_BIN_DIR/keytool

	setvar gcjrel = $(ls -d /usr/lib/gcj-$gccver-* | cut -d '-' -f 3)

	mkdir -p $JRE_BIN_DIR $JRE_LIB_DIR/$CPU
	ln -sf ../../../../../bin/gij $JRE_BIN_DIR/java
	ln -sf ../../../../../bin/grmiregistry $JRE_BIN_DIR/rmiregistry
	ln -sf ../../../../../bin/gkeytool $JRE_BIN_DIR/keytool
	ln -sf ../../../../../share/java/libgcj-$gccver.jar $JRE_LIB_DIR/rt.jar
	ln -sf ../../../../../share/java/libgcj-tools-$gccver.jar $JRE_LIB_DIR/tools.jar
	ln -sf ../../../../../../lib/gcj-$gccver-$gcjrel/libjvm.so $JRE_LIB_DIR/$CPU
	ln -sf ../../../../../../lib/gcj-$gccver-$gcjrel/libjavamath.so $JRE_LIB_DIR/$CPU
	ln -sf ../../../../../../lib/gcj-$gccver-$gcjrel/classmap.db $JRE_LIB_DIR/$CPU
}