#!/bin/sh

# Slackware build script for qbittorrent 1.3.3

PRGNAM=qbittorrent
VERSION=${VERSION:-1.3.3}
ARC=${ARC:-gz}
TAGS=network,
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-nu}
CWD=$(pwd)
TMP=${TMP:-/tmp/NomerUNO/$PRGNAM-$VERSION}
PKG=$TMP/FakeInstall
AUTHOR=${AUTHOR:-NomerUNO}
EMAIL=${EMAIL:-sorry@email.hide}

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 -fPIC"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.${ARC} || exit 1
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --qtdir=/usr/lib/qt4

make
mkdir -p $PKG/usr/bin
cp src/qbittorrent $PKG/usr/bin/
mkdir -p $PKG/usr/man/man1
cp doc/qbittorrent.1 $PKG/usr/man/man1/
mkdir -p $PKG/usr/share/applications
cp src/Icons/qBittorrent.desktop $PKG/usr/share/applications/
mkdir -p $PKG/usr/share/icons/hicolor/16x16/apps/
mkdir -p $PKG/usr/share/icons/hicolor/22x22/apps/
mkdir -p $PKG/usr/share/icons/hicolor/24x24/apps/
mkdir -p $PKG/usr/share/icons/hicolor/32x32/apps/
mkdir -p $PKG/usr/share/icons/hicolor/36x36/apps/
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps/
mkdir -p $PKG/usr/share/icons/hicolor/64x64/apps/
mkdir -p $PKG/usr/share/icons/hicolor/72x72/apps/
mkdir -p $PKG/usr/share/icons/hicolor/96x96/apps/
mkdir -p $PKG/usr/share/icons/hicolor/128x128/apps/
mkdir -p $PKG/usr/share/icons/hicolor/192x192/apps/
cp src/menuicons/16x16/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/16x16/apps/
cp src/menuicons/22x22/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/22x22/apps/
cp src/menuicons/24x24/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/24x24/apps/
cp src/menuicons/32x32/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/32x32/apps/
cp src/menuicons/36x36/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/36x36/apps/
cp src/menuicons/48x48/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/48x48/apps/
cp src/menuicons/64x64/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/64x64/apps/
cp src/menuicons/72x72/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/72x72/apps/
cp src/menuicons/96x96/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/96x96/apps/
cp src/menuicons/128x128/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/128x128/apps/
cp src/menuicons/192x192/apps/qbittorrent.png $PKG/usr/share/icons/hicolor/192x192/apps/

( 
  cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null
)

( 
  cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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

cd $PKG
/sbin/makepkg -l y -c n $CWD/$PRGNAM-$VERSION$SVN-$ARCH-$BUILD$TAG.tgz || exit 1
mpkg convert $CWD/$PRGNAM-$VERSION$SVN-$ARCH-$BUILD$TAG.tgz || exit 1
mpkg gendeps2 $CWD/$PRGNAM-$VERSION$SVN-$ARCH-$BUILD$TAG.tgz || exit 1

#delete build garbage
rm -rf /tmp/NomerUNO /tmp/mpkg*