# Template file for 'qmc2'
pkgname=qmc2
version=0.53
revision=1
wrksrc=${pkgname}
build_style=gnu-makefile
make_build_args="PREFIX=/usr DATADIR=/usr/share MAN_DIR=/usr/share/man TARGET=qmc2-mame"
make_install_args="${make_build_args}"
hostmakedepends="rsync unzip"
makedepends="SDL2-devel qt5-multimedia-devel qt5-script-devel qt5-svg-devel
 qt5-webkit-devel qt5-xmlpatterns-devel qt5-x11extras-devel"
depends="rsync ${pkgname}-data mame"
conf_files="/etc/${pkgname}/${pkgname}.ini"
short_desc="Qt M.A.M.E. Catalog/Launcher II"
maintainer="Jürgen Buchmüller <pullmoll@t-online.de>"
license="GPL-2"
homepage="http://qmc2.arcadehits.net/wordpress/"
distfiles="${SOURCEFORGE_SITE}/${pkgname}/${pkgname}/${version}/${pkgname}-${version}.tar.bz2
 http://distfiles.voidlinux.de/distfiles/qmc2_flyers-20150604.tar.xz
 http://distfiles.voidlinux.de/distfiles/qmc2_snapshots-20150604.tar.xz"
checksum="b8564aeab2ffb569d136a484e54468b2d346ff9346327b72383b5c3910d3eb61
 e36ecd9eaef60f94207fad8cdc323b114fbd9581feeb03de437df8146dd6cf8e
 421815fce90013ebd8c63f9a597bca6efa360058f0de7fc50eb7770c0367d962"

pre_build() {
	# Replace sdl-config with sdl2-config
	sed -e "s;sdl-config;sdl2-config;g" -i ${wrksrc}/scripts/sdl-version.sh
	# Replace obsolete -sdlmame suffix
	sed -e "s;-sdlmame;-mame;g" -i ${wrksrc}/Makefile
	# Replace lzma/CpuArch.c with patched version (http://sourceforge.net/p/sevenzip/bugs/1338/)
	cp -av ${FILESDIR}/CpuArch.c ${wrksrc}/lzma
}
do_build() {
	# Print configuration info
	make ${make_build_args} config
	make ${makejobs} qchdman
	make ${make_build_args} ${makejobs}
}
do_install() {
	# Install /usr/bin/qmc2-mame and /etc/qmc2/qmc2.ini
	vbin qmc2-mame
	vmkdir etc/${pkgname}
	sed ${wrksrc}/inst/qmc2.ini.template \
		-e "s;DATADIR;/usr/share;" \
		> ${DESTDIR}/etc/${pkgname}/${pkgname}.ini

	# Install qmc2 desktop file
	vmkdir usr/share/applications
	sed ${wrksrc}/inst/qmc2.desktop.template \
		-e "s;DATADIR;/usr/share;" \
		-e "s;EMULATOR;MAME;" \
		-e "s;TARGET;qmc2-mame;" \
		-e "s;EMUICO;mame.png;" \
		-e "s;GENERICNAME;$short_desc;" \
		> ${DESTDIR}/usr/share/applications/qmc2-mame.desktop

	# Install translations
	vmkdir usr/share/${pkgname}/lng
	for qm in $(find ${wrksrc}/data/lng -name "*.qm"); do
		vinstall $qm 644 ${DESTDIR}/usr/share/${pkgname}/lng
	done

	# Install qchdman and img
	vbin ${wrksrc}/tools/qchdman/qchdman
	vmkdir usr/share/${pkgname}
	cp -aR ${wrksrc}/data/img ${DESTDIR}/usr/share/${pkgname}

	# Install qchdman desktop file
	vmkdir usr/share/applications
	sed ${wrksrc}/inst/qchdman.desktop.template \
		-e "s;DATADIR;/usr/share;" \
		> ${DESTDIR}/usr/share/applications/qchdman.desktop
}
qmc2-data_package() {
	noarch=yes
	short_desc+=" - data files"
	pkg_install() {
		local ddir
		vmove usr/share/qmc2
		for ddir in opt img doc prv fly ico cat cab ctl \
			mrq ttl sws swn gmn js; do
			cp -aR data/$ddir ${PKGDESTDIR}/usr/share/qmc2
		done
	}
}
qmc2-flyers_package() {
	noarch=yes
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - machine flyers images"
	pkg_install() {
		mkdir -p ${PKGDESTDIR}/usr/share/qmc2
		mv -v ${XBPS_BUILDDIR}/fly ${PKGDESTDIR}/usr/share/qmc2
	}
}
qmc2-snapshots_package() {
	noarch=yes
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - machine preview images (snapshots)"
	pkg_install() {
		mkdir -p ${PKGDESTDIR}/usr/share/qmc2
		mv -v ${XBPS_BUILDDIR}/prv ${PKGDESTDIR}/usr/share/qmc2
	}
}
