#!/bin/bash

# Slackware build script for flac-opt

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20250512 bkw: BUILD=2
# - fix man breakage that occurred if MANPATH wasn't already set
#   when the profile script ran. Thanks to fourtysixandtwo for
#   catching this.

# Notes:
# - Not based on PV's flac.SlackBuild. Started with SBo template.
# - Static libraries, not shared, because they live in a weird prefix.
# - It's possible to export PREFIX=/whatever, but not documented in README
#   because I really don't expect anyone to do this.
# - I include the API docs and examples even though PV leaves them
#   out of his flac package.
# - Encoding really is about 25% faster than Slackware's older flac, even if
#   you don't use the new -j option to run in parallel. With -j8 on my 8-core
#   workstation, it's 4-5x as fast as the old flac.
# - It's possible to build with either autoconf or cmake. I tried both,
#   didn't see any difference. Went with autoconf because it's more
#   familiar. May change in the future, if upstream drops autoconf.
# - I see no advantage to building with -O2 rather than upstream's
#   default -O3, but I added an option to use -O2 if you're that
#   fanatical about using default CFLAGS.

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

PRGNAM=flac-opt
SRCNAM=flac
VERSION=${VERSION:-1.5.0}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

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 "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

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

# upstream uses -O3, we'll go with that unless the user insists.
COPT=-O3
[ "${FORCE_O2:-no}" = "yes" ] && COPT=-O2
SLKCFLAGS="$COPT $SLKCFLAGS"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz
cd $SRCNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod -h 755 {} + -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod -h 644 {} +

if [ "${ASM:-yes}" = "no" ]; then
  ASMOPT=disable
  WITHASM="WITHOUT"
else
  ASMOPT=enable
  WITHASM="WITH"
fi

PREFIX=${PREFIX:-/opt/$PRGNAM}
DOCDIR=/usr/doc/$PRGNAM-$VERSION
PKGDOC=$PKG/$DOCDIR
LIBDIR=$PREFIX/lib$LIBDIRSUFFIX

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --disable-examples \
  --disable-werror \
  --$ASMOPT-asm-optimizations \
  --prefix=$PREFIX \
  --libdir=$LIBDIR \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=$PREFIX/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --disable-shared \
  --enable-static \
  --build=$ARCH-slackware-linux

make V=1
make install-strip DESTDIR=$PKG
rm -f $PKG/$PREFIX/lib*/*.la
gzip -9 $PKG/$PREFIX/man/man*/*

PROF=$PKG/etc/profile.d
mkdir -p $PROF
for i in flac-opt.sh flac-opt-dev.sh; do
  sed -e "s,@PREFIX@,$PREFIX,g" -e "s,@LIBDIR@,$LIBDIR,g" $CWD/$i > $PROF/$i
done
chmod 755 $PROF/flac-opt.sh

# pkgconfig needs a bit of help. we do this so callers don't have to
# specify --static as a pkg-config option.
sed -i -e '/^Libs\.private/d' \
       -e '/^Libs:/s,$, -logg -lm,' \
       $PKG/$LIBDIR/pkgconfig/flac.pc

# we didn't build the examples, but include their source in the doc dir.
mkdir -p $PKGDOC
cp -a examples/ AUTHORS *.md COPYING* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed -e "s,@WITHASM@,$WITHASM," \
    -e "s,@PREFIX@,$PREFIX," \
    -e "s,@SLKCFLAGS@,$SLKCFLAGS," \
    $CWD/slack-desc > $PKG/install/slack-desc

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