#!/bin/sh

# Heavily based on the Slackware 13.37 SlackBuild
# Written by Loris Vincenzi (http://www.slacky.eu)
# Last build from Loris Vincenzi (http://www.slacky.eu)

# Slackware build script for worker
# Official Site: http://avf.sourceforge.net/

# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

NAME=worker
TARGET=i486
VERSION=2.18.1
ARCH=${ARCH:-i486}
BUILD=1sl

CWD=`pwd`
TMP=${TMP:-/tmp/txz/$NAME}
PKG=$TMP/package-$NAME
rm -rf $PKG
mkdir -p $PKG

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

SOURCE=http://www.boomerangsworld.de/cms/worker/downloads/$NAME-$VERSION.tar.bz2
SOURCE_DOC=http://www.boomerangsworld.de/cms/worker/downloads/$NAME-2.10.0.2-doc.tar.bz2

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

if [ ! -e $NAME-2.10.0.2-doc.tar.bz2 ]; then
   wget -c $SOURCE_DOC
fi

cd $TMP
rm -rf $NAME-$VERSION
tar xjvf $CWD/$NAME-$VERSION.tar.bz2 || exit 1
tar xjvf $CWD/$NAME-2.10.0.2-doc.tar.bz2 || exit 1
cd $NAME-$VERSION
chown -R root:root .
chmod -R u+rw,go+r-w,a-s .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
 --prefix=/usr \
 --sysconfdir=/etc \
 --localstatedir=/var \
 --mandir=/usr/man \
 --program-prefix= \
 --program-suffix= \
 --disable-debug \
 --build=$TARGET-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1
strip $PKG/usr/bin/*
find $PKG/usr/man -type f -exec gzip -9 {} \;
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS ChangeLog INSTALL NEWS README README_LARGEFILES THANKS \
 $PKG/usr/doc/$NAME-$VERSION

# kde menu-entry
mkdir -p $PKG/usr/share/applications
cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Comment=Two-pane Filemanager
Encoding=UTF-8
Exec=worker
GenericName=File Manager
Icon=/usr/share/pixmaps/WorkerIcon48.xpm
MimeType=
Name=Worker
Name[en_US]=Worker
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
Categories=Application;FileManager;Utility;Qt;KDE;
X-DCOP-ServiceType=
X-KDE-SubstituteUID=
X-KDE-Username=
X-KDE-AuthorizeAction=
EOF

(cd $TMP/worker-2.10.0.2-doc
./configure --prefix=/usr && make && make install DESTDIR=$PKG
mkdir -p $PKG/usr/doc/$NAME-$VERSION/manual
mv $PKG/usr/share/doc/worker/{Deutsch,English,pics,index.html} $PKG/usr/doc/$NAME-$VERSION/manual
rm -rf $PKG/usr/share/doc
chown -R root:root $PKG/usr/doc/$NAME-$VERSION/manual
find $PKG/usr/doc -type f | xargs -r chmod 644
find $PKG/usr/doc -type d | xargs -r chmod 755
)

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 -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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