#!/bin/bash

# Copyright 2020  Patrick J. Volkerding, Sebeka, Minnesota, USA
# Modifications 2022-2023 Jay Lanagan (j@lngn.net), Detroit, MI, 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.

wget -c https://download.gnome.org/sources/libnma/1.10/libnma-1.10.6.tar.xz

cd $(dirname $0) ; CWD=$(pwd)

SRCNAM=libnma
PKGNAM=libnma-gtk4
VERSION=${VERSION:-1.10.6}
BUILD=${BUILD:-1}
TAG=${TAG:-_gfs}
PKGTYPE=${PKGTYPE:-txz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz"
  exit 0
fi

TMP=${TMP:-/tmp/gfs}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz || exit 1
cd $SRCNAM-$VERSION || exit 1
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 {} \+

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
mkdir build
cd build
meson setup \
  --prefix=/usr \
  --libdir=lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  -Dlibnma_gtk4=true 
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | \
  grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Remove all extra bits included in the base libnma package
# This package is _ONLY_ for the gtk4 lib
rm -r $PKG/usr/doc
rm -r $PKG/usr/include
rm -r $PKG/usr/share/glib-2.0
rm -r $PKG/usr/share/gtk-doc
rm -r $PKG/usr/share/locale
rm -f $PKG/usr/share/vala/vapi/libnma.deps
rm -f $PKG/usr/share/vala/vapi/libnma.vapi
rm -f $PKG/usr/share/gir-1.0/NMA-1.0.gir

rm $PKG/usr/lib${LIBDIRSUFFIX}/girepository-1.0/NMA-1.0.typelib
rm $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/libnma.pc
rm $PKG/usr/lib${LIBDIRSUFFIX}/libnma.so.0.0.0
rm $PKG/usr/lib${LIBDIRSUFFIX}/libnma.so.0
rm $PKG/usr/lib${LIBDIRSUFFIX}/libnma.so

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cat $CWD/$PKGNAM.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.SlackBuild

mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
if [ -f $CWD/slack-required ]; then cat $CWD/slack-required > $PKG/install/slack-required; fi
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
