#!/bin/bash

# mysql.SlackBuild
# Build and install MySQL on Slackware
# Author....................: David Cantrell <david@slackware.com>
# Currently maintained by...: Patrick Volkerding <volkerdi@slackware.com>
# Modified for Slackware ARM: Stuart Winter <mozes@slackware.com>
# 19-Sep-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

if ls $CWD/*.diff.gz 1> /dev/null 2> /dev/null ; then
  for patch in $CWD/*.diff.gz ; do
    zcat $patch | patch -p1 --verbose || exit 1
  done
fi

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS -felide-constructors -fno-exceptions -fno-rtti" \
CXX=gcc \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --with-mysqld-user=mysql \
   --with-innodb \
   --with-unix-socket-path=/var/run/mysql/mysql.sock \
   --localstatedir=/var/lib/mysql \
   --enable-assembler \
   --without-debug \
   --enable-thread-safe-client \
   --with-ssl=/usr \
   --enable-largefile \
   --with-readline \
   --with-extra-charsets=complex \
   --build=$ARCH-slackware-linux-gnueabi || failconfig
#
# --without-readline

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# Install additional headers needed for building external engine plugins
for i in sql include regex; do
  for j in $i/*.h; do
    install -m 644 $j $PKG/usr/include/mysql/
  done
done

mkdir -p $PKG/usr/include/mysql/atomic
for i in include/atomic/*.h; do
  install -m 644 $i $PKG/usr/include/mysql/atomic/
done

# The ./configure option to omit this has gone away, so we'll omit it
# the old-fashioned way.  It's all in the source tarball if you need it.
rm -rf $PKG/usr/sql-bench

# Install support files
mkdir -p $PKG/etc
cp support-files/my-{huge,large,medium,small}.cnf $PKG/etc

# Install docs
mkdir -p $PKG/usr/doc/mysql-$VERSION/Docs
cp -a \
  COPYING* EXCEPTIONS* INSTALL-SOURCE README* \
  $PKG/usr/doc/mysql-$VERSION
( cd Docs
  cp -a INSTALL-BINARY *.html *.txt Flags \
     $PKG/usr/doc/mysql-$VERSION/Docs )
## Too large to justify since the .html version is right there:
#rm $PKG/usr/doc/mysql-$VERSION/Docs/manual.txt
find $PKG/usr/doc/mysql-$VERSION -type f -exec chmod 0644 {} \;
changelogliposuction ChangeLog $PKGNAM $VERSION

# This is the directory where databases are stored
mkdir -p $PKG/var/lib/mysql
chown mysql.mysql $PKG/var/lib/mysql
chmod 0750 $PKG/var/lib/mysql

# This is where the socket is stored
mkdir -p $PKG/var/run/mysql
chown mysql.mysql $PKG/var/run/mysql
chmod 0755 $PKG/var/run/mysql

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

# Add init script:
mkdir -p $PKG/etc/rc.d
# This is intentionally chmod 644.
zcat $CWD/rc.mysqld.gz > $PKG/etc/rc.d/rc.mysqld.new

# Add some handy library symlinks:
if [ -r $PKG/usr/lib${LIBDIRSUFFIX}/mysql/libmysqlclient.so.16 ]; then
  ( cd $PKG/usr/lib${LIBDIRSUFFIX}
    rm -f libmysqlclient.so libmysqlclient.so.16
    ln -sf mysql/libmysqlclient.so .
    ln -sf mysql/libmysqlclient.so.16 .
  )
else
  exit 1
fi
if [ -r $PKG/usr/lib${LIBDIRSUFFIX}/mysql/libmysqlclient_r.so.16 ]; then
  ( cd $PKG/usr/lib${LIBDIRSUFFIX}
    rm -f libmysqlclient_r.so libmysqlclient_r.so.16
    ln -sf mysql/libmysqlclient_r.so .
    ln -sf mysql/libmysqlclient_r.so.16 .
  )
else
  exit 1
fi

##########################################################################
# Build MySQL embedded, for use with amarok.
# The static library is saved in this $CWD and copied onto the system
# by the amarok build script.
# The code here is taken from mysql-embedded.build
##########################################################################

echo "******************************************************************"
echo "* Building embedded MySQL library                                *"
echo "******************************************************************"

cd $TMPBUILD
rm -rf mysql-embedded
mkdir -vpm755 mysql-embedded
cd mysql-embedded
tar xf $CWD/$PKGNAM-$VERSION.tar.*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

if ls $CWD/*.diff.gz 1> /dev/null 2> /dev/null ; then
  for patch in $CWD/*.diff.gz ; do
    zcat $patch | patch -p1 --verbose || exit 1
  done
fi

CFLAGS="$SLKCFLAGS -fPIC" CXXFLAGS="$SLKCFLAGS -fPIC" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --datadir=/usr/share \
   --sysconfdir=/etc/mysql \
   --libexecdir=/usr/sbin \
   --localstatedir=/var/lib/mysql \
   --without-docs \
   --without-man \
   --without-server \
   --with-embedded-server \
   --without-innodb \
   --without-berkeley-db \
   --without-row-based-replication \
   --without-readline \
   --disable-shared \
   --with-charset=utf8 \
   --without-debug \
   --with-pthread \
   --without-ssl \
   --without-query-cache \
   --without-geometry \
   --with-pic || exit 1

make $NUMJOBS || make || exit 1

# Save the staic library.  It's 11MB uncompressed, 1.5M compressed.
xz -czf < libmysqld/libmysqld.a > $PORTCWD/libmysqld.a.xz || exit 1

##########################################################################

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
