#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# sdl.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 18-Aug-2004
#
# Copyright 2008, 2009, 2010, 2011, 2013, 2016, 2018  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Versions of additional stuff:
pushd $CWD
  VERSION=${VERSION:-$(echo SDL-*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  IMAGE=${IMAGE:-$(echo SDL_image-*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  MIXER=${MIXER:-$(echo SDL_mixer-*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  NET=${NET:-$(echo SDL_net-*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  SOUND=${SOUND:-$(echo SDL_sound-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
  TTF=${TTF:-$(echo SDL_ttf-*.tar.!(*sign|*asc|*sig) | 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

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

# Extract source:
tar xf $CWD/SDL-$VERSION.tar.!(*sign|*asc|*sig)
cd SDL-$VERSION || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

# Apply patches:
zcat $CWD/libsdl-1.2.15-resizing.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/sdl-1.2.14-fix-mouse-clicking.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/CVE-2021-33657.patch.gz | patch -p1 --verbose || exit 1

# GCC 14 "fix":
SLKCFLAGS+=" -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration"

# 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 || make || failmake

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

# Install to package:
make install DESTDIR=$PKG || failinstall

# Copy documentation:
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 xf $CWD/SDL_image-$IMAGE.tar.!(*sign|*asc|*sig)
cd SDL_image-$IMAGE || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

# 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 || make || 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

# We do not want to try to pull in -lmikmod, since that was linked static:
sed -i -e "s/ -lmikmod//g" $PKG/usr/lib${LIBDIRSUFFIX}/libSDL_mixer.la

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

# Extract source:
cd $TMPBUILD
tar xf $CWD/SDL_mixer-$MIXER.tar.!(*sign|*asc|*sig)
cd SDL_mixer-$MIXER || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

# Fix default library path.  Don't use /usr/local, and use lib64 where needed:
sed -i "s,usr/local/lib,usr/lib${LIBDIRSUFFIX},g" timidity/config.h

# Install patched static libmikmod:
tar xf $CWD/libmikmod-3.1.20.tar.!(*sign|*asc|*sig)
( cd libmikmod-3.1.20 || exit 1
  CFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr/local \
  --libdir=/usr/local/lib${LIBDIRSUFFIX} \
  --with-pic \
  --enable-shared=no \
  --enable-static=yes || exit 1
  make $NUMJOBS || make || exit 1
  make install || exit 1
) || exit 1

# 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 || make || 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 xf $CWD/SDL_net-$NET.tar.!(*sign|*asc|*sig)
cd SDL_net-$NET || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

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

# Build:
make $NUMJOBS || make || 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 xf $CWD/SDL_ttf-$TTF.tar.!(*sign|*asc|*sig)
cd SDL_ttf-$TTF || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

# Apply patches:
zcat $CWD/SDL_ttf.shaded.text.diff.gz | patch -p1 --verbose || exit 1

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

# Build:
make $NUMJOBS || make || 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

################################## sdl_sound #############################################

# Extract source:
cd $TMPBUILD
rm -rf SDL_sound-$SOUND
tar xf $CWD/SDL_sound-$SOUND.tar.!(*sign|*asc|*sig) || exit 1
cd SDL_sound-$SOUND || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

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

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG || exit 1

# Copy docs:
mkdir -p $PKG/usr/doc/SDL_sound-$SOUND
cp -a \
  CHANGES COPYING CREDITS README TODO \
  $PKG/usr/doc/SDL_sound-$SOUND

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

# Remove .la files from the package and the system (due to make install):
pushd $PKG
  for lafile in usr/lib${LIBDIRSUFFIX}/*.la ; do
    rm -f ${lafile} /${lafile}
  done
popd

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
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
