#!/bin/sh
# Heavily based on the Slackware 13.37 SlackBuild
#http://lescienze500.wordpress.com
# Packager Michele Petrecca ( michelinux at alice.it)
# http://www.slacky.eu
#
#
# 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.
#
# Required: pygtksourceview
#
# Exit on most errors
set -e


# Set variables
CWD=`pwd`
OUTPUT=${OUTPUT:-$CWD}
PKGNAME=lescienze500
TMP=${TMP:-/tmp/txz/$PKGNAME}
PKG=$TMP/package
VERSION=${VERSION:-1.3.6}
ARCH=${ARCH:-i486}
JOBS=${JOBS:--j2}
BUILD=${BUILD:-1}
TAG=${TAG:-sl}


# Download sorce tarball if still not present
SOURCE=https://github.com/downloads/simon-r/LeScienze500/$PKGNAME'-'$VERSION.tar.gz/download
if [ ! -e $PKGNAME'-'$VERSION.tar.gz ]; then
echo "+--------------------+"
echo "| Downloading source |"
echo "+--------------------+"
wget -c $SOURCE
fi


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


# Create building directory
rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
tar xzvf $CWD/$PKGNAME'-'$VERSION.tar.gz
cd $PKGNAME'-'$VERSION


# Adjust permissions and ownerships
chown -R root:root .
chmod -R u+w,go+r-w,a-s .


# Configure
qmake -unix PREFIX=/usr QMAKE_CFLAGS="$SLKCFLAGS" QMAKE_CXXFLAGS="$SLKCFLAGS" -o Makefile LeScienze500.pro


# Compile the application and install it into the $PKG directory
make

mkdir -p $PKG/usr/bin
install -m 755 -p "LeScienze500" "$PKG/usr/bin/LeScienze500"

# Strip binaries
strip --strip-unneeded $PKG/usr/bin/*


# Copy program documentation into the package
# Also, include the SlackBuild script in the documentation directory
mkdir -p $PKG/usr/doc/$PKGNAME-$VERSION
cp -a \
  LEGGIMI.txt License_gpl.txt \
  $PKG/usr/doc/$PKGNAME-$VERSION
cat $CWD/$PKGNAME.SlackBuild > $PKG/usr/doc/$PKGNAME-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/usr/share/icons/hicolor/
cp -f -r src/icons/* $PKG/usr/share/icons/hicolor/

mkdir -p $PKG/usr/share/applications
install -m 644 -p src/lescienze500.desktop $PKG/usr/share/applications/

# Copy the slack-desc (and a custom doinst.sh if necessary) into ./install
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Find and print dependencies into slack-required file
cd $PKG
chown -R root:root $PKG
if [ -x /usr/bin/requiredbuilder ];then
requiredbuilder -y -v -s  $CWD $PKG
fi
# Create txz package
/sbin/makepkg -l y -c n $CWD/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}


#Clean up file temp
if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi