# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI="7"


inherit flag-o-matic linux-info \
	multiprocessing prefix toolchain-funcs

MY_PV="${PV//_pre*}"
MY_P="${PN}-${MY_PV}"

# Patch version
PATCH_SET="https://dev.gentoo.org/~whissi/dist/mysql/mysql-8.0.23-patches-01.tar.xz"

SRC_URI="https://dev.mysql.com/get/Downloads/MySQL-Shell/mysql-shell-${PV}-src.tar.gz
https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-${PV}.tar.gz
	${PATCH_SET}"

HOMEPAGE="https://www.mysql.com/"
DESCRIPTION="An interface supporting development and administration for the MySQL Server"
LICENSE="GPL-2"
SLOT="8.0"
IUSE="cjk cracklib debug jemalloc latin1 libressl numa +perl profiling
	router selinux +server tcmalloc test python javascript"

S="${WORKDIR}/mysql-shell-${PV}-src"
# Tests always fail when libressl is enabled due to hard-coded ciphers in the tests
RESTRICT="!test? ( test ) libressl? ( test )"

REQUIRED_USE="?? ( tcmalloc jemalloc )
	cjk? ( server )
	jemalloc? ( server )
	numa? ( server )
	profiling? ( server )
	router? ( server )
	tcmalloc? ( server )"

KEYWORDS="~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sparc x86 ~amd64-linux ~x86-linux ~x64-macos ~x86-macos ~x64-solaris ~x86-solaris"

# Shorten the path because the socket path length must be shorter than 107 chars
# and we will run a mysql server during test phase

# Be warned, *DEPEND are version-dependant
# These are used for both runtime and compiletime
COMMON_DEPEND="
	>=app-arch/lz4-0_p131:=
	app-arch/zstd:=
	sys-libs/ncurses:0=
	>=sys-libs/zlib-1.2.3:0=
	libressl? ( dev-libs/libressl:0= )
	!libressl? ( >=dev-libs/openssl-1.0.0:0= )
	server? (
		dev-libs/icu:=
		dev-libs/libevent:=[ssl,threads]
		>=dev-libs/protobuf-3.8:=
		net-libs/libtirpc:=
		cjk? ( app-text/mecab:= )
		jemalloc? ( dev-libs/jemalloc:0= )
		kernel_linux? (
			dev-libs/libaio:0=
			sys-process/procps:0=
		)
		numa? ( sys-process/numactl )
		tcmalloc? ( dev-util/google-perftools:0= )
	)
"
DEPEND="${COMMON_DEPEND}
	|| ( >=sys-devel/gcc-3.4.6 >=sys-devel/gcc-apple-4.0 )
	virtual/yacc
	server? ( net-libs/rpcsvc-proto )
	test? (
		acct-group/mysql acct-user/mysql
		dev-perl/JSON
	)
"
RDEPEND="${COMMON_DEPEND}
	!dev-db/mariadb !dev-db/mariadb-galera !dev-db/percona-server !dev-db/mysql-cluster
	!dev-db/mysql:0
	!dev-db/mysql:5.7
	selinux? ( sec-policy/selinux-mysql )
	!prefix? (
		acct-group/mysql acct-user/mysql
		dev-db/mysql-init-scripts
	)
"

src_prepare() {
	cd  ${WORKDIR}/mysql-${PV}
	eapply "${WORKDIR}"/mysql-patches
    eapply_user
	# Avoid rpm call which would trigger sandbox, #692368
	sed -i \
		-e 's/MY_RPM rpm/MY_RPM rpmNOTEXISTENT/' \
		CMakeLists.txt || die

	# Remove the centos and rhel selinux policies to support mysqld_safe under SELinux
	if [[ -d "${S}/support-files/SELinux" ]] ; then
		echo > "${S}/support-files/SELinux/CMakeLists.txt" || die
	fi

	# Remove man pages for client-lib tools we don't install
	rm \
		man/my_print_defaults.1 \
		man/perror.1 \
		man/zlib_decompress.1 \
		|| die

	cd  ${WORKDIR}/mysql-shell-${PV}-src
	sed -i \
		-e 's/MY_RPM rpm/MY_RPM rpmNOTEXISTENT/' \
		CMakeLists.txt || die
}

