#!/bin/sh
# Build and install MySQL on Slackware
# by:  David Cantrell <david@slackware.com>
# Currently maintained by:  Patrick Volkerding <volkerdi@slackware.com>
CWD=`pwd`
TMP=${TMP:-/tmp}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
PKG=$TMP/package-devel32
mkdir -p $PKG

VERSION=5.0.51b
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-1}
DISTRO=${DISTRO:-slamd64}

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

if [ $DISTRO = slamd64 ]; then
	LIBSUFFIX=${LIBSUFFIX:-64}
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi
cd $TMP
rm -rf mysql-$VERSION
tar xjvf $CWD/mysql-$VERSION.tar.bz2
cd mysql-$VERSION
chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
CFLAGS="$SLKCFLAGS" CXX=gcc CXXFLAGS="$SLKCFLAGS -felide-constructors -fno-exceptions -fno-rtti" \
./configure \
  --prefix=/usr \
	--libdir=/usr/lib$LIBSUFFIX \
  --with-mysqld-user=mysql \
  --with-unix-socket-path=/var/run/mysql/mysql.sock \
  --localstatedir=/var/lib/mysql \
  --enable-assembler \
  --with-raid \
  --without-debug \
  --without-bench \
  --with-extra-charsets=complex \
  --with-vio \
  --with-openssl \
	--without-server \
	--without-client \
  --program-prefix="" \
  --program-suffix="" \
  $ARCH-$DISTRO-linux
#
# --without-readline
make -j6 || exit 1
make install DESTDIR=$PKG

( # Stuff we want from the 64-bit package instead, or don't want at all
	cd $PKG
	rm -rf usr/{man,mysql-test}
	rm usr/bin/*mysql*
)

# Strip ELF objects:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Add some handy library symlinks:
if [ -r $PKG/usr/lib$LIBSUFFIX/mysql/libmysqlclient.so.15 ]; then
  ( cd $PKG/usr/lib$LIBSUFFIX
    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$LIBSUFFIX/mysql/libmysqlclient_r.so.15 ]; then
  ( cd $PKG/usr/lib$LIBSUFFIX
    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