#!/bin/bash

# Slackware build script for quackle

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=quackle
VERSION=${VERSION:-1.0.4}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# qmake picks the correct CFLAGS, we don't need SLKCFLAGS.
# nothing goes in /usr/lib(64)?, so we don't need LIBDIRSUFFIX.

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# Note: there's CMakeLists.txt, but upstream's docs say to use qmake.
# Looking at the cmake stuff, it seems to be written for Mac and
# Windows only, not Linux.

for dir in . quackleio quacker; do
  ( cd $dir && qmake-qt5 && make )
done

# no 'make install'. it expects to find data/ in the current dir, so
# there's a wrapper script.
mkdir -p $PKG/usr/libexec/$PRGNAM $PKG/usr/games $PKG/usr/share/games/$PRGNAM
install -oroot -groot -m0755 $CWD/$PRGNAM.sh $PKG/usr/games/$PRGNAM
install -s quacker/Quackle $PKG/usr/libexec/$PRGNAM/$PRGNAM
find data -name CMakeLists.txt | xargs rm -f
cp -a data $PKG/usr/share/games/$PRGNAM

# .desktop file written by SlackBuild author.
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

for i in quacker/Images.xcassets/AppIcon.appiconset/*.png; do
  px="$( basename $i .png | cut -d- -f2)"
  sz="${px}x${px}"
  dir=$PKG/usr/share/icons/hicolor/$sz/apps
  mkdir -p $dir
  cp -a $i $dir/$PRGNAM.png
done
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/64x64/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# README.md is just the compile instructions, don't bother.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a LICENSE $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

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

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