src_configure() {
	# Bug #114895, bug #110149
	filter-flags "-O" "-O[01]"

	append-cxxflags -felide-constructors

	# bug #283926, with GCC4.4, this is required to get correct behavior.
	append-flags -fno-strict-aliasing

	CMAKE_BUILD_TYPE="RelWithDebInfo"

	# debug hack wrt #497532
	mycmakeargs=(
		-DCMAKE_C_FLAGS_RELWITHDEBINFO="$(usex debug '' '-DNDEBUG')"
		-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="$(usex debug '' '-DNDEBUG')"
		-DMYSQL_DATADIR="${EPREFIX}/var/lib/mysql"
		-DSYSCONFDIR="${EPREFIX}/etc/mysql"
		-DINSTALL_BINDIR=bin
		-DINSTALL_DOCDIR=share/doc/${PF}
		-DINSTALL_DOCREADMEDIR=share/doc/${PF}
		-DINSTALL_INCLUDEDIR=include/mysql
		-DINSTALL_INFODIR=share/info
		-DINSTALL_LIBDIR=$(get_libdir)
		-DINSTALL_MANDIR=share/man
		-DINSTALL_MYSQLSHAREDIR=share/mysql
		-DINSTALL_PLUGINDIR=$(get_libdir)/mysql/plugin
		-DINSTALL_MYSQLDATADIR="${EPREFIX}/var/lib/mysql"
		-DINSTALL_SBINDIR=sbin
		-DINSTALL_SUPPORTFILESDIR="${EPREFIX}/usr/share/mysql"
		-DCOMPILATION_COMMENT="Gentoo Linux ${PF}"
		-DWITH_UNIT_TESTS=$(usex test ON OFF)
		# Using bundled editline to get CTRL+C working
		-DWITH_EDITLINE=bundled
		-DWITH_ZLIB=system
		-DWITH_SSL=system
		-DWITH_LIBWRAP=0
		-DENABLED_LOCAL_INFILE=1
		-DMYSQL_UNIX_ADDR="${EPREFIX}/var/run/mysqld/mysqld.sock"
		-DWITH_DEFAULT_COMPILER_OPTIONS=0
		# The build forces this to be defined when cross-compiling. We pass it
		# all the time for simplicity and to make sure it is actually correct.
		-DSTACK_DIRECTION=$(tc-stack-grows-down && echo -1 || echo 1)
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON
		-DWITH_CURL=system
		-DWITH_BOOST="${S}/boost"
		-DWITH_ROUTER=$(usex router ON OFF)
	)

	if is-flagq -fno-lto ; then
		einfo "LTO disabled via {C,CXX,F,FC}FLAGS"
		mycmakeargs+=( -DWITH_LTO=OFF )
	elif is-flagq -flto ; then
		einfo "LTO forced via {C,CXX,F,FC}FLAGS"
		myconf+=( -DWITH_LTO=ON )
	else
		# Disable automagic
		myconf+=( -DWITH_LTO=OFF )
	fi

	if use test ; then
		mycmakeargs+=( -DINSTALL_MYSQLTESTDIR=share/mysql/mysql-test )
	else
		mycmakeargs+=( -DINSTALL_MYSQLTESTDIR='' )
	fi

	mycmakeargs+=( -DWITHOUT_CLIENTLIBS=YES )

	mycmakeargs+=(
		-DWITH_ICU=system
		-DWITH_LZ4=system
		# Our dev-libs/rapidjson doesn't carry necessary fixes for std::regex
		-DWITH_RAPIDJSON=bundled
		-DWITH_ZSTD=system
	)

	if [[ -n "${MYSQL_DEFAULT_CHARSET}" && -n "${MYSQL_DEFAULT_COLLATION}" ]] ; then
		ewarn "You are using a custom charset of ${MYSQL_DEFAULT_CHARSET}"
		ewarn "and a collation of ${MYSQL_DEFAULT_COLLATION}."
		ewarn "You MUST file bugs without these variables set."
		ewarn "Tests will probably fail!"

		mycmakeargs+=(
			-DDEFAULT_CHARSET=${MYSQL_DEFAULT_CHARSET}
			-DDEFAULT_COLLATION=${MYSQL_DEFAULT_COLLATION}
		)
	elif use latin1 ; then
		mycmakeargs+=(
			-DDEFAULT_CHARSET=latin1
			-DDEFAULT_COLLATION=latin1_swedish_ci
		)
	else
		mycmakeargs+=(
			-DDEFAULT_CHARSET=utf8mb4
			-DDEFAULT_COLLATION=utf8mb4_0900_ai_ci
		)
	fi

	if use server ; then
		mycmakeargs+=(
			-DWITH_EXTRA_CHARSETS=all
			-DWITH_DEBUG=$(usex debug)
			-DWITH_MECAB=$(usex cjk system OFF)
			-DWITH_LIBEVENT=system
			-DWITH_PROTOBUF=system
			-DWITH_NUMA=$(usex numa ON OFF)
		)

		if use jemalloc ; then
			mycmakeargs+=( -DWITH_JEMALLOC=ON )
		elif use tcmalloc ; then
			mycmakeargs+=( -DWITH_TCMALLOC=ON )
		fi

		if use profiling ; then
			# Setting to OFF doesn't work: Once set, profiling options will be added
			# to `mysqld --help` output via sql/sys_vars.cc causing
			# "main.mysqld--help-notwin" test to fail
			mycmakeargs+=( -DENABLED_PROFILING=ON )
		fi

		# Storage engines
		mycmakeargs+=(
			-DWITH_EXAMPLE_STORAGE_ENGINE=0
			-DWITH_ARCHIVE_STORAGE_ENGINE=1
			-DWITH_BLACKHOLE_STORAGE_ENGINE=1
			-DWITH_CSV_STORAGE_ENGINE=1
			-DWITH_FEDERATED_STORAGE_ENGINE=1
			-DWITH_HEAP_STORAGE_ENGINE=1
			-DWITH_INNOBASE_STORAGE_ENGINE=1
			-DWITH_INNODB_MEMCACHED=0
			-DWITH_MYISAMMRG_STORAGE_ENGINE=1
			-DWITH_MYISAM_STORAGE_ENGINE=1
		)
	else
		mycmakeargs+=(
			-DWITHOUT_SERVER=1
			-DWITH_SYSTEMD=no
		)
	fi

}

