#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://www.cstr.ed.ac.uk/projects/festival/
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
# Depends from :
# Speech_tools.....(http://www.cstr.ed.ac.uk/projects/speech_tools/ )


CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/usr/share
fi
PKG=/tmp/package-festival
NAME=festival
VERSION=1.96-beta
ARCH=${ARCH:-i686}
BUILD=2as

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP

tar xvzf $CWD/$NAME-$VERSION.tar.gz #
tar xvzf $CWD/festlex_CMU.tar.gz #
tar xvzf $CWD/festlex_OALD.tar.gz #
tar xvzf $CWD/festlex_POSLEX.tar.gz #
tar xvzf $CWD/festvox_don.tar.gz
tar xvzf $CWD/festvox_kallpc16k.tar.gz #
tar xvzf $CWD/festvox_kedlpc16k.tar.gz
tar xvzf $CWD/festvox_rablpc16k.tar.gz
tar xvzf $CWD/festvox_cmu_us_awb_arctic_hts.tar.gz
tar xvzf $CWD/festvox_cmu_us_bdl_arctic_hts.tar.gz
tar xvzf $CWD/festvox_cmu_us_jmk_arctic_hts.tar.gz
tar xvzf $CWD/festvox_cmu_us_slt_arctic_hts.tar.gz

echo -e "\E[0;32m+-------------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+-------------------------------------+\E[0;0m"

cd $NAME

./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var \
--program-prefix="" \
--program-suffix=""

make


mkdir -p $PKG/etc/rc.d
cp -a $CWD/rc.festival $PKG/etc/rc.d

mkdir -p $PKG/var/log/festival

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
 ACKNOWLEDGMENTS COPYING INSTALL NEWS README  \
 $PKG/usr/doc/$NAME-$VERSION
cd $CWD
cp -a *.SlackBuild slack-desc text2wave text2pos saytime rc.festival festival_* festival $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/usr/bin

( cd $TMP/$NAME/src/main
cp -a festival $PKG/usr/bin/festival.bin
cp -a festival_client  $PKG/usr/bin/festival_client.bin )

cp -a $CWD/festival        $PKG/usr/bin
cp -a $CWD/festival_client $PKG/usr/bin
cp -a $CWD/text2wave       $PKG/usr/bin
cp -a $CWD/saytime         $PKG/usr/bin
cp -a $CWD/festival_server       $PKG/usr/bin
cp -a $CWD/festival_server_control        $PKG/usr/bin

( cd $TMP/$NAME/bin
cp -a festival_server festival_server_control $PKG/usr/bin )

mkdir -p $PKG/usr/share/festival/lib

cp -a -r $TMP/$NAME/lib/*       $PKG/usr/share/festival/lib
cp -a    $TMP/$NAME/src/lib/*   $PKG/usr/share/festival/lib

mkdir -p $PKG/usr/include/festival
cp -a  $TMP/$NAME/src/include/*    $PKG/usr/include/festival

mkdir  -p  $PKG/usr/man/man1  
( cd $TMP/$NAME/doc
 cp -a    festival.1  festival_client.1 $PKG/usr/man/man1
)

( cd $PKG
find -iname 'Makefile'            -exec rm -f {} \;
find -iname '.made'               -exec rm -f {} \;
)

( cd $PKG 
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

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

gzip -9 $PKG/usr/man/man1/*

chmod 755 $PKG/usr/bin/*

cd $PKG
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
chown -R root:root .
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-1.96_beta-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP/$NAME-$VERSION
 rm -rf $PKG
fi
