#!/bin/sh

# Slackware build script for aacgain

# Copyright 2009-2012 Heinz Wiesinger, Amsterdam, The Netherlands
# 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.

PRGNAM=aacgain
VERSION=1.9
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

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

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
mkdir -p $PRGNAM
cd $PRGNAM

#unpack mp3gain
rm -rf mp3gain
mkdir -p mp3gain
cd mp3gain
  unzip $CWD/mp3gain-1_5_2-src.zip
  chown -R root:root .
  chmod -R u+w,go+r-w,a-s .
cd -

#unpack faad2
rm -rf faad2
tar xvf $CWD/faad2-2.7.tar.bz2
mv faad2-2.7 faad2
cd faad2
  chown -R root:root .
  chmod -R u+w,go+r-w,a-s .
cd -

#unpack mp4v2
rm -rf mp4v2
tar xvf $CWD/mp4v2-trunk-r355.tar.bz2
mv mp4v2-trunk-r355 mp4v2
cd mp4v2
  chown -R root:root .
  chmod -R u+w,go+r-w,a-s .
cd -

#unpack aacgain
rm -rf $PRGNAM
tar xvf $CWD/${PRGNAM}_1_9.tar.bz2
cd $PRGNAM
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# The new patch program forces us to change a little how patches are applied
cd ../mp3gain
patch -p3 -i ../$PRGNAM/linux/mp3gain.patch
cd ../mp4v2
patch -p2 -i ../$PRGNAM/mp4v2.patch

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --build=$ARCH-slackware-linux

make libmp4v2.la

cd ../faad2

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --build=$ARCH-slackware-linux

cd libfaad

make

cd ../../aacgain/linux

# This don't work
sed -i "s|^patch|#patch|" ./prepare.sh
chmod +x ./prepare.sh
./prepare.sh

cd ../../

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --build=$ARCH-slackware-linux

make

mkdir -p $PKG/usr/bin
install -m 755 aacgain/aacgain $PKG/usr/bin/

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/{faad2,mp3gain,mp4v2}
cp -a aacgain/COPYING aacgain/README aacgain/linux/README.linux \
  $PKG/usr/doc/$PRGNAM-$VERSION
cp -a faad2/AUTHORS faad2/ChangeLog faad2/COPYING faad2/NEWS \
  faad2/README* faad2/TODO $PKG/usr/doc/$PRGNAM-$VERSION/faad2
cp -a mp3gain/lgpl.txt $PKG/usr/doc/$PRGNAM-$VERSION/mp3gain
cp -a mp4v2/COPYING mp4v2/INSTALL mp4v2/README \
  $PKG/usr/doc/$PRGNAM-$VERSION/mp4v2
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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