#!/bin/bash

# Slackware build script for GrafX2

# Copyright 2018 Jorge Giner Cordero
# 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.

# Modified by B. Watson, 20230421. I'm not taking over this build;
# I'm updating it at the request of the maintainer.
# - updated for v2.8.3091.
# - add gtk-update-icon-cache to doinst.sh.
# - autodetect lua (allow LUA=no to override).
# - make the slack-desc show whether or not lua scripting is built in.
# - don't include the sample lua scripts if lua is disabled.
# - move 32x32 bitmap icon to correct dir, install 48x48 icon.
# - install man pages.
# - make the .desktop file actually work (name executable 'grafx2').

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

PRGNAM=grafx2
VERSION=${VERSION:-2.8.3091}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

# 20230421 bkw: depending on whether content-disposition is respected
# or not, the file gets saved with one or the other of these 2 names.
SRC1=65
SRC2=$PRGNAM-$VERSION-HEAD-src.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}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# 20230421 bkw: Assume the user wants lua scripting if lua is
# installed... unless he says LUA=no.
if [ -x /usr/bin/lua ] && [ "${LUA:-yes}" = "yes" ]; then
  LUA=""
  WITHLUA="WITH"
else
  LUA="NOLUA=1"
  WITHLUA="WITHOUT"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
tar xvf $CWD/$SRC1 || tar xvf $CWD/$SRC2
cd $PRGNAM
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 {} \+

CFLAGS="$SLKCFLAGS" \
  make -C src install V=1 PREFIX=/usr DESTDIR=$PKG $LUA

# 20230421 bkw: shut sbopkglint up.
echo "Placeholder; ignore me." > $PKG/usr/share/grafx2/scripts/libs/.emptydir.ok

# 20230421 bkw: if we built the package without lua, don't include the
# directory full of (now-useless) lua scripts.
[ "$WITHLUA" = "WITHOUT" ] && rm -rf $PKG/usr/share/$PRGNAM/scripts

# 20230421 bkw: The Makefile installs it as "grafx2-sdl", but the
# .desktop file and man pages call it plain "grafx2". Symlink it so
# either name is correct.
strip $PKG/usr/bin/$PRGNAM-sdl
ln -s $PRGNAM-sdl $PKG/usr/bin/$PRGNAM

# 20230421 bkw: The 32x32 icon gets installed in the wrong place.
# There's also a 48x48 icon hidden away, might as well install it, too.
# Also link it to pixmap dir, for old-style WMs.
HICOLOR=$PKG/usr/share/icons/hicolor
mkdir -p $HICOLOR/{32x32,48x48}/apps
mv $HICOLOR/scalable/apps/*.xpm $HICOLOR/32x32/apps
cat misc/morphos/$PRGNAM.info > $HICOLOR/48x48/apps/$PRGNAM.png
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# 20230421 bkw: 'make install' doesn't install the man pages. Looks
# like we have English and French ones. The .fr page is actually in
# ASCII (not ISO-8859-1 nor UTF-8), so it goes in /usr/man/fr.
mkdir -p $PKG/usr/man/man1 $PKG/usr/man/fr/man1
gzip -9c < misc/unix/grafx2.1 > $PKG/usr/man/man1/grafx2.1.gz
gzip -9c < misc/unix/grafx2.fr.1 > $PKG/usr/man/fr/man1/grafx2.1.gz

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
rm -f doc/COMPILING.txt   # don't need in package
cp -a doc/*.txt $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@WITHLUA@,$WITHLUA," < $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