#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://jtgzmanager.sourceforge.net
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.eu
# Depends on qt4

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-qtgzmanager
NAME=qtgzmanager
VERSION=1.0beta4
CHOST=i486
ARCH=${ARCH:-i686}
BUILD=2as

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

cd $TMP
mkdir $NAME
cd $NAME
tar xvzf $CWD/qtgzmanager-src-1.0beta4.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"

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 .

export PATH=/opt/qt4/bin:$PATH

sed -i s/"#QMAKE_CXXFLAGS_RELEASE = -mtune=i586"/"QMAKE_CXXFLAGS_RELEASE =\"-O2 -march=i486 -mtune=i686\""/ qtgzmanager.pro

qmake -unix -o Makefile qtgzmanager.pro

make || exit 1

mkdir -p $PKG/usr/bin

cp -a bin/qtgzmanager $PKG/usr/bin

mkdir -p $PKG/usr/doc/$NAME-$VERSION

cp -a LICENSE THANKS $PKG/usr/doc/$NAME-$VERSION

mkdir -p  $PKG/usr/share/applications

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=QTGZManager
GenericName=Slackware Package Manager
Comment=A simple Slackware TGZ management app
Icon=/usr/share/pixmaps/QTGZManager.png
TryExec=qtgzmanager
Exec=qtgzmanager
Terminal=false
Categories=Qt;Application;System
EOF

mkdir -p $PKG/usr/share/pixmaps

cp -a resources/images/QTGZManager.png $PKG/usr/share/pixmaps

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

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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