#!/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 ARMedslack by: Stuart Winter <stuart@armedslack.org>
# 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=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
esac

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

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS -felide-constructors -fno-exceptions -fno-rtti" \
CXX=gcc \
./configure \
   --prefix=/usr \
   --with-mysqld-user=mysql \
   --with-unix-socket-path=/var/run/mysql/mysql.sock \
   --localstatedir=/var/lib/mysql \
   --enable-assembler \
   --with-raid \
   --without-debug \
   --enable-thread-safe-client \
   --without-bench \
   --with-extra-charsets=complex \
   --with-vio \
   --with-openssl \
   --build=$ARCH-slackware-linux || failconfig
#
# --without-readline

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# 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* MIRRORS 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 {} \;

# 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/mysql/libmysqlclient.so.15 ]; then
  ( cd $PKG/usr/lib
    rm -f libmysqlclient.so libmysqlclient.so.15
    ln -sf mysql/libmysqlclient.so .
    ln -sf mysql/libmysqlclient.so.15 .
  )
else
  exit 1
fi
if [ -r $PKG/usr/lib/mysql/libmysqlclient_r.so.15 ]; then
  ( cd $PKG/usr/lib
    rm -f libmysqlclient_r.so libmysqlclient_r.so.15
    ln -sf mysql/libmysqlclient_r.so .
    ln -sf mysql/libmysqlclient_r.so.15 .
  )
else
  exit 1
fi

# 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
