#!/bin/bash

# arm/build
# Check package dependencies, set metadata and launch
# package build script.
# by Stuart Winter <mozes@slackware.com>
#
source /usr/share/slackdev/buildkit.sh

# Package metadata:
export PKGNAM=mesa
export VERSION=${VERSION:-26.0.5}
#export VERSION=${VERSION:-23.1.3}
export BUILD=${BUILD:-1}
slack_var_pkgseries

# If building in /patches for a Stable Release, we drop the packages into the
# '/patches' directory within the configured Slackware tree:
slack_findpkgstore_is_stablerelease && {
    export PKGSTORE=$PORTPATCHPKGS
    export PKGSERIES=/ # unset as it's not required for patches
    export BUILD=1_slack15.1
    mkdir -vpm755 $PKGSTORE/$PKGSERIES ;}

## ******************************************************************* ##
# For test packages - store in another location rather than overwriting
# the working copy in the main tree:
# export PKGSTORE=/tmp/
# mkdir -vpm755 $PKGSTORE/$PKGSERIES
## ******************************************************************* ##

# Set package name:
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

# Ensure base ARM packages are installed first:
slackbasedeps

# Ensure $PKGNAM isn't already installed:
slackfailpkgdeps $PKGNAM || removepkg $PKGNAM

# Ensure specific build dependencies (mainly libraries):
# You need mesa to be installed before running X11.SlackBuild, but you
# can run it straight after grabbing the git repo of modular xorg and
# then running the build script.
#
# Not found with lddslack:
#{ slackcheckpkgdeps Mako            || installpkg $PKGSTORE/l/Mako-[0-9]*.t?z || exit 99; }
#{ slackcheckpkgdeps libglvnd        || installpkg $PKGSTORE/x/libglvnd-[0-9]*.t?z || exit 99; }

# This is a circular dep.  Build Mesa first, then glu, then mesa, then glu, then mesa.
{ slackcheckpkgdeps glu             || installpkg $PKGSTORE/x/glu-[0-9]*.t?z     || exit 99; }

# Mesa 8.x needs this - v7.x didn't.
{ slackcheckpkgdeps libdrm      || installpkg $PKGSTORE/x/libdrm-[0-9]*.t?z  || exit 99; }

# The Slackware X11 build scripts reference the Mesa source in /tmp.
# Since we're building a new Mesa here (but not in /tmp), we need to remember
# to wipe this, since ARMedslack's scripts either unarchive the compiled
# copy (generated from mesa.SlackBuild), or use an existing copy in /tmp.
# Since we're obviously rebuilding Mesa, let's wipe it out:
# Mesa switched to lower case in the src archive at some point, so let's handle both:
rm -rf /tmp/Mesa* /tmp/mesa*

# Launch the package build script:
BUILDLOG=$SLKPORTARCH-$( basename $SLACKPACKAGE .t?z ).build.log
( ./$PKGNAM.SlackBuild ) >& /dev/stdout | tee $BUILDLOG

# Compress the build log:
bzip2 -9fvz $BUILDLOG

# We don't usually have llvm on the ARM builds:
#removepkg llvm
