#!/bin/sh
# Heavily based on the Slackware 13.1 SlackBuild
# http://smile.tuxfamily.org
# Packager Andrea Sciucca Gohanz ( gohanz at infinito.it)
# http://www.slacky.eu



CWD=`pwd`
OUTPUT=${OUTPUT:-$CWD}
PKGNAME=smile
TMP=${TMP:-/tmp/txz/$PKGNAME}
PKG=$TMP/package
VERSION=1.0
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}
TAG=${TAG:-sl}
SOURCE=http://download.tuxfamily.org/smiletool/$PKGNAME-$VERSION.tar.gz

if [ ! -e $PKGNAME-$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i486"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST="i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"; CHOST="x86_64"
fi

rm -rf $PKG
mkdir -p $TMP $PKG 


cd $TMP
tar xvzf $CWD/$PKGNAME-$VERSION.tar.gz || exit 1

echo -e "\E[0;32m+------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild SMILE |\E[0;0m"
echo -e "\E[0;32m+------------------------+\E[0;0m"

cd $PKGNAME
chmod -R u+w,go+r-w,a-s .
chown -R root:root .

mkdir -p $PKG/usr/bin
cat << EOF > $PKG/usr/bin/smile
#!/bin/bash
cd /usr/share/smile
exec /usr/share/smile/smile
EOF

chmod 755 $PKG/usr/bin/*


# Configure:
qmake -unix PREFIX=/usr QMAKE_LIBS="$SLKLDFLAGS" QMAKE_CFLAGS="$SLKCFLAGS" QMAKE_CXXFLAGS="$SLKCFLAGS" -o Makefile smile.pro

# Build and install:
LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
make

install -m 755 -p -D  smile $PKG/usr/share/smile/smile 

cp -a smile_*.{qm,ts} $PKG/usr/share/smile
cp -a -r {BIB_ManSlide,Interface} $PKG/usr/share/smile

mkdir -p $PKG/usr/share/applications
cat << EOF > $PKG/usr/share/applications/$PKGNAME.desktop
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Smile
Name[it]=Smile
GenericName=Slideshow creator
GenericName[it]=Creatore di presentazioni
Comment=This is a program to simply create your own slideshow
Icon=/usr/share/pixmaps/logostart.png
Exec=smile
Terminal=false
StartupNotify=true
Categories=Qt;AudioVideo
EOF

install -m 644 -p -D  $TMP/$PKGNAME/Interface/Theme/logostart.png  $PKG/usr/share/pixmaps/logostart.png 

mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild/slack-desc
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/SlackBuild/$PKGNAME.SlackBuild


	echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/gtk-update-icon-cache ]; then
  /usr/bin/gtk-update-icon-cache  -f -q /usr/share/icons/hicolor &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh


strip --strip-unneeded $PKG/usr/share/smile/smile


cd $PKG
chown -R root:root $PKG
if [ -x "$(which requiredbuilder)" ]; then
requiredbuilder -y -v -c -b -s  $CWD $PKG
fi
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}
requiredbuilder -y -v -s $CWD $PKG


if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi

