#!/bin/bash

# Slackware build script for jgrf

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

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

# Note: The Jolly Good stuff is a *joy* to package. Simple
# human-readable Makefile, with sane defaults which can easily be
# overridden (even CFLAGS). No need for patching[*] or sedding anything.
# My hat is off to Rupert Carmichael (the jgemu author).

# [*] OK, yes, I did include a patch in this build. It adds a few
# filename extensions to the list, e.g. .smd for Sega Genesis, since
# such files exist aplenty in the wild. Still, I didn't have to patch
# the code just to get it to build and install as desired, which is
# nice.

# I don't see any reason to package jg and jgrf separately; the one
# is useless without the other. So this build is for both of them.
# Right now the only existing frontend is jgrf; someday if there are
# other frontends, they can list REQUIRES="jollygood".

# I packaged all but one of the available cores: the gngeo core's
# README says it's pre-alpha quality, unsuitable for packaging.

# VERSION is the jgrf version. The jg version will be the latest
# at the time jgrf is updated. Possibly it will always be the same
# version number, but there's a separate APIVER in case that turns out
# to be wrong.

# 20250527 bkw: updated for v1.2.1 (APIVER still 1.0.0).
# 20230902 bkw: updated for v1.0.2 (APIVER still 1.0.0).
# 20230404 bkw: updated for v1.0.1 (APIVER still 1.0.0).
# 20230121 bkw: submitted v1.0.0.

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

PRGNAM=jollygood
SRCNAM=jgrf
VERSION=${VERSION:-1.2.1}
APINAM=jg
APIVER=${APIVER:-1.0.0}
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}

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
TOPDIR=$(pwd)
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
tar xvf $CWD/$APINAM-$APIVER.tar.gz
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 {} \+

LIBDIR=/usr/lib$LIBDIRSUFFIX
PKGLIB=$PKG/$LIBDIR
DOCDIR=/usr/doc/$PRGNAM-$VERSION
PKGDOC=$PKG/$DOCDIR

# First, jg (the API, which is just a bunch of headers):
cd $APINAM-$APIVER
make install \
  DESTDIR=$PKG \
  PREFIX=/usr \
  DATAROOTDIR=/usr/share \
  DOCDIR=$DOCDIR/$APINAM-$APIVER \
  INCLUDEDIR=/usr/include

# Now, build jgrf:
cd $TOPDIR/$SRCNAM-$VERSION

# USE_EXTERNAL_MD5=1 makes it link with Slackware's libcrypto (from
# openssl) instead of its own bundled copy. Can't see why this would
# be a problem.

# Do not use USE_EXTERNAL_MINIZ=1, we don't have a miniz package (we
# do have minizip, but it's not the same thing apparently). Plus, who
# needs an extra dependency?

# Add .smd .sra for Genesis/Megadrive, add .fig .swc for SNES. Ideally
# in some future version of jgrf, these will be found in a config file
# instead of hardcoded in the executable.
# 20250527 bkw: keep the old patch around in case someone wants to build
# the old 1.0.x versions. for now, anyway.
PATCHVER="$( echo $VERSION | cut -d. -f1,2 )"
patch -p1 < $CWD/more_extensions.diff.$PATCHVER

make install-strip \
  CFLAGS="$SLKCFLAGS" \
  CFLAGS_JG="-I $PKG/usr/include" \
  USE_EXTERNAL_MD5=1 \
  DESTDIR=$PKG \
  PREFIX=/usr \
  BINDIR=/usr/games \
  DATAROOTDIR=/usr/share \
  DOCDIR=$DOCDIR/$SRCNAM-$VERSION \
  LIBDIR=$LIBDIR \
  MANDIR=/usr/man

gzip -9 $PKG/usr/man/man*/*.?

# Don't want a svg icon in pixmaps, it should be a PNG.
rm -f $PKG/usr/share/pixmaps/jollygood.svg
ln -s ../icons/hicolor/48x48/apps/jollygood.png $PKG/usr/share/pixmaps/jollygood.png

# Not sure if these duplicate icons are used, replace with symlinks.
for i in 96 1024; do
  f=$PKG/usr/share/$PRGNAM/jgrf/${PRGNAM}$i.png
  rm $f
  ln -s ../../icons/hicolor/${i}x${i}/apps/$PRGNAM.png $f
done

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
