# Template file for 'emacs'
pkgname=emacs
version=24.3
revision=8
nocross=yes
hostmakedepends="pkg-config"
makedepends="ncurses-devel libXaw-devel gtk+-devel"
depends="emacs-common-${version}_${revision}"
replaces="emacs>=0"
build_style="gnu-configure"
configure_args="ac_cv_lib_gif_EGifPutExtensionLast=yes"
short_desc="GNU Emacs editor"
maintainer="Philipp Hirsch <itself@hanspolo.net>"
license="GPL-3"
homepage="http://www.gnu.org/software/${pkgname}/"
distfiles="http://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${version}.tar.xz"
checksum=70aa2942e9ae689ed17eddedfca5027c364ffbcc8b59968b1645e935f4c7058d

# Package build options
build_options="dbus jpeg tiff gif png xpm svg xml imagemagick gnutls sound"
desc_option_xpm="Enable support for XPM images"
desc_option_sound="Enable support for sound"
build_options_default="jpeg tiff gif png xpm svg xml gnutls sound"

if [ "$build_option_dbus" ]; then
	configure_args+=" --with-dbus"
	makedepends+=" dbus-devel"
else
	configure_args+=" --without-dbus"
fi

if [ "$build_option_jpeg" ]; then
	configure_args+=" --with-jpeg"
	makedepends+=" libjpeg-turbo-devel"
else
	configure_args+=" --without-jpeg"
fi

if [ "$build_option_tiff" ]; then
	configure_args+=" --with-tiff"
	makedepends+=" tiff-devel"
else
	configure_args+=" --without-tiff"
fi

if [ "$build_option_gif" ]; then
	configure_args+=" --with-gif"
	makedepends+=" giflib-devel"
else
	configure_args+=" --without-gif"
fi

if [ "$build_option_png" ]; then
	configure_args+=" --with-png"
	makedepends+=" libpng-devel"
else
	configure_args+=" --without-png"
fi

if [ "$build_option_xpm" ]; then
	configure_args+=" --with-xpm"
	makedepends+=" libXpm-devel"
else
	configure_args+=" --without-xpm"
fi

if [ "$build_option_svg" ]; then
	configure_args+=" --with-rsvg"
	makedepends+=" librsvg-devel"
else
	configure_args+=" --without-rsvg"
fi

if [ "$build_option_imagemagick" ]; then
	configure_args+=" --with-imagemagick"
	makedepends+=" libmagick-devel"
else
	configure_args+=" --without-imagemagick"
fi

if [ "$build_option_xml" ]; then
	configure_args+=" --with-xml2"
	makedepends+=" libxml2-devel"
else
	configure_args+=" --without-xml2"
fi

if [ "$build_option_gnutls" ]; then
	configure_args+=" --with-gnutls"
	makedepends+=" gnutls-devel"
else
	configure_args+=" --without-gnutls"
fi

if [ "$build_option_sound" ]; then
	configure_args+=" --with-sound"
	makedepends+=" alsa-lib-devel"
else
	configure_args+=" --without-sound"
fi

pre_configure() {
	# Just configuring in different directories results in
	# spurious emacs rebuilds with incompatible build numbers.
	mkdir -p nox
	mv * nox || true
	cp -a nox x11
	cp -a nox gtk2 
}

do_configure() {
	cd $wrksrc/nox
	./configure --without-x ${configure_args} 

	cd $wrksrc/x11
	./configure --with-x-toolkit=athena --without-toolkit-scroll-bars ${configure_args} 

	cd $wrksrc/gtk2
	./configure --with-x-toolkit=gtk2 ${configure_args} 
}

do_build() {
	make ${makejobs} -C nox
	make ${makejobs} -C x11
	make ${makejobs} -C gtk2
}

do_install() {
	make DESTDIR=$DESTDIR -C ${wrksrc}/nox install
	rm -f ${DESTDIR}/usr/bin/ctags
	rm -f ${DESTDIR}/usr/share/man/man1/ctags.1*
	rm -f ${DESTDIR}/usr/share/info/info.info
	rm -rf ${DESTDIR}/usr/share/applications
	rm -rf ${DESTDIR}/usr/share/icons
}

emacs-common_package() {
	short_desc+=" - common files"
	noarch=yes
	pkg_install() {
		vmove usr/share/emacs
		vmove usr/share/man
		vmove usr/share/info
		vmove var/games/emacs
		rm -f ${PKGDESTDIR}/usr/share/info/info.info.gz
	}
}

emacs-x11_package() {
	depends="emacs-common-${version}_${revision}"
	provides="emacs-${version}_${revision}"
	replaces="emacs>=0"
	short_desc+=" - X11 version"
	pkg_install() {
		make DESTDIR=${PKGDESTDIR} -C ${wrksrc}/x11 install
		rm -f ${PKGDESTDIR}/usr/bin/ctags
		rm -rf ${PKGDESTDIR}/usr/share
		rm -rf ${PKGDESTDIR}/var/games/emacs
	}
}

emacs-gtk2_package() {
	depends="emacs-common-${version}_${revision} desktop-file-utils hicolor-icon-theme"
	provides="emacs-${version}_${revision}"
	replaces="emacs>=0"
	short_desc+=" - GTK+2 version"
	pkg_install() {
		make DESTDIR=${PKGDESTDIR} -C ${wrksrc}/gtk2 install
		rm -f ${PKGDESTDIR}/usr/bin/ctags
		rm -rf ${PKGDESTDIR}/usr/share/{emacs,man,info}
		rm -rf ${PKGDESTDIR}/var/games/emacs
	}
}
