#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# http://peazip.sourceforge.net
# Packager Andrea Sciucca ( gohanz at infinito.it)
# http://www.slacky.it
#
#
# Exit on most errors
set -e

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=peazip
PKG=$TMP/package/$NAME
VERSION=3.1
ARCH=${ARCH:-i686}
BUILD=1as

SOURCE=http://downloads.sourceforge.net/peazip/$NAME'_'portable-$VERSION.LINUX.GTK2.tar.gz

if [ ! -e $NAME'_'portable-$VERSION.LINUX.GTK2.tar.gz ]; then
wget -c $SOURCE
fi


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

cd $TMP

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"

mkdir -p $PKG/usr/lib

( cd $PKG/usr/lib
  tar xvzf $CWD/$NAME'_'portable-$VERSION.LINUX.GTK2.tar.gz
  mv $NAME'_'portable-$VERSION.LINUX.GTK2 peazip  )

# Install the executable file.
mkdir -p $PKG/usr/bin
cat <<EOF >$PKG/usr/bin/peazip
#!/bin/sh
exec /usr/lib/peazip/peazip
EOF
chmod +x $PKG/usr/bin/peazip

mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/peazip.png > $PKG/usr/share/pixmaps/peazip.png
mv $PKG/usr/lib/peazip/FreeDesktop_integration/peazip.desktop $PKG/usr/share/applications

mkdir -p $PKG/usr/share/kde4/services
mv $PKG/usr/lib/peazip/FreeDesktop_integration/{peazipadd,peazipext,peazipextfolder,peazipexthere,peazipopen}.desktop $PKG/usr/share/kde4/services

mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
mv $PKG/usr/lib/peazip/*.txt   $PKG/usr/doc/$NAME-$VERSION 

# Remove Nautilus script.
rm -r $PKG/usr/lib/peazip/FreeDesktop_integration

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

	echo "if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database /usr/share/mime &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	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

cd $PKG
chmod -R u+w,go+r-w,a-s .
chown -R root:root .
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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