#!/bin/bash

# sdl.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 18-Aug-2004

# Versions of additional stuff:
IMAGE=1.2.6
MIXER=1.2.8
NET=1.2.7
TTF=2.0.9

# 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 $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

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

# Extract source:
tar xvvf $CWD/SDL-$VERSION.tar.bz2
cd SDL-$VERSION
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 \
   --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.bz2
cd SDL_image-$IMAGE
slackhousekeeping

# Configure:
# we don't want sdl to load the libs with dlopen(), gcc is smarter...
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --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.bz2
cd SDL_mixer-$MIXER
slackhousekeeping

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

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --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_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.bz2
cd SDL_net-$NET
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --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.bz2
cd SDL_ttf-$TTF
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --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_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

