#!/bin/sh

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

# Homepage: http://qsynth.sourceforge.net/qsynth-index.html


set -e


SOFTNAME=qsynth
VERSION=0.3.5
ZENVER=64
BUILD=1

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

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

SRCFILES=(
	$SOFTNAME-$VERSION.tar.gz
)

HOSTURL=http://zenwalk.pinguix.com/user-accounts/ejis/$CATEGORY/$SOFTNAME/$ZENVER
SRCURLS=(
	http://downloads.sourceforge.net/project/qsynth/qsynth%20%28stable%29/$VERSION
)

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

DOC=(
	$SOFTDIR/AUTHORS
	$SOFTDIR/ChangeLog
	$SOFTDIR/COPYING
	$SOFTDIR/INSTALL
	$SOFTDIR/README
	$SOFTDIR/README-OSX
	$SOFTDIR/TODO
	$SOFTDIR/TRANSLATORS
)

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


# Erase the previous build files

rm -rf $PKGFILE $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 \
	--prefix=/usr \
	--localstatedir=/var \
	--sysconfdir=/etc \
	--mandir=/usr/share/man \
	--infodir=/usr/info

cd $SOFTDIR/src
qmake -o $SOFTNAME.mak
cd ../

sed -i "s:i486:$MARCH:g" $SOFTDIR/src/$SOFTNAME.mak

make -j2
make DESTDIR=$PKGDIR install


rm -rvf $PKGDIR/usr/share/man

sed -i "s:/usr/share/pixmaps/qsynth.png:qsynth.png:g" \
	$PKGDIR/usr/share/applications/$SOFTNAME.desktop

mkdir -p $PKGDIR/usr/share/icons/hicolor/32x32/apps
mv -v $PKGDIR/usr/share/pixmaps/$SOFTNAME.png \
	$PKGDIR/usr/share/icons/hicolor/32x32/apps/
rm -rvf $PKGDIR/usr/share/pixmaps


# 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 - Qt GUI Interface for FluidSynth
$SOFTNAME:
$SOFTNAME: Qsynth is a fluidsynth GUI front-end application written in C++
$SOFTNAME: around the Qt4 toolkit using Qt Designer. Eventually it may evolve
$SOFTNAME: into a softsynth management application allowing the user to
$SOFTNAME: control and manage a variety of command line softsynth but for
$SOFTNAME: the moment it wraps the excellent FluidSynth. FluidSynth is a
$SOFTNAME: command line software synthesiser based on the Soundfont
$SOFTNAME: specification.
$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
		dotnewfiles=$(find $PKGDIR/etc -type f -print -exec \
			mv {} {}.new \;)
		cat << EOF > $PKGDIR/install/doinst.sh
dotnewfiles=(${dotnewfiles[@]})

for file in \${dotnewfiles[@]}; do
	if [ ! -f \$file ]; then
		mv \$file.new \$file
	fi
done
EOF
		sed -i "s:$PKGDIR/::g" $PKGDIR/install/doinst.sh
	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
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'
echo "$HOSTURL/build-$SOFTNAME.sh" > $SRC
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
