#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild  
#
# Slackware build script for qtcurve
#
# Copyright 2008,2009 Corrado Franco (http://conraid.net)
# All rights reserved.
#
# 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.

# Exit on most errors
set -e

# Set variables:
CWD=$(pwd)

PRGNAME=${PRGNAME:-qtcurve}
VERSION=${VERSION:-0.68.1}
VERSION2=${VERSION2:-0.68.1}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-cf}
TMP=${TMP:-/tmp/pkg}
PKG=$TMP/package-$PRGNAME
JOBS=${JOBS:-2}
SOURCE1="http://home.freeuk.com/cpdrummond/QtCurve-Gtk2-${VERSION2}.tar.bz2"
SOURCE2="http://home.freeuk.com/cpdrummond/QtCurve-KDE4-${VERSION}.tar.bz2"
NAME1=$(basename $SOURCE1 .tar.bz2)
NAME2=$(basename $SOURCE2 .tar.bz2)

DOCS="$PKG/usr/doc/$PRGNAME-$VERSION"

# Insert document files in this variable
DOCFILES="AUTHORS COPYING ChangeLog"

# Set compiling FLAGS
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  SLKLDFLAGS=""; LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -pipe -fomit-frame-pointer"
  SLKLDFLAGS=""; LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC -pipe -fomit-frame-pointer"
  SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
fi

# Download source, if not exist
if ! [ -e $(basename $SOURCE1) ]; then
    wget $SOURCE1
fi

if ! [ -e $(basename $SOURCE2) ]; then
    wget $SOURCE2
fi

# Extract source
mkdir -p $PKG
cd $TMP
rm -rf QtCurve-* $PKG
find $CWD -name 'QtCurve*' -exec tar xjvf {} \;

mkdir -p ${DOCS}

# Compiling for GTK2
cd $NAME1
chown -R root:root .
chmod -R u+w,go+r-w,a-s .
cat README > $DOCS/README.gtk2
mkdir build
cd build
cmake \
 -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
 -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
 -DCMAKE_LD_FLAGS:STRING="$SLKLDFLAGS" \
 -DCMAKE_INSTALL_PREFIX=/usr \
 -DMAN_INSTALL_DIR=/usr/man \
 -DSYSCONF_INSTALL_DIR=/etc \
 -DQTC_MODIFY_MOZILLA=false \
 ..
make -j $JOBS VERBOSE=1
make install/strip VERBOSE=1 DESTDIR=$PKG
cd ..

# Compiling for KDE3
cd $TMP
cd $NAME2
chown -R root:root .
chmod -R u+w,go+r-w,a-s .
cat README > $DOCS/README.kde4
mkdir build
cd build
cmake \
 -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
 -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
 -DCMAKE_LD_FLAGS:STRING="$SLKLDFLAGS" \
 -DCMAKE_INSTALL_PREFIX=/usr \
 -DMAN_INSTALL_DIR=/usr/man \
 -DSYSCONF_INSTALL_DIR=/etc \
 ..
make -j $JOBS VERBOSE=1
make install/strip VERBOSE=1 DESTDIR=$PKG
cd ..

# Add a documentation directory:
for i in ${DOCFILES}; do
    if [ -e ${i} ]; then
        if ! [ -L ${i} ]; then
            cp -a ${i} ${DOCS}
        else
            cp -LRp ${i} ${DOCS}
        fi
    fi;
done

# Add a slackbuild script:
cat $CWD/$PRGNAME.SlackBuild > $DOCS/$PRGNAME.SlackBuild

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

# Create slack-required file:
if [ -x /usr/bin/requiredbuilder ]; then
    requiredbuilder -v -y -s $CWD $PKG
fi

# Create package:
cd $PKG
/sbin/makepkg -l y -c n $CWD/$PRGNAME-$VERSION-$ARCH-${BUILD}${TAG}.txz

# Clean file, if option is set:
if [ "$1" = "--cleanup" ]; then
    rm -rf $TMP/$PRGNAME-$VERSION
    rm -rf $PKG
fi
