#!/bin/bash

# mesa.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2006, 2007  Patrick J. Volkerding, Sebeka, MN, 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

DEMOVERS=8.0.1

# 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
export PKG=/tmp/package-mesa
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Be sure this list is up-to-date:
# ( "sis" driver doesn't compile on ARM. )
DRI_DRIVERS="i810,i915,i965,mach64,mga,nouveau,r128,r200,r300,r600,radeon,savage,tdfx,unichrome,swrast"

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t" 
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Mesa configure function:
function buildmesa() {

  # Clean up from the 1st pass:
  cd /
  rm -rf $TMPBUILD && mkdir -vpm755 $TMPBUILD
  cd $TMPBUILD

  # Extract source:
  echo "Unpacking sources..."
  tar xf $CWD/MesaLib-${VERSION}.tar.?z* || exit 1
  # If MesaLib came from git, we might already have GLUT.
  if [ ! -d Mesa*/src/glut/glx ]; then
     tar xvf $CWD/MesaGLUT-${VERSION}.tar.?z* || exit 1
  fi

  cd Mesa-$VERSION || exit 1
  slackhousekeeping

  # Apply patches:
  #
 if /bin/ls $CWD/patches/*.patch 1> /dev/null 2> /dev/null ; then
   for patch in $CWD/patches/*.patch ; do
     patch -p1 < $patch || exit 1 ;
   done
 fi

  # Revert "i915: Enable ARB_fragment_shader by default."
  # https://bugzilla.redhat.com/show_bug.cgi?id=643399
  zcat $CWD/patches/i915_fragment_shader_disable.patch.gz | patch -p1 || exit 1

  # Allow to build against libdrm-2.4.23:
  zcat $CWD/patches/mesa-7.9-libdrm-2.4.23-nouveau-api.patch.gz | patch -p1 || exit 1

  # Tell configure script to find the includes:
  autoreconf -vfi -I include/

  # These aren't for ARM:
  #  --enable-gallium-intel \
  #  --enable-gallium-radeon \
  #  --enable-gallium-nouveau \
  # Configure:
  CFLAGS="$SLKCFLAGS" \
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --mandir=/usr/man \
    --docdir=/usr/doc/mesa-$VERSION \
    --enable-xcb \
    --disable-gallium \
    --build=$ARCH-slackware-linux-gnueabi \
    $@ || exit 1

  # Build:
  make $NUMJOBS || make || exit 1

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

  # Install onto filesystem so that the demos can find the 
  # includes:
  make install || exit 1
}

# We two a two-pass build of mesa.  The first is with DRI support so that
# we can build the DRI header file, GL/internal/dri_interface.h for xorg-server.
# I think that we could build the xorg-server package without DRI (it's just a config option)
# but I'm not sure what else would break.
# This way seems to be good compromise.
#  
# The header file will be installed into the package, and then we can
# proceed to the 2nd pass:
cat << EOF 
*********************************************************************
** Building 1st pass of Mesa, enabling DRI to get header file      **
*********************************************************************
EOF
buildmesa \
  --with-driver=dri \
  --with-dri-driverdir=/usr/lib${LIBDIRSUFFIX}/xorg/modules/dri \
  --with-dri-drivers="$DRI_DRIVERS" || exit 1

# 2nd pass:
#
cat << EOF 
*********************************************************************
** Building 2nd pass of Mesa, enabling xlib as the default driver  **
*********************************************************************
EOF
buildmesa \
  --disable-egl \
  --with-driver=xlib \
  --disable-gl-osmesa || exit 1
#  --disable-glut || exit 1

# Now install the demos
( cd $TMPBUILD
cat << EOF 
*********************************************************************
** Building Mesa Demos                                             **
*********************************************************************
EOF
  rm -rf mesa-demos-$DEMOVERS
  tar xf $CWD/mesa-demos-$DEMOVERS.tar.?z* || exit 1
  cd mesa-demos-$DEMOVERS
  chown -R root:root .
  find . \
    \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
    -exec chmod 755 {} \; -o \
    \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
    -exec chmod 644 {} \;
  CFLAGS="$SLKCFLAGS -I$PWD/include/GL/glut.h" \
  ./configure \
    --prefix=/usr \
    --build=$ARCH-slackware-linux
  # Build and install gears and glinfo, as well as a few other demos
  make -C src/demos gears glinfo
  make -C src/xdemos \
    glthreads glxcontexts glxdemo glxgears glxgears_fbconfig \
    glxheads glxinfo glxpbdemo glxpixmap
  mkdir -p $PKG/usr/bin
  cp -a src/demos/{gears,glinfo} $PKG/usr/bin
  for i in glthreads glxcontexts glxdemo glxgears glxgears_fbconfig \
      glxheads glxinfo glxpbdemo glxpixmap ; do
        cp -a src/xdemos/$i $PKG/usr/bin ;
  done
)

mkdir -p $PKG/usr/doc/Mesa-$VERSION/html
cp -fav \
  docs/COPYING* docs/relnotes-${VERSION}*.html docs/README* docs/GL* \
  $PKG/usr/doc/Mesa-$VERSION
cp -fav docs/*.html $PKG/usr/doc/Mesa-$VERSION/html
rm -f $PKG/usr/doc/Mesa-$VERSION/html/relnotes*.html

# I know this is messy but the Slackware configure options
# for xorg-server looks for Mesa source in here, and it's easier
# to do this than modify the config options:
( cd $TMPBUILD
  echo "Archiving compiled mesa source..."
  tar -Ixz -pcf $PORTCWD/mesa-compiled.tar.xz Mesa-$VERSION )

# 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
