#!/bin/sh

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

# Homepage: http://kokkinizita.net/linuxaudio/aeolus/index.html


set -e


SOFTNAME=aeolus
VERSION=0.8.4
STOPSVER=0.3.0
ZENVER=64
BUILD=2
CATEGORY=extra/xap

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
STOPSDIR=$CWD/stops-$STOPSVER

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

SRCFILES=(
	$SOFTNAME-$VERSION.tar.bz2
	stops-$STOPSVER.tar.bz2
	$SOFTNAME.png
)
SRCURLS=(
	http://kokkinizita.net/linuxaudio/downloads
	http://zenwalk.pinguix.com/user-accounts/ejis/$CATEGORY/$SOFTNAME/$ZENVER
)

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

DOC=(
	$SOFTDIR/AUTHORS
	$SOFTDIR/COPYING
	$SOFTDIR/INSTALL
	$SOFTDIR/INSTALL-osx
	$SOFTDIR/README
)

EXTRADEPS=(
)

LESSDEPS=(
)


# Check for source files and download them if necessary

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

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

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


# Erase the previous build files

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


# Build

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

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

cd $SOFTDIR/source

export CFLAGS=$FLAGS
export CXXFLAGS=$FLAGS

sed -i "s:PREFIX = /usr/local:PREFIX = /usr:g" ./Makefile
sed -i "s:-O3::g" ./Makefile

make -j2 || return 1

sed -i "s:PREFIX = /usr:PREFIX = $PKGDIR/usr:g" ./Makefile
sed -i "s:\$\(PREFIX\)/bin:\$\(PREFIX\)/bin/:g" ./Makefile

mkdir -p $PKGDIR/usr/bin

make install


mkdir -p $PKGDIR/usr/share/aeolus
mkdir -p $PKGDIR/usr/share/icons/hicolor/48x48/apps
mkdir -p $PKGDIR/usr/share/applications

mv -v $PKGDIR/usr/bin/aeolus \
	$PKGDIR/usr/bin/aeolus-bin
cat << EOF > $PKGDIR/usr/bin/aeolus
#!/bin/sh
if [[ ! -d \$HOME/.aeolus/stops-$STOPSVER ]]; then
	mkdir \$HOME/.aeolus
	cp -rvf /usr/share/aeolus/stops-$STOPSVER \$HOME/.aeolus/
	rm -rvf \$HOME/.aeolus/stops
	ln -sv \$HOME/.aeolus/stops-$STOPSVER \$HOME/.aeolus/stops
fi

if [[ ! -r \$HOME/.aeolusrc ]]; then
	echo "-J -S /home/\`whoami\`/.aeolus/stops" > \$HOME/.aeolusrc
fi

/usr/bin/aeolus-bin \$*
EOF
chmod -v 755 $PKGDIR/usr/bin/aeolus

cd $CWD

tar -xjvf $CWD/stops-$STOPSVER.tar.bz2
mv -v $STOPSDIR $PKGDIR/usr/share/aeolus/

cp -v $CWD/${SRCFILES[2]} \
	$PKGDIR/usr/share/icons/hicolor/48x48/apps/

cat << EOF > $PKGDIR/usr/share/applications/$SOFTNAME.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Aeolus
Comment=Pipe organ emulator
Exec=aeolus
Icon=aeolus
Terminal=false
Type=Application
X-MultipleArgs=false
Categories=Audio;AudioVideo;AudioVideoEditing;Multimedia
EOF


# 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
$SOFTNAME:
$SOFTNAME: Aeolus is a synthesised (i.e. not sampled) pipe organ emulator
$SOFTNAME: that should be good enough to make an organist enjoy playing it.
$SOFTNAME: It is a software synthesiser optimised for this job, with
$SOFTNAME: possibly hundreds of controls for each stop, that enable the
$SOFTNAME: user to "voice" his instrument.
$SOFTNAME: 
$SOFTNAME: 
$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 \
	$CWD/${SRCFILES[1]} \
	$CWD/${SRCFILES[2]} \
	$PKGDIR/usr/src/$SOFTNAME-$VERSION/

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


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


fakeroot /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
