#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-flac

VERSION=1.2.1
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf flac-$VERSION
tar xjvf $CWD/flac-$VERSION.tar.bz2 || exit 1
cd flac-$VERSION
zcat $CWD/flac.man.diff.gz | patch -p1 --verbose --backup --suffix=.orig

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

# 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"}

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  $DISABLE_ASM_OPTIMIZATIONS \
  --build=$ARCH-slackware-linux
make -j4 || exit 1
make install DESTDIR=$PKG
# Adjust docs to traditional 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
rmdir $PKG/usr/doc/flac-$VERSION/html
# we're not putting 3MB of API docs in the binary package...
( cd $PKG/usr/doc/flac-$VERSION/api && rm -r * )
cat << EOF > $PKG/usr/doc/flac-$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 -a \
  AUTHORS COPYING* README \
  $PKG/usr/doc/flac-$VERSION
# Strip files
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
gzip -9 $PKG/usr/man/man?/*.?
mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n ../flac-$VERSION-$ARCH-$BUILD.tgz