# Maintainer: Trevor SANDY <trevor[dot]sandy[at]gmail[dot]com>
#  Last Update: March 15, 2025
#  Copyright (C) 2017 - 2025 by Trevor SANDY
pkgname=lpub3d
pkgver=2.4.9.4133
pkgrel=1
pkgdesc="An LDraw Building Instruction Editor"
url="https://github.com/trevorsandy/lpub3d.git"
arch=('x86_64' 'i686')
license=('GPL3')
makedepends=('glu' 'gcc' 'make')
depends=('unzip' 'qt5-tools' 'qt5-base'  'libjpeg-turbo' 'mesa-libgl' 'gl2ps' 'tinyxml' 'minizip' 'llvm' 'mesa')
conflicts=()
replaces=()
backup=()
source=("lpub3d-git.tar.gz" "complete.zip" "lpub3dldrawunf.zip" "tenteparts.zip" "vexiqparts.zip" "povray.tar.gz" "ldview.tar.gz" "ldglite.tar.gz")
noextract=("complete.zip" "lpub3dldrawunf.zip" "tenteparts.zip" "vexiqparts.zip" "povray.tar.gz" "ldview.tar.gz" "ldglite.tar.gz")
md5sums=(SKIP SKIP SKIP SKIP SKIP SKIP SKIP SKIP)

prepare() {
	# location is ../src
	echo "  -> Current working directory: $PWD"
	export WD="$PWD"
	export LDRAWDIR="${HOME}/ldraw"
	export PLATFORM_PRETTY="Arch Linux"
	export PLATFORM_CODE="ar"
	export PLATFORM_VER="2025"
	export LPUB3D="${pkgname}"
	export TARGET_CPU="$(uname -m)"
	export LP3D_LOG_PATH="${WD}"
	export LP3D_CPU_CORES="$(nproc)"
	export LP3D_3RD_DIST_DIR="lpub3d_linux_3rdparty"
	cd ${pkgname}-git
	chmod a+x builds/utilities/CreateRenderers.sh && ./builds/utilities/CreateRenderers.sh
	# location is ../src/${pkgname}-git
	echo "  -> Current working directory: $PWD"
	if [ -f ../complete.zip ] ; then
		cp ../complete.zip mainApp/extras
		echo "LDraw archive library complete.zip copied"
	else
		echo "complete.zip not found at $PWD"
	fi
	if [ -f ../lpub3dldrawunf.zip ] ; then
		cp ../lpub3dldrawunf.zip mainApp/extras
		echo "LDraw archive library lpub3dldrawunf.zip copied"
	else
		echo "lpub3dldrawunf.zip not found at $PWD"
	fi
	if [ -f ../tenteparts.zip ] ; then
		cp ../tenteparts.zip mainApp/extras
		echo "LDraw archive library tenteparts.zip copied"
	else
		echo "tenteparts.zip not found at $PWD"
	fi
	if [ -f ../vexiqparts.zip ] ; then
		cp ../vexiqparts.zip mainApp/extras
		echo "LDraw archive library vexiqparts.zip copied"
	else
		echo "vexiqparts.zip not found at $PWD"
	fi
	export QT_SELECT=qt5
	export LP3D_BUILD_PKG=yes
	export LDRAWDIR=${HOME}/ldraw
	if [ -x /usr/bin/qmake ] ; then
		QMAKE_EXEC=qmake
	elif [ -x /usr/bin/qmake-qt5 ] ; then
		QMAKE_EXEC=qmake-qt5
	fi
	${QMAKE_EXEC} -makefile CONFIG+=release CONFIG+=build_check CONFIG-=debug_and_release CONFIG+=pkg
}

build() {
	cd ${pkgname}-git
	make clean
	make -j$(nproc)
	# check lpub3d dependencies
	LP3D_RELEASE="32bit_release"
	if [[ "${TARGET_CPU}" = "x86_64" || "${TARGET_CPU}" = "aarch64" ]] ; then
		LP3D_RELEASE="64bit_release"
	fi
	versuffix=$(cat builds/utilities/version.info | cut -d " " -f 1-2 | sed s/" "//g)
	validExe="mainApp/${LP3D_RELEASE}/lpub3d${versuffix}"
	if [ -f "${validExe}" ] ; then
		echo "LDD check ${validExe}..."
		ldd ${validExe} 2>/dev/null
	else
		echo "ERROR - LDD check failed for ${PWD}/${validExe}"
	fi
}

package() {
	cd ${pkgname}-git
	make INSTALL_ROOT=${pkgdir} install
}
