#!/bin/sh

[ -z $PREPKG ] && exit 1

PACKAGE=flac
VERSION=1.2.1
FILES=$(cd $(dirname $0) ; pwd)

PATCHES="
$PACKAGE.man.diff.gz
"

for PATCH in $PATCHES ; do
  zcat $FILES/patches/$PATCH | patch -p0 &> $PATCH.output
done

mkdir -p \
  $PREPKG/install/

# We disable assembly optimizations to ensure compatibility with older
# hardware, but you're welcome to comment this out and give it a try.
# I've heard it gives a noticable speedup.
DISABLE_ASM_OPTIMIZATIONS=${DISABLE_ASM_OPTIMIZATIONS:-"--disable-asm-optimizations"}

chmod -R \
  g-w .
chown -R \
  root:root .
CFLAGS="-O2 -march=i486 -mtune=i686" \
CXXFLAGS="-O2 -march=i486 -mtune=i686" \
  ./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  $DISABLE_ASM_OPTIMIZATIONS \
  --build=i486-slackware-linux &> configure.output || exit 1
make $MAKEJOBS &> make.output || exit 1
make DESTDIR=$PREPKG install &> make_install.output || exit 1

# Adjust docs to traditional Slackware location:
mv \
  $PREPKG/usr/share/doc/ \
  $PREPKG/usr/
mv \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/html/* \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/
rmdir \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/html/

# We're not putting 3MB of API docs in the binary package...
rm \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/FLAC.tag \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/api/*
cat << EOF > $PREPKG/usr/doc/$PACKAGE-$VERSION/api/README
Full HTML based API documentation may be found in the FLAC source
package.  It is not included in the binary package due to size.
EOF

# Add a few more docs:
cp -p \
  AUTHORS COPYING* README \
  $PREPKG/usr/doc/$PACKAGE-$VERSION/
gzip -r9 \
  $PREPKG/usr/man/

cp \
  $FILES/slack-desc \
  $PREPKG/install/

find $PREPKG -exec file {} \; \
  | awk -F : '/ ELF / {print $1}' \
  | xargs strip -p --strip-unneeded