src_compile() {
  
  cd  ${WORKDIR}/mysql-${PV}
  mkdir build
  cd build
  cmake .. -DWITH_BOOST="../boost" -DWITH_SSL=system -DWITH_PROTOBUF=bundled -Dprotobuf_BUILD_SHARED_LIBS=OFF
  cmake --build . --target mysqlclient
  cmake --build . --target mysqlxclient
  cd  ${WORKDIR}/mysql-shell-${PV}-src
  mkdir build
  cd build
  protobuf_dir=$(basename $(find ${WORKDIR}/mysql-${PV}/extra/protobuf -maxdepth 1 -type d -name 'protobuf-*'))
  cmake .. \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DMYSQL_SOURCE_DIR="${WORKDIR}/mysql-${PV}" \
    -DMYSQL_BUILD_DIR="${WORKDIR}/mysql-${PV}/build" \
    -DWITH_SSL=system \
    -DWITH_STATIC_LINKING=1 \
	-DHAVE_PYTHON=1 \
	-DPYTHON_LIBRARY=/usr/lib64/libpython3.8.so \
	-DPYTHON_INCLUDE_DIR=/usr/include/python3.8 \
	-DPACKAGE_YEAR="$(date +%Y)" \
    -DWITH_PROTOBUF="${WORKDIR}/mysql-${PV}/extra/protobuf/$protobuf_dir/src" \
    -DPROTOBUF_INCLUDE_DIR="${WORKDIR}/mysql-${PV}/extra/protobuf/$protobuf_dir/src" \
    -DPROTOBUF_LIBRARY="${WORKDIR}/mysql-${PV}/bld/extra/protobuf/$protobuf_dir/cmake/libprotobuf.a"
  emake
}

src_install() {
	cd  ${WORKDIR}/mysql-shell-${PV}-src/build
    emake DESTDIR="${D}" install
    doman "${WORKDIR}/mysql-shell-${PV}-src/doc/man/mysqlsh.1"
}