# Build and install MySQL on Slackware
# by:  David Cantrell <david@slackware.com>
# maintained for Slackware by:  Patrick Volkerding <volkerdi@slackware.com>
# SlackBuild->TukBuild and other minor modifications by:  Lasse Collin <lasse.collin@tukaani.org>

NAME=mysql
VERSION=4.1.19
SOURCE=(http://mysql.dataphone.se/Downloads/MySQL-${VERSION%.*}/mysql-$VERSION.tar.gz
	rc.mysqld.gz
	doinst.sh.gz )
SOURCE_MD5=874514ef3cca9e729aaf4e0a894c5cbb
BUILD=${BUILD:-1}
# MySQL.com examples use -O3 so it should be safe. -fomit-frame-pointer
# is also in the examples. Comments are appreciated as always. ;-)
# --Larhzu
OPTIMIZE=${OPTIMIZE:-"-O3 -fomit-frame-pointer -pipe"}
ROOT_REQUIRED=1

build() {
	_explode 0
	cd mysql-$VERSION
	CXX=gcc CXXFLAGS="$CFLAGS -felide-constructors -fno-exceptions -fno-rtti" \
		./configure \
		--prefix=$PREFIX \
		--libdir=$LIBDIR \
		--with-mysqld-user=mysql \
		--with-unix-socket-path=$LOCALSTATEDIR/run/mysql/mysql.sock \
		--localstatedir=$LOCALSTATEDIR/lib/mysql \
		--enable-assembler \
		--with-raid \
		--without-debug \
		--enable-thread-safe-client \
		--without-bench \
		--with-extra-charsets=complex \
		--with-vio \
		--with-openssl \
		--with-archive-storage-engine \
		--with-csv-storage-engine \
		--program-prefix="" \
		--program-suffix="" \
		$CONFIGURE_TRIPLET
	# Remove --with-raid?

	make
	make install DESTDIR=$PKG

	# Not building for this:
	rm -f $PKG$BINDIR/{make_win_binary_distribution,make_win_src_distribution}

	mkdir -p $PKG/etc
	(
		cd support-files
		cp my-huge.cnf my-large.cnf my-medium.cnf my-small.cnf $PKG/etc
	)

	# install docs
	_doc 0 mysql-$VERSION  COPYING* README Docs/INSTALL-BINARY

	# Do not include the test suite:
	rm -rf $PKG$PREFIX/mysql-test

	# Add init script:
	mkdir -p $PKG/etc/rc.d
	# This is intentionally chmod 644.
	zcat ${SOURCE[1]} > $PKG/etc/rc.d/rc.mysqld
	_new 0 etc/rc.d/rc.mysqld

	# Install script:
	zcat ${SOURCE[2]} > $PKG/install/doinst.sh

	# Add some handy library symlinks:
	if [ -r $PKG$LIBDIR/mysql/libmysqlclient.so.14 ]; then
	(
		cd $PKG$LIBDIR
		rm -f libmysqlclient.so libmysqlclient.so.14
		ln -sf mysql/libmysqlclient.so .
		ln -sf mysql/libmysqlclient.so.14 .
	)
	fi
	
	if [ -r $PKG$LIBDIR/mysql/libmysqlclient_r.so.14 ]; then
	(
		cd $PKG$LIBDIR
		rm -f libmysqlclient_r.so libmysqlclient_r.so.14
		ln -sf mysql/libmysqlclient_r.so .
		ln -sf mysql/libmysqlclient_r.so.14 .
	)
	fi

	# Fix these before creating directories with special permissions:
	_chfix
	_chgrp_bin

	# this is the directory where databases are stored
	mkdir -p $PKG$LOCALSTATEDIR/lib/mysql
	chown mysql:mysql $PKG$LOCALSTATEDIR/lib/mysql
	chmod 750 $PKG$LOCALSTATEDIR/lib/mysql

	# this is where the socket is stored
	mkdir -p $PKG$LOCALSTATEDIR/run/mysql
	chown mysql:mysql $PKG$LOCALSTATEDIR/run/mysql
	chmod 755 $PKG$LOCALSTATEDIR/run/mysql
}
