#!/bin/bash

# flac.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 18-Sep-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/flac.man.diff.gz | patch -p1 --verbose --backup --suffix=.orig
zcat $PORTCWD/sources/flac*diff* | patch -p1
for i in 08_gcc-4.3_fixes.dpatch 09_noexec-stack.dpatch 07_metaflac_error_exit.dpatch 03_cosmetic_flac_stats.dpatch ; do
  patch --verbose -p1 < debian/patches/$i || exit 1
done

if [ "$ARCH" != "x86_64" ]; then
  # 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"}
else
  DISABLE_ASM_OPTIMIZATIONS=${DISABLE_ASM_OPTIMIZATIONS:-""}
fi

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure $DISABLE_ASM_OPTIMIZATIONS \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --build=$ARCH-slackware-linux-gnueabi \
   --host=$ARCH-slackware-linux-gnueabi \
   --target=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package directory:
make install DESTDIR=$PKG

# Move docs to Slackware location:
mv $PKG/usr/share/doc $PKG/usr
rm $PKG/usr/doc/flac-$VERSION/FLAC.tag
mv $PKG/usr/doc/flac-$VERSION/html/* $PKG/usr/doc/flac-$VERSION
rm -rf $PKG/usr/doc/flac-$VERSION/html
cp -a AUTHORS COPYING* README \
      $PKG/usr/doc/$PKGNAM-$VERSION
# we're not putting 3MB of API docs in the binary package...
( cd $PKG/usr/doc/$PKGNAM-$VERSION/api && rm -r * )
cat << EOF > $PKG/usr/doc/$PKGNAM-$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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
