# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-emulation/basiliskII-jit/basiliskII-jit-1.0_pre20060501.ebuild,v 1.3 2008/08/24 12:51:33 maekke Exp $

inherit flag-o-matic eutils linux-mod

DESCRIPTION="Basilisk II with optional JIT engine for about 8x speedup: Macintosh Classic or II Emulator (MacOSes 6.x to 8.x)"
# https://lists.sourceforge.net/lists/listinfo/basilisk-devel

# Compiling the Source Code for Basilisk II Under Linux (JIT/Non-JIT)
# http://mes.emuunlim.com/projects/

# http://pleasantfiction.ipower.com/bodega/viewtopic.php?f=6&t=83

LICENSE="|| ( GPL-2 LGPL-2.1 )"
KEYWORDS="~amd64 -ppc ~x86"

IUSE="dga esd fbdev gtk jit nls sdl xv debug"

RDEPEND="esd? ( media-sound/esound )
	!sdl? ( fbdev? ( x11-drivers/xf86-video-fbdev ) )
	gtk? ( >=x11-libs/gtk+-1.3.15 gnome-base/libgnomeui )
	!sdl? ( dga? ( x11-libs/libXxf86dga ) )
	sdl? ( media-libs/libsdl )
	nls? ( virtual/libintl )

	!sdl? ( dga? ( x11-proto/xf86dgaproto ) )
	nls? ( sys-devel/gettext )

src_unpack() {
	unpack ${A}
	cd "${S}" || die "Failed to chdir to ${S}"
	cd ../..
	epatch ${FILESDIR}"/file_handle.patch" || die
	cd src/Unix || die
	epatch ${FILESDIR}"/sys_unix.cpp.patch" || die

	# Fix up the vendor (bug 35352)
	sed -i \
		-e "s/Mandrake/Gentoo/g" keycodes || \
			die "sed keycodes failed"

	#prevent prestripped binary
	sed -i -e '/^INSTALL_PROGRAM/s/-s//' Makefile.in

	# allow full screen, hildonized it, and fixed the "sticky-mouse" problem 
	# http://gm.stackunderflow.com/maemo/basiliskii/basiliskii_maemo_0.3.tar.gz

	# a version of basiliskII that makes use of gtk+ 2.0.
	# http://www.koders.com/noncode/fid387F60C37D25C6294E55C4763BC679807088557F.aspx

	if ( use sdl && ( use fbdev || use dga ) ) ; then
		elog "SDL support was requested, disabling DGA and fbdev"
	elif ( use dga && use fbdev ) ; then
		elog "DGA support was requested, disabling fbdev"

src_compile() {
	#fpu_x86 doesnt compile properly if -O3 or greater :(
	replace-flags -O[3-9] -O2

	# some patches from Debian to be considered
	# http://patch-tracking.debian.net/patch/debianonly/view/basilisk2/0.9.20070407-4

	local myflags

	use jit && myflags="--enable-jit-compiler"

	if ( use sdl && ( ! use fbdev ) ) ; then
		myflags="${myflags} --enable-fbdev-dga"
	elif ( use sdl && ( ! use dga ) ) ; then
		myflags="${myflags} --enable-xf86-dga --disable-fbdev-dga"
	elif ( ! use sdl && ( use dga ) ) ; then
		myflags="${myflags} --enable-xf86-dga"
	elif ( ! use sdl && ( use fbdev ) ) ; then
		myflags="${myflags} --enable-fbdev-dga"

	use debug && append-flags "-DDEBUG"

	# http://svn.debian.org/wsvn/collab-maint/deb-maint/basilisk2/trunk/ChangeLog.cvs?op=file&rev=0&sc=1
	# according to ChangeLog of svn sources
	# "BTW, a quick Speedometer 4 CPU performance test showed a +15%
	#  speed increase in real addressing mode vs. direct addressing.
	#  x86 arches don't benefit much from that mode since they support
	#  complex address modes already (beyond plain load/store)."

	# According to other comments in ChangeLog the SDL support is still
	# being improved (so far was slow) and maybe still is.

	# either of these two --with-gtk=gtk1 --with-gtk=gtk2 is possible
	econf $(use_with esd) \
		$(use_with gtk) \
		$(use_enable nls) \
		$(use_enable sdl sdl-video) \
		$(use_enable sdl sdl-audio) \
		$(use_enable xv xf86-vidmode) \
		${myflags} --enable-addressing=real

	emake || die "emake failed"
	# for direct access to an Ethernet card via the "sheep_net" kernel module
	# /lib/modules/<version>/kernel/drivers/net
	# add line
	# alias char-major-10-198 sheep_net
	# to /etc/modules.conf
	# FIXME: cannot compile the kernel module somehow
	# it seems 2.4.17 and early 2.6 were targeted in the past
	# make modules
	# alternatively one can use TAP interface, e.g. tap0 but enable in kernel
	# also "Kernel/User netlink socket" and "Netlink device emulation"
	# Seems current svn sources use some "router" mode or "tun" or "slirp"
	#  or "sheep_net" kernel module
	# http://www.cebix.net/viewcvs/cebix/BasiliskII/src/Unix/Linux/NetDriver/
	# modprobe tun
	# echo 1 >/proc/sys/net/ipv4/ip_forward


src_install() {

	emake DESTDIR="${D}" install || die "Install failed"

	cd ../../
	dodoc ChangeLog README TECH TODO

pkg_postinst() {
	einfo ""
	einfo "Please read the README doc file for information on networking"
	einfo "in Basilisk II. Probably the easiest would be to enble TAP or"
	einfo "SLIP in kernel. Also, make sure to enable IP forwarding:"
	einfo "echo 1 >/proc/sys/net/ipv4/ip_forward"
	einfo "For TAP to work also enable in kernel:"
	einfo "'Kernel/User netlink socket' and 'Netlink device emulation'"
	einfo ""
	einfo "Basilisk II requires a Mac II (1MB) or Mac Classic ROM (512kB)"
	einfo "to work. For example quadra650.rom, vMac.ROM, macse.rom, etc."
	einfo "System ROMs can be retreived from a real Mac, see info/man pages"
	einfo "and http://home.tampabay.rr.com/osemu/n800/b2/rom.htm"
	einfo ""
	einfo "Mac OS 7.5.3r2 is available freely from the Apple Homepage:"
	einfo "http://www.info.apple.com/support/oldersoftwarelist.html#system"
	einfo "A snapshot of the site is at"
	einfo "ftp://ftp.mayn.de/pub/really_old_stuff/unix/emulators/computer/macintosh"
	einfo "An update from 7.x to 7.5.5 is also available from that site."
	einfo "If your system does not boot due to some broken system modules,"
	einfo "e.g. A/ROSE or network stuff being broken, move such files outside"
	einfo "the 'system' folder. Then you can boot up and add one after another"
	einfo "back".
	einfo ""
	einfo "SDL support is slow, go for DGA instead."
	einfo "The following sites will guide your install:"
	einfo "http://emaculation.com/doku.php/basilisk_ii_setup"
	einfo "Software available: http://mes.emuunlim.com/software/index.htm"
	einfo ""
	einfo "Current, updated CVS is at http://www.cebix.net/viewcvs/cebix/BasiliskII/"
	einfo "and this ebuild should probably be chaged to build from those, updated sources."
	einfo "Do you want to help with this transition?"