#!/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:-21.3.5}
export BUILD=${BUILD:-2}
export PKGSERIES=${PKGSERIES:-x}
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

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

# 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.
#
{ slackcheckpkgdeps lm_sensors      || installpkg $PKGSTORE/ap/lm_sensors-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps llvm            || installpkg $PKGSTORE/d/llvm-[0-9]*.t?z    || exit 99; }
{ slackcheckpkgdeps expat           || installpkg $PKGSTORE/l/expat-[0-9]*.t?z   || exit 99; }
{ slackcheckpkgdeps zstd            || installpkg $PKGSTORE/l/zstd-[0-9]*.t?z    || exit 99; }
{ slackcheckpkgdeps freeglut        || installpkg $PKGSTORE/x/freeglut-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps glew            || installpkg $PKGSTORE/x/glew-[0-9]*.t?z    || exit 99; }
{ slackcheckpkgdeps glu             || installpkg $PKGSTORE/x/glu-[0-9]*.t?z     || exit 99; }
{ slackcheckpkgdeps libX11          || installpkg $PKGSTORE/x/libX11-[0-9]*.t?z  || exit 99; }
{ slackcheckpkgdeps libXdamage      || installpkg $PKGSTORE/x/libXdamage-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libXext         || installpkg $PKGSTORE/x/libXext-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libXfixes       || installpkg $PKGSTORE/x/libXfixes-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libXv           || installpkg $PKGSTORE/x/libXv-[0-9]*.t?z   || exit 99; }
{ slackcheckpkgdeps libXvMC         || installpkg $PKGSTORE/x/libXvMC-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libXxf86vm      || installpkg $PKGSTORE/x/libXxf86vm-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libdrm          || installpkg $PKGSTORE/x/libdrm-[0-9]*.t?z  || exit 99; }
{ slackcheckpkgdeps libglvnd        || installpkg $PKGSTORE/x/libglvnd-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps libxcb          || installpkg $PKGSTORE/x/libxcb-[0-9]*.t?z  || exit 99; }
{ slackcheckpkgdeps libxshmfence    || installpkg $PKGSTORE/x/libxshmfence-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps wayland         || installpkg $PKGSTORE/x/wayland-[0-9]*.t?z || exit 99; }
# 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
