#!/bin/sh

# Slackware build script for megamario

# Written by B. Watson (yalhcru@gmail.com)

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

PRGNAM=megamario
VERSION=${VERSION:-1.7}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

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

CWD=$(pwd)
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

ZIPFILE="MegaMario_v${VERSION}_full.zip"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION

# zip file doesn't contain a top-level directory, so:
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

# don't even extract the windows binaries or trash files
unzip $CWD/$ZIPFILE -x '*.exe' '*.dll' \
     log.txt data/save.sav data/levels/1/1 data/levels/11/mai

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# missing includes
patch -p1 < $CWD/patches/compilefix.diff

# look for mp3music in DATADIR, not current dir
patch -p1 < $CWD/patches/usemp3music.diff

# write log.txt to ~/.megamario/, not current dir
patch -p1 < $CWD/patches/logfile.diff

# bin is hardcoded
sed -i 's,\<bin\>,games,' Makefile

# default to windowed, since there's no GUI for changing resolution or
# fullscreen/windowed. it's 2016, everyone has a 1080p monitor, but the
# default res is 640x480. in SDL1 this means huge black borders. this
# is only the default, users can change it by editing the ini file.
sed -i '/getboolean.*Mario:FULLSCREEN/s,1,0,' src/main.cpp

# 'make install' doesn't create the data dir
DATADIR=/usr/share/games/$PRGNAM
mkdir -p $PKG/$DATADIR

make CFLAGS="$SLKCFLAGS" \
     DATADIR="$DATADIR" \
     PREFIX=/usr
strip $PRGNAM
make install \
     PREFIX=$PKG/usr \
     DATADIR="$PKG/$DATADIR"

# 'make install' doesn't install the high-quality mp3 music
cp -a mp3music $PKG/$DATADIR

# upstream's .desktop fails to validate, use our own fixed one.
# the icon is upstream's.
# not doing a man page as there are no command-line options.
mkdir -p $PKG/usr/share/applications $PKG/usr/share/pixmaps
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop
cat $PRGNAM.png > $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a *.txt $PKG/usr/doc/$PRGNAM-$VERSION
# get rid of DOS line endings:
sed -i 's,\r,,' $PKG/usr/doc/$PRGNAM-$VERSION/*.txt
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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:-tgz}
