# Maintainer: Christian Hesse <mail@eworm.de>
# Contributor: mickele <mimocciola at yahoo dot com> (librecad-git PKGBUILD)
# Contributor: Ilmari Repo <ilmari at gmail dot com> (librecad-svn PKGBUILD)
# Contributor: GazJ Gary James <garyjames82 at gmail dot com> (CADuntu PKGBUILD)

pkgname=librecad
pkgver=2.1.3
pkgrel=6
pkgdesc='A 2D CAD drawing tool based on the community edition of QCad'
arch=('x86_64')
url='https://www.librecad.org/'
license=('GPL')
depends=('qt5-base' 'qt5-svg' 'libxcb' 'muparser')
makedepends=('qt5-tools' 'boost' 'imagemagick' 'librsvg')
conflicts=('librecad-git')
replaces=('librecad-svn' 'caduntu' 'caduntu-svn')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/LibreCAD/LibreCAD/archive/${pkgver}.tar.gz"
        '0001-fix-build-with-Qt-5.11.patch'
        '0002-fix-build-with-Qt-5.15.patch'
        '0003-add-boost-tuple-include-to-fix-build.patch')
sha256sums=('74c4ede409b13d0365c65c0cd52dba04f1049530f6df706dc905443d5e60db06'
            '7590015c964888673844b1342e684e417b8bd2a5baacecebfbdc2f9aa0749360'
            'a5c37f871ce88a4efd0236c6329a19afe7993be96df9fe02b3affd79a44314bb'
            '84e9440b18ae28c22e7becf9e1f20f9ea38687d2112f38a1186b03a2278ce23a')

prepare() {
	cd "${srcdir}/LibreCAD-${pkgver}"

	# fix build with recent Qt
	patch -Np1 < ../0001-fix-build-with-Qt-5.11.patch
	patch -Np1 < ../0002-fix-build-with-Qt-5.15.patch
	patch -Np1 < ../0003-add-boost-tuple-include-to-fix-build.patch
}

build() {
	cd "${srcdir}/LibreCAD-${pkgver}"

	# fix version string
	sed -i "/^SCMREVISION/c SCMREVISION=\"${pkgver}\"" librecad/src/src.pro

	qmake-qt5 librecad.pro

	# fix include path... this is an issue with gcc 6.1.1 and qmake-qt5...
	make qmake_all
	sed -i '/INCPATH/s|-isystem /usr/include ||' librecad/src/Makefile

	make
}

package() {
	cd "${srcdir}/LibreCAD-${pkgver}"

	# executables
	install -D -m0755 unix/librecad "${pkgdir}/usr/bin/librecad"
	install -D -m0755 unix/ttf2lff "${pkgdir}/usr/bin/ttf2lff"

	# desktop file and man pages
	install -D -m0644 desktop/librecad.desktop "${pkgdir}/usr/share/applications/librecad.desktop"
	install -D -m0644 desktop/librecad.1 "${pkgdir}/usr/share/man/man1/librecad.1"

	# documentation
	install -D -m0644 librecad/support/doc/README "${pkgdir}/usr/share/doc/librecad/index.README"
	install -D -m0644 librecad/support/doc/index.html "${pkgdir}/usr/share/doc/librecad/index.html"
	install -D -m0644 librecad/support/doc/style.css "${pkgdir}/usr/share/doc/librecad/style.css"
	install -D -m0644 librecad/support/doc/img/librecadlogo.png "${pkgdir}/usr/share/doc/librecad/img/librecadlogo.png"

	# icons
	for SIZE in 16 24 32 48 64 96 128; do
		# set modify/create for reproducible builds
		convert -scale ${SIZE} +set date:create +set date:modify \
			desktop/graphics_icons_and_splash/Icon\ LibreCAD/Icon_Librecad.svg \
			"${srcdir}"/librecad.png
		install -D -m0644 "${srcdir}"/librecad.png "${pkgdir}/usr/share/icons/hicolor/${SIZE}x${SIZE}/apps/librecad.png"
	done
	install -D -m0644 desktop/graphics_icons_and_splash/Icon\ LibreCAD/Icon_Librecad.svg "${pkgdir}/usr/share/icons/hicolor/scalable/apps/librecad.svg"

	# resources
	mkdir -p "${pkgdir}/usr/share/librecad/"
	cp -r unix/resources/{library,patterns,fonts,qm} "${pkgdir}/usr/share/librecad/"
}

