#!/bin/sh

# Build script for STK
# Author: Ejis <ejis@mailoo.org>

# Homepage: http://ccrma.stanford.edu/software/stk/


set -e


SOFTNAME=stk
VERSION=4.4.2
ZENVER=64
BUILD=1
CATEGORY=extra/d

MARCH=i686
FLAGS="-O2 -march=$MARCH -mtune=i686"

DOTNEW=1

CWD=$(pwd)

PKG=$SOFTNAME-$VERSION-$MARCH-$ZENVER.$BUILD

SOFTDIR=$CWD/$SOFTNAME-$VERSION
PKGDIR=$CWD/$PKG

PKGFILE=$PKGDIR.txz
MD5=$PKGDIR.md5
SRC=$PKGDIR.src
DEP=$PKGDIR.dep

SRCFILES=(
	$SOFTNAME-$VERSION.tar.gz
	$SOFTNAME.patch
)
SRCURLS=(
	http://ccrma.stanford.edu/software/stk/release
	http://zenwalk.pinguix.com/user-accounts/ejis/$CATEGORY/$SOFTNAME/$ZENVER
)

SRCLINKS=(
	${SRCURLS[0]}/${SRCFILES[0]}
	${SRCURLS[1]}/${SRCFILES[1]}
)

DOC=(
	$SOFTDIR/INSTALL
	$SOFTDIR/README
	$SOFTDIR/doc/*
)

EXTRADEPS=(
)

LESSDEPS=(
	fakeroot
)


# Check for source files and download them if necessary

if [ ! -f ${SRCFILES[0]} ]; then
	wget -c -t 0 -T 50 -w 0 ${SRCLINKS[0]} --no-check-certificate
fi

if [ ! -f ${SRCFILES[1]} ]; then
	wget -c -t 0 -T 50 -w 0 ${SRCLINKS[1]}
fi


# Erase the previous build files

rm -rf $SOFTDIR $PKGDIR $MD5 $SRC $DEP
mkdir -p $PKG


# Build

echo ""
echo "-----Building-----"
echo ""

tar -xvf $CWD/${SRCFILES[0]}

cd $SOFTDIR

export CFLAGS=$FLAGS
export CXXFLAGS=$FLAGS

./configure --with-jack RAWWAVE_PATH='/usr/share/stk/rawwaves/' \
				INCLUDE_PATH='/usr/include/stk/'

patch -p1 < $CWD/${SRCFILES[1]}

cd $SOFTDIR/src
make -j2


mkdir -p /usr/include/stk
mkdir -p /usr/share/stk/rawwaves

cp -rf $SOFTDIR/include/* /usr/include/stk/
cp -rf $SOFTDIR/rawwaves/* /usr/share/stk/rawwaves/

cd $SOFTDIR/projects/demo/
	make -j2

cd $SOFTDIR/projects/effects/
	make -j2

cd $SOFTDIR/projects/examples/
	make -j2

cd $SOFTDIR/projects/ragamatic/
	make -j2

rm -rfv /usr/include/stk /usr/share/stk


mkdir -p $PKGDIR/usr/lib
mkdir -p $PKGDIR/usr/include/stk
mkdir -p $PKGDIR/usr/share/stk/rawwaves
mkdir -p $PKGDIR/usr/share/stk/tcl/demo
mkdir -p $PKGDIR/usr/share/stk/tcl/effects
mkdir -p $PKGDIR/usr/share/stk/tcl/ragamatic
mkdir -p $PKGDIR/usr/bin


cp $SOFTDIR/src/libstk.a $PKGDIR/usr/lib/

cp -rf $SOFTDIR/include/* $PKGDIR/usr/include/stk/

cp -rf $SOFTDIR/rawwaves/* $PKGDIR/usr/share/stk/rawwaves/

cp -rf $SOFTDIR/projects/demo/tcl/* \
	$PKGDIR/usr/share/stk/tcl/demo/
cp -rf $SOFTDIR/projects/effects/tcl/* \
	$PKGDIR/usr/share/stk/tcl/effects/
cp -rf $SOFTDIR/projects/ragamatic/tcl/* \
	$PKGDIR/usr/share/stk/tcl/ragamatic/

cp $SOFTDIR/projects/demo/demo \
	$PKGDIR/usr/bin/stk-demo
cp $SOFTDIR/projects/effects/effects \
	$PKGDIR/usr/bin/stk-effects
cp $SOFTDIR/projects/examples/sine \
	$PKGDIR/usr/bin/stk-examples-sine
cp $SOFTDIR/projects/examples/foursine \
	$PKGDIR/usr/bin/stk-examples-foursine
cp $SOFTDIR/projects/examples/play \
	$PKGDIR/usr/bin/stk-examples-play
cp $SOFTDIR/projects/examples/record \
	$PKGDIR/usr/bin/stk-examples-record
cp $SOFTDIR/projects/examples/audioprobe \
	$PKGDIR/usr/bin/stk-examples-audioprobe
cp $SOFTDIR/projects/examples/midiprobe \
	$PKGDIR/usr/bin/stk-examples-midiprobe
cp $SOFTDIR/projects/examples/duplex \
	$PKGDIR/usr/bin/stk-examples-duplex
cp $SOFTDIR/projects/examples/inetIn \
	$PKGDIR/usr/bin/stk-examples-inetIn
cp $SOFTDIR/projects/examples/inetOut \
	$PKGDIR/usr/bin/stk-examples-inetOut
cp $SOFTDIR/projects/examples/rtsine \
	$PKGDIR/usr/bin/stk-examples-rtsine
cp $SOFTDIR/projects/examples/crtsine \
	$PKGDIR/usr/bin/stk-examples-crtsine
cp $SOFTDIR/projects/examples/bethree \
	$PKGDIR/usr/bin/stk-examples-bethree
cp $SOFTDIR/projects/examples/controlbee \
	$PKGDIR/usr/bin/stk-examples-controlbee
cp $SOFTDIR/projects/examples/threebees \
	$PKGDIR/usr/bin/stk-examples-threebees
cp $SOFTDIR/projects/examples/playsmf \
	$PKGDIR/usr/bin/stk-examples-playsmf
cp $SOFTDIR/projects/examples/grains \
	$PKGDIR/usr/bin/stk-examples-grains
cp $SOFTDIR/projects/ragamatic/ragamat \
	$PKGDIR/usr/bin/stk-ragamat


# Packaging

echo ""
echo "-----Packaging-----"
echo ""

cd $PKGDIR

mkdir -p $PKGDIR/install
mkdir -p $PKGDIR/usr/src/$SOFTNAME-$VERSION
mkdir -p $PKGDIR/usr/doc/$SOFTNAME-$VERSION


	       #|-----handy-ruler------------------------------------------------------|
cat << EOF > $PKGDIR/install/slack-desc
$SOFTNAME: $SOFTNAME - Synthesis ToolKit
$SOFTNAME:
$SOFTNAME: The Synthesis ToolKit in C++ (STK) is a set of open source
$SOFTNAME: audio signal processing and algorithmic synthesis classes
$SOFTNAME: written in the C++ programming language. STK was designed to
$SOFTNAME: facilitate rapid development of music synthesis and audio
$SOFTNAME: processing software, with an emphasis on cross-platform
$SOFTNAME: functionality, realtime control, ease of use, and educational
$SOFTNAME: example code.
$SOFTNAME: 
$SOFTNAME: 
EOF


# Redundent

if [ -d $PKGDIR/usr/share/doc ]; then
	mv $PKGDIR/usr/share/doc/$SOFTNAME/* $PKGDIR/usr/doc/$SOFTNAME-$VERSION/
	rm -rvf $PKGDIR/usr/share/doc
fi

if [ -d $PKGDIR/etc ]; then
	if [ $DOTNEW == 1 ]; then
		find $PKGDIR/etc -type f -exec \
			mv {} {}.new \;
	fi
fi

find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
	xargs strip --strip-unneeded 2> /dev/null || true
find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
	xargs strip --strip-unneeded 2> /dev/null || true

if [ -d $PKGDIR/usr/man ]; then
	
	( cd $PKGDIR/usr/man
		find . -type f -exec gzip -9 {} \;
		for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
	)
	
fi

if [ -d $PKGDIR/usr/info ]; then
	
	rm -f $PKGDIR/usr/info/dir
	gzip -9 $PKGDIR/usr/info/*.info*
	
fi

find . -name perllocal.pod \
	-o -name ".packlist" \
	-o -name "*.bs" \
	| xargs rm -f


cp -v $CWD/build-$SOFTNAME.sh \
	$PKGDIR/usr/src/$SOFTNAME-$VERSION/

cp -rv	${DOC[@]} $PKGDIR/usr/doc/$SOFTNAME-$VERSION/


# fix permissions and ownership
chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
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 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;


/sbin/makepkg -l y -c n $PKGFILE


cd $CWD

echo 'Creating .md5 file'
md5sum $PKG.txz > $MD5

echo 'Creating .src file'
for link in ${SRCLINKS[@]}; do
	echo $link >> $SRC
done

echo 'Checking for dependencies'
deps=$(depfinder $PKGFILE)

for dep in ${EXTRADEPS[@]}; do
	deps="$deps,$dep"
done

for dep in ${LESSDEPS[@]}; do
	deps=${deps/$dep/}
done

order=$(echo ${deps//,/ } | sort -d)
echo ${order// /,} > $DEP

echo "Removing build directories"
rm -rf $SOFTDIR $PKG
