#!/bin/bash

# Slackware build script for mame

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

# Adapted from Erik W. Hanson's mame.SlackBuild for older mame versions,
# which was originally adapted from sdlmame.SlackBuild by B. Watson.

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

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

PRGNAM=mame
VERSION=${VERSION:-0.287}
BUILD=${BUILD:-1}
TAG=${TAG:-_lngn}
PKGTYPE=${PKGTYPE:-txz}

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/lngn}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# NB nonstandard flags here. Upstream defaults to -O3, so we'll leave
# that as-is. Only the arch-specific stuff goes here.
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-march=i586 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-fPIC"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-fPIC"
else
  SLKCFLAGS=""
fi

# Source extracts to e.g. mame-mame0175/
SHORTNAME=${PRGNAM}${VERSION/./}
DIRNAME=$PRGNAM-$SHORTNAME

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $DIRNAME
tar xvf $CWD/$DIRNAME.tar.gz
cd $DIRNAME
chown -R root:root .

# 20181201 bkw: Urchlay's personal template:
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# OK, building modern mame is a bit of a PITA. It uses genie (written
# in lua, a fork of premake), but you don't get to run genie directly,
# you got a main makefile that builds lua, then builds genie,
# then runs genie with arguments based on the options in the main
# makefile... and genie generates a bunch of makefiles, then runs make
# on those. Also, it uses python3 to convert XML layout files to
# C++ code.

# During the Slackware 14.2 cycle, I used to do this:
# LDOPTS="-fuse-ld=gold -static-libstdc++ -static-libgcc" \
# This allowed installing Pat's unsupported gcc9 package,
# building mame with it, then restoring the original gcc
# version and mame would still run.

# Where possible, use system libraries instead of building the ones
# bundled with the mame source. However, SBo's lua is (still!) too old
# for mame.
#USE_SYSTEM_LIB_LUA=1 \

# Upstream dropped sqlite as a dependency, this no longer matters.
#USE_SYSTEM_LIB_SQLITE3=1 \

# Not going to USE_SYSTEM_LIB_PORTAUDIO or USE_SYSTEM_LIB_PORTMIDI,
# the bundled versions are fine and I don't want a mile-long dependency
# tree. Using an external portmidi dep would make mame require Oracle's
# JDK! In case you're wondering, only the C code in the bundled portmidi
# is built (no Java needed).

# This is purely cosmetic, and can't be set on the make command line.
sed -i 's,ubuntu-intrepid,slackware,g' scripts/genie.lua
sed -i 's/DISTRO := generic/DISTRO := slackware/' makefile

# SDL_INI_PATH needs to contain a $ character. I haven't figured out
# how (or even if) I can pass this through multiple layers of make
# calling genie which calls make again, so screw it:
sed -i 's,\.;ini,/etc,' src/osd/sdl/sdlmain.cpp

# Tried several variants of this, none work, ended up using the sed
# stuff above.
#SDL_INI_PATH='$$HOME/.mame;/etc' \


# Build option notes:

# Having ARCH set in the env will break the build, hence ARCH="".

# Using ld.gold is *much* faster, and I see no disadvantage to it.

# The point of the OVERRIDE_CC and friends is to make the build use $PATH
# to find gcc (so ccache will be found if it's using the symlink method).

# 20211109 bkw: N.B. upstream requires python 3, but they default to
# /usr/bin/python as the executable, so I still have to override it.
# The -S means the same thing in python3 as it did it 2: disable use
# of site packages. We needed it with py2 because otherwise the build
# would fail if PyXML happened to be installed. Probably don't need it
# with py3, but it won't hurt anything either.


# 20211111 bkw: undocumented option, build a mame that only supports
# pac-man and its clones. used for testing only.
if [ "${PACONLY:-no}" = "yes" ]; then
  PACOPT="SOURCES=src/mame/drivers/pacman.cpp"
fi

make -j$(expr $(nproc) + 1) \
     USE_SYSTEM_LIB_EXPAT=1 \
     USE_SYSTEM_LIB_ZLIB=1 \
     USE_SYSTEM_LIB_JPEG=1 \
     USE_SYSTEM_LIB_FLAC=1 \
     PYTHON_EXECUTABLE="/usr/bin/python3 -S" \
     OPT_FLAGS="$SLKCFLAGS" \
     LDOPTS="-fuse-ld=mold" \
     PTR64=1 \
     PRECOMPILE=1 \
     VERBOSE=1 \
     NOWERROR=1 \
     TOOLS=1 \
     USE_WAYLAND=1

# No 'make install' target, do it manually.
mkdir -p $PKG/usr/games $PKG/etc $PKG/usr/man/man6 $PKG/usr/man/man1 \
         $PKG/usr/share/applications $PKG/usr/share/pixmaps

# Deal with upstream's executable-naming silliness.
for i in tiny 32 64; do
  [ -e ${PRGNAM}$i ] && mv ${PRGNAM}$i $PRGNAM
done

# .desktop borrowed from Ludovic Lechapt's Debian package.
cat $CWD/desktop/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# Icon extracted from src/mame/osd/windows/mame/mame.ico with icotool.
for px in 16 32 48 64 128; do
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  convert -resize $size $CWD/desktop/$PRGNAM.png $dir/$PRGNAM.png
done

ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

cat $CWD/$PRGNAM.ini > $PKG/etc/$PRGNAM.ini.new

# This one *must* exist.
install -s -m0755 -oroot -groot $PRGNAM $PKG/usr/games/

# 20211110 bkw: removed src2html (no longer exists) and renamed split
# to mame-split since we have a split command in coreutils already.
# Also, in case upstream drops another of these utilities, check for
# existence before trying to install (because it takes hours to
# compile).
# Install the tools:
for i in castool chdman floptool imgtool jedutil \
         ldresample ldverify nltool nlwav pngcmp \
         regrep romcmp srcclean unidasm
do
  [ -e $i ] && install -s -m0755 -oroot -groot $i $PKG/usr/games/
done

[ -e split ] && install -s -m0755 -oroot -groot $i $PKG/usr/games/$PRGNAM-split

ln -s $PRGNAM $PKG/usr/games/mess

# Install the man pages. mame and mess still have separate man
# pages, though the binaries are combined now.
install -m0644 -oroot -groot docs/man/*.1 $PKG/usr/man/man1
install -m0644 -oroot -groot docs/man/*.6 $PKG/usr/man/man6
gzip -9 $PKG/usr/man/man?/*.?

# Create empty dirs for the user to populate with ROMs and such.
SHAREDIR=$PKG/usr/share/games/$PRGNAM
mkdir -p $SHAREDIR/{roms,font,cheat}

# Install the support stuff.
cp -a artwork bgfx plugins samples hash ctrlr $SHAREDIR

# 20211110 bkw: also the translations (but not the .po files, those are
# the sources for the .mo files, which are what mame actually needs).
cp -p --parents language/*/*.mo $SHAREDIR

# CONTRIBUTING.md is a 1-byte placeholder, and we don't need a shell script.
rm -f docs/CONTRIBUTING.md docs/update.sh

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
# can't just "cp docs/* ..." because man/ is a dir, set -e kills the script
# 20240330 bkw: docs/swlist is gone in 0.264.
cp docs/L* docs/*.* $PKGDOC
cp -a docs/legal $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# in case someone finds these useful...
cp -a ini/examples ini/presets $PKGDOC

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

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