# Template file for 'CLion'
pkgname=CLion
version=2021.2.1
revision=1
archs="x86_64 aarch64"
wrksrc="clion-${version}"
depends="jetbrains-jdk-bin giflib libXtst"
short_desc="Smart cross-platform IDE for C and C++"
maintainer="Anton Afanasyev <anton@doubleasoftware.com>"
license="custom:Commercial"
homepage="https://www.jetbrains.com/clion"
distfiles="https://download.jetbrains.com/cpp/CLion-${version}.tar.gz"
checksum=2db99f8a755ad35b87b276dcbdfbb95be4fe35d0a741e7dfb340b2121403d44e
repository=nonfree
restricted=yes
nopie=yes
# JetBrains' tools are self-sufficient and while they include code that appears to be linked to libs from other packages, these libs are either included in the tool package, or the code works by looking for one of several supported libs.
noverifyrdeps=yes
python_version=3

build_options="bundled_cmake bundled_gdb bundled_lldb"
build_options_default="bundled_cmake bundled_gdb bundled_lldb"
desc_option_bundled_cmake="Install bundled CMake"
desc_option_bundled_gdb="Install bundled GDB"
desc_option_bundled_lldb="Install bundled LLDB"

post_extract() {
	# Remove files for other OSes and/or CPU architectures
	# Darwin (this is not packaged for macOS)
	rm -rf plugins/cwm-plugin/quiche-native/darwin-aarch64
	rm -rf plugins/cwm-plugin/quiche-native/darwin-x86-64
	rm -rf plugins/performanceTesting/bin/libyjpagent.dylib
	rm -rf plugins/python-ce/helpers/pydev/pydevd_attach_to_process/attach_x86.dylib
	rm -rf plugins/python-ce/helpers/pydev/pydevd_attach_to_process/attach_x86_64.dylib
	# Windows (this is not packaged for Windows)
	rm -rf plugins/cwm-plugin/quiche-native/win32-x86-64
	rm -rf plugins/performanceTesting/bin/yjpagent.dll
	rm -rf plugins/performanceTesting/bin/yjpagent64.dll
	rm -rf plugins/python-ce/helpers/pydev/pydevd_attach_to_process/attach_amd64.dll
	rm -rf plugins/python-ce/helpers/pydev/pydevd_attach_to_process/attach_x86.dll
	# x86 (unsupported after v2021.1)
	rm -rf bin/clion.vmoptions
	rm -rf lib/pty4j-native/linux/x86
	rm -rf plugins/performanceTesting/bin/libyjpagent.so
	rm -rf plugins/python-ce/helpers/pydev/pydevd_attach_to_process/attach_linux_x86.so
	# MIPS
	rm -rf lib/pty4j-native/linux/mips64el
	# ARM
	rm -rf lib/pty4j-native/linux/arm
	# PPC
	rm -rf lib/pty4j-native/linux/ppc64le


	case "$XBPS_TARGET_MACHINE" in
		x86_64)
			rm -rf lib/pty4j-native/linux/aarch64
			;;
		aarch64)
			rm -rf lib/pty4j-native/linux/x86-64
			rm -rf plugins/performanceTesting/bin/libyjpagent64.so
			rm -rf plugins/python-ce/helpers/pydev/pydevd_attach_to_process/attach_linux_amd64.so
			;;
	esac

	if [ ! "$build_option_bundled_cmake" ]; then
		rm -rf bin/cmake
	fi
	if [ ! "$build_option_bundled_gdb" ]; then
		rm -rf bin/gdb
	fi
	if [ ! "$build_option_bundled_lldb" ]; then
		rm -rf bin/lldb
	fi
}

do_install() {
	TARGET_PATH="usr/lib/${pkgname}"

	vmkdir usr/bin
	vmkdir ${TARGET_PATH}

	local i
	for i in license/* ; do
		vlicense $i
	done

	local launcher_path="bin/clion.sh"
	sed -i '1 s/$/\nCLION_JDK=${CLION_JDK:-${IDEA_JDK}}/' "${launcher_path}"
	vcopy bin ${TARGET_PATH}
	vcopy help ${TARGET_PATH}
	vcopy lib ${TARGET_PATH}
	vcopy plugins ${TARGET_PATH}
	vcopy product-info.json ${TARGET_PATH}
	vcopy build.txt ${TARGET_PATH}

	ln -sf "/${TARGET_PATH}/${launcher_path}" "${DESTDIR}/usr/bin/${pkgname}"
}
