#!/bin/bash

# sdl.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 18-Aug-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

# Versions of additional stuff:
pushd $CWD
  VERSION=${VERSION:-$(echo SDL-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  IMAGE=${IMAGE:-$(echo SDL_image-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  MIXER=${MIXER:-$(echo SDL_mixer-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  NET=${NET:-$(echo SDL_net-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  TTF=${TTF:-$(echo SDL_ttf-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
popd

# 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

################################## sdl ############################################

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

# Configure:
# We must use --disable-x11-shared or programs linked with SDL will
# crash on machines that use the closed source nVidia drivers.
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --disable-arts \
   --disable-esd \
   --enable-shared=yes \
   --enable-static=no \
   --disable-x11-shared || failconfig

# Build:
make $NUMJOBS || failmake

# Install onto filesystem so that mixer/image can compile:
make install || failinstall

# Install to package:
make install DESTDIR=$PKG
mkdir -p $PKG/usr/doc/SDL-$VERSION/html
cp -a docs/index.html $PKG/usr/doc/SDL-$VERSION
cp -a docs/html/*.html $PKG/usr/doc/SDL-$VERSION/html
cp -a BUGS COPYING CREDITS INSTALL README* TODO WhatsNew $PKG/usr/doc/SDL-$VERSION

################################## sdl_image ########################################

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/SDL_image-$IMAGE.tar.?z*
cd SDL_image-$IMAGE || exit 1
slackhousekeeping

# Configure:
# we don't want sdl to load the libs with dlopen(), gcc is smarter...
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --enable-shared=yes \
  --enable-static=no \
  --enable-jpg-shared=no \
  --enable-png-shared=no \
  --enable-tif-shared=no || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# Copy docs:
mkdir -p $PKG/usr/doc/SDL_image-$IMAGE
cp -a CHANGES COPYING README \
      $PKG/usr/doc/SDL_image-$IMAGE

################################## sdl_mixer ########################################

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/SDL_mixer-$MIXER.tar.?z*
cd SDL_mixer-$MIXER || exit 1
slackhousekeeping

# Don't look for things in /usr/local, since this is an installed package:
zcat $CWD/SDL_mixer.usrlocal.diff.gz | patch -p1 --verbose || exit 1

# Install patched static libmikmod:
unzip libmikmod-3.1.12.zip
( cd libmikmod-3.1.12.patched
  ./configure \
  --prefix=/usr/local \
  --libdir=/usr/local/lib${LIBDIRSUFFIX} \
  --with-pic \
  --enable-shared=no \
  --enable-static=yes
  make $NUMJOBS || make || exit 1
  make install
)

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --enable-shared=yes \
   --enable-music-mod \
   --enable-static=no || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# Copy docs:
mkdir -p $PKG/usr/doc/SDL_mixer-$MIXER
cp -a CHANGES COPYING README \
      $PKG/usr/doc/SDL_mixer-$MIXER

################################## sdl_net ##########################################

# Extract source:
cd $TMPBUILD
rm -rf SDL_net-$NET
tar xvvf $CWD/SDL_net-$NET.tar.?z*
cd SDL_net-$NET || exit 1
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --enable-shared=yes \
   --enable-static=no || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# Copy docs:
mkdir -p $PKG/usr/doc/SDL_net-$NET
cp -a CHANGES COPYING README \
     $PKG/usr/doc/SDL_net-$NET

################################## sdl_ttf #############################################

# Extract source:
cd $TMPBUILD
rm -rf SDL_ttf-$TTF
tar xvvf $CWD/SDL_ttf-$TTF.tar.?z*
cd SDL_ttf-$TTF || exit 1
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --enable-shared=yes \
   --enable-static=no || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

# Copy docs:
mkdir -p $PKG/usr/doc/SDL_ttf-$TTF
cp -a CHANGES COPYING README \
      $PKG/usr/doc/SDL_ttf-$TTF

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

# 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

