#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://k7z.sourceforge.net/7Z/Q7Z/
# Packager Michelino Chionchio - Thanks to www.slacky.eu
# Depends on libtar, libpuzzle

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-q7z
NAME=q7z
VERSION=0.7.1
TARGET=i486
ARCH=${ARCH:-i486}
BUILD=1mch

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then 
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
tar xvjf $CWD/Q7Z-$VERSION.tar.bz2
cd Q7Z/Build

make \
"CFLAGS=$SLKCFLAGS" \
"CXXFLAGS=$SLKCFLAGS" \
|| exit 1


mkdir -p $PKG/usr/share/$NAME-$VERSION
cp -r ../Desktop/ $PKG/usr/share/$NAME-$VERSION
cp -r ../Source/ $PKG/usr/share/$NAME-$VERSION
mkdir -p $PKG/usr/bin/
cat << EOF > $PKG/usr/bin/q7z
#!/bin/sh

cd /usr/share/q7z-0.7.1/Source && ./Q7Z.pyw
EOF
chmod 755 $PKG/usr/bin/q7z

mkdir -p $PKG/usr/share/apps/konqueror/servicemenus/
mkdir -p $PKG/usr/share/apps/dolphin/servicemenus/
cp -a ../Desktop/Context/*.desktop $PKG/usr/share/apps/konqueror/servicemenus/
cp -a ../Desktop/Context/*.desktop $PKG/usr/share/apps/dolphin/servicemenus/
mkdir -p $PKG/usr/share/applications/kde/
cp -a ../Desktop/KMenu/* $PKG/usr/share/applications/kde/
mkdir -p $PKG/usr/share/icons/hicolor/32x32/actions/
cp -a ../Source/Images/actions/ark* $PKG/usr/share/icons/hicolor/32x32/actions/
cp -a ../Source/Images/actions/endturn* $PKG/usr/share/icons/hicolor/32x32/actions/
mkdir -p $PKG/usr/share/icons/hicolor/32x32/apps/
cp -a $PKG/usr/share/$NAME-$VERSION/Source/Images/apps/Q7Z.png $PKG/usr/share/icons/hicolor/32x32/apps/
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a ../Doc/* $PKG/usr/doc/$NAME-$VERSION

cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-suggests > $PKG/install/slack-suggests

cd $PKG
chown -R root:root .
ADD="python >= 2.5.1-i486-1,qt4 >= 4.3.3-i686-1as,pyqt4 >= 4.3.3-i486-1as,tar >= 1.16.1-i486-1,p7zip >= 4.57-i686-1sl"
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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