#!/bin/bash

# Slackware build script for vectoroids

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

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

# 20250614 bkw: update for v1.1.2. this means SDL2, which lacks .s3m
# (mod) support on Slackware so I had to convert decision.s3m to an
# .ogg (with milkytracker and oggenc), then patch the code to load
# the .ogg.

# 20230107 bkw: BUILD=3, add doinst.sh, fix man page permission.
# 20211016 bkw: BUILD=2
# - add icons and .desktop

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

PRGNAM=vectoroids
VERSION=${VERSION:-1.1.2}
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"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -e

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

# 20250614 bkw: the git history is included in the tarball, and it's
# about 2.5x the size of the actual source. Don't waste time/space
# extracting it.
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz --exclude='*/.git/*'

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 {} +

# Get rid of cruft accidentally left in the distribution tarball
rm -rf data/images/.xvpics

# 20250614 bkw: PV never responded to my request to add mod support
# to 15.0's SDL2, and at this late date I wouldn't expect him to.
# Use an .ogg version of the soundtrack.
sed -i '/mus_game_name/s,s3m,ogg,' $PRGNAM.c

# Can't just override CFLAGS...
sed -i -e "s/-O2/$SLKCFLAGS/" Makefile

make PREFIX=/usr BIN_PREFIX=/usr/games

mkdir -p $PKG/usr $PKG/usr/games
strip $PRGNAM
make install PREFIX=$PKG/usr BIN_PREFIX=$PKG/usr/games

chmod 644 $PKG/usr/man/man6/*.6
gzip -9 $PKG/usr/man/man6/*.6

# 20250614 bkw: put the converted music in the package. Thought about
# deleting the original .s3m, but it's small, and it's the "source" for
# the .ogg.
cat $CWD/decision.ogg > $PKG/usr/share/vectoroids/music/decision.ogg

# Upstream's icon.png is barely visible due to being vector
# drawings on a transparent background. I made a hopefully nicer
# icon like so:
# convert -resize 64x64 /usr/share/vectoroids/images/redspot-e.bmp 1.png
# composite /usr/share/vectoroids/images/icon.png -geometry +12+12 1.png 64.png
# convert -resize 48x48 64.png 48.png
# convert -resize 32x32 64.png 32.png
# convert -resize 16x16 64.png 16.png
for i in $CWD/icons/*.png; do
  px=$( basename $i | cut -d. -f1 )
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  cat $i > $dir/$PRGNAM.png
done

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

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

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp *.txt $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
