#!/bin/sh

# Slackware build script for Pure Data

# Modified by by Ilya Dmitrichenko <errordeveloper-AT-gmail-DOT-com>
# Modified by Robby Workman <rworkman@slackbuilds.org> with minor stuff:
# -fix makefile patch to use DESTDIR correctly
# -miscellanous script tweaks
# No additional license terms
# Modified by Michales Michaloudes <korgie@gmail.com> (2011)
# - version bump
# - desktop icon
# - enable jack and portmidi
# - include all .h files

PRGNAM=pd
VERSION=${VERSION:-0.45.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

SRCVERSION=0.45-3

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-$SRCVERSION
tar xvf $CWD/$PRGNAM-$SRCVERSION.src.tar.gz
cd $PRGNAM-$SRCVERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

./autogen.sh

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --datadir=/usr/share/pd \
  --localstatedir=/var \
  --enable-alsa \
  --enable-setuid \
  --enable-jack \
  --disable-portaudio \
  --disable-portmidi
make
make install DESTDIR=$PKG

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

# create and cp KDE desktop icon
mkdir -p $PKG/usr/share/applications
mkdir -p $PKG/usr/share/$PRGNAM
cp -a $CWD/$PRGNAM.desktop $PKG/usr/share/applications
convert $TMP/$PRGNAM-$SRCVERSION/src/pd.ico $TMP/${PRGNAM}_48.xpm
cp $TMP/${PRGNAM}_48-3.xpm $PKG/usr/share/$PRGNAM

# For 0.43-0 version
#mkdir -p $PKG/usr/lib/pd/tcl/
#cp -a $TMP/$PRGNAM-$SRCVERSION/tcl/*.tcl $PKG/usr/lib/pd/tcl/

# copy all header files (for pd_mrpeach)
cp -p $TMP/$PRGNAM-$SRCVERSION/src/*.h $PKG/usr/include/

mv $PKG/usr/share/man $PKG/usr/man
find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a INSTALL.txt README.txt LICENSE.txt src/CHANGELOG.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
ln -s /usr/lib/pd/doc/ $PKG/usr/doc/$PRGNAM-$VERSION/docs

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

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