# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>

pkgname=rstudio-desktop
pkgver=2023.3
_gittag=v2023.03.0+386
_panmirrortag=aea3027882718af657d60221818c3ff35f413219
pkgrel=0
pkgdesc="A powerful and productive integrated development environment (IDE) for R programming language"
arch="x86_64"
url="https://www.rstudio.com/products/rstudio/"
license='AGPL3'
options="!check" # No testsuite
# missing quarto-cli
depends="
	R>=3.0.1
	clang14
	hunspell-en
	libldap
	lxqt-config
	mathjax2
	pandoc
	sqlite
	"
makedepends="
	apache-ant
	boost-dev
	cmake>=3.1.0
	cups-libs
	desktop-file-utils
	grep
	libxml2-dev
	musl-dev
	openjdk8
	openssl
	patchelf
	postgresql-dev
	qt5-qtbase-dev
	qt5-qtlocation-dev
	qt5-qtsensors-dev
	qt5-qtsvg-dev
	qt5-qtwebchannel-dev
	qt5-qtwebengine-dev
	qt5-qtxmlpatterns-dev
	R-dev
	soci-dev
	unzip
	yaml-cpp-dev
	yarn
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/rstudio/rstudio/archive/refs/tags/$_gittag.tar.gz
	$pkgname-$pkgver-panmirror.tar.gz::https://github.com/quarto-dev/quarto/archive/$_panmirrortag.tar.gz
	12317_support-system-yaml-cpp.patch
	cran_multithread.patch
	desktop-main-skip-nosandbox-on-musl.patch
	filepath-use-unistd.patch
	nodejs-external.patch
	sessionhistoryarchive-use-ctime.patch
	qt.conf
	"
builddir="$srcdir"/rstudio-${_gittag/v}
builddir=${builddir/+/-}

prepare() {
	default_prepare
	sed "s/@@proc_num@@/$JOBS/" -i $builddir/dependencies/common/install-packages

	cd "$builddir"/dependencies/common
	_pandocver=$(grep -oP "(?<=PANDOC_VERSION=\").*(?=\"$)" install-pandoc)
	install -d pandoc/$_pandocver

	ln -sfT /usr/share/hunspell dictionaries
	ln -sfT /usr/share/mathjax2 mathjax-27
	# ln -sfT /opt/quarto quarto
	ln -sfT /usr/bin/pandoc pandoc/$_pandocver/pandoc
	ln -sfT /usr/bin/pandoc-citeproc pandoc/$_pandocver/pandoc-citeproc

	# Fix links for src/cpp/session/CMakeLists.txt
	cd "$builddir"/dependencies
	ln -sfT common/dictionaries dictionaries
	ln -sfT common/mathjax-27 mathjax-27
	# ln -sfT common/quarto quarto
	ln -sfT common/pandoc pandoc

	# Panmirror is picked up now from Quarto repo
	mkdir -p "$builddir"/src/gwt/lib
	cp -r "$srcdir/"quarto-$_panmirrortag "$builddir"/src/gwt/lib/quarto
}

build() {
	msg "Downloading and installing R packages..."
	R_LIBS_USER="$srcdir"/R/library/ R_USER="$srcdir"/R bash "$builddir"/dependencies/common/install-packages

	export PATH=/usr/lib/jvm/java-8-openjdk/jre/bin/:$PATH
	export JAVA_TOOL_OPTIONS="-Djava.util.prefs.userRoot=$srcdir"
	export BUILD_ID="local"
	export PACKAGE_OS="Alpine Linux"

	cmake -S "$builddir" \
		-B build \
		-DRSTUDIO_TARGET=Desktop \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr/lib/rstudio \
		-DRSTUDIO_USE_SYSTEM_BOOST=yes \
		-DRSTUDIO_USE_SYSTEM_YAML_CPP=yes \
		-DQT_QMAKE_EXECUTABLE=/usr/bin/qmake \
		-DBoost_NO_BOOST_CMAKE=ON \
		-DQUARTO_ENABLED=false \
		-DRSTUDIO_USE_SYSTEM_SOCI=yes \
		-DRSTUDIO_BUNDLE_QT=FALSE
	make -C build
}

package() {
	# Install the program
	make -C build DESTDIR="$pkgdir" install

	# Install the license
	install -Dm 644 "$builddir"/COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"

	# Symlink main binary
	install -d "$pkgdir/usr/bin"
	ln -sfT "/usr/lib/rstudio/bin/rstudio" "$pkgdir/usr/bin/rstudio"

	# BUGFIX: qt5-webengine isn't init'ing properly. Likely an Rstudio bug.
	install -Dm 644 "$srcdir/qt.conf" "$pkgdir/usr/lib/qt/libexec/qt.conf"
}
sha512sums="
64794b753bb366993d981a4df0a342a1d643a3c0105b80cca22a39a4665a47a0bb0573fbef2d1339a3a23652af67810b1afd842b8d407a4e3d622d5a5e4007c3  rstudio-desktop-2023.3.tar.gz
fc51164e454480e3bc4a1a4ebdc09bb71fca3a41d7eac1f3b1ac6e773d40a147f95a835f3733a19c8f5b30cb9e1be083c5b1671b1a5a01fc63439b5f282a7873  rstudio-desktop-2023.3-panmirror.tar.gz
bc02f3541e7c8b8bff296e9d9f627d6908d2464bdf6a2cb091795083721c89232fe45ade7b54af8b6532fed183b83838307396283ac9ae230a49014842e7b6da  12317_support-system-yaml-cpp.patch
eb0c023916babdbb3871bd7212c065ad16363e1b0c94db29eb283c4051681aa443bbe2fefd3bce286bf8b149aba1405bc09521b53fd16c31a566d463273ec969  cran_multithread.patch
54625810117dea0287a8184babde409e579ffc3cc0a138136a23ca088d5c4cb5ad1885d4d1cb123683a7bece43f9cf0c64ef43f79885a336b44000eca1e67647  desktop-main-skip-nosandbox-on-musl.patch
1a3da5f17dfed7463d7a7abadb167eacc784d0c2861fdfa9392a088e658778b45216de3e52940b0b150bed3b9236e4207b77ec82458d947642949315f466fe2f  filepath-use-unistd.patch
2a400a12886028f60ebeb56c0fc20799ab3ed641824497c93f2e935076172aefc199b7df9a5d8fdf0deb23089a183b8408d6ee6ced78bafb7a129ede75652c00  nodejs-external.patch
16a4445be211316ed890709cc068519d217210ee8e492509a0d1ae9883e61779c717ba04902d14c0f0a4ad19f46b527658f0eadd73876ba74e2e769d9103bbca  sessionhistoryarchive-use-ctime.patch
bd348f2ac6f1620ca0d8192e0deb2c2334c7f185da4cab72725119c5a0734b920230868aac2aaf0b211f842f114066cded54f616c1df83b47f0bd3d44df61b89  qt.conf
"
