#!/bin/bash

# Slackware build script for defendguin

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

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

# 20230819 bkw: update for v0.0.13, use better icon.
# 20230107 bkw: BUILD=2:
# - fix man page permission.
# - remove useless INSTALL from doc dir.

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

PRGNAM=defendguin
VERSION=${VERSION:-0.0.13}
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" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e

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

# Cruft removal:
rm -f data/images/{l2r.sh,loader/loading-orig.bmp.gz,*/*.sh}

# Fix path to data files in man page.
patch -p1 < $CWD/man.diff

# 20230819 bkw: use our flags, and don't install data +x.
sed -i -e "s,-Wall,& $SLKCFLAGS," \
       -e '/chmod /d' \
       Makefile

# 20230819 bkw: 'make install' should create these, but doesn't:
mkdir -p $PKG/usr/games $PKG/usr/share/games/$PRGNAM

# 20230819 bkw: N.B. DATA_PREFIX needs the trailing / here!
make DATA_PREFIX=/usr/share/games/$PRGNAM/
strip $PRGNAM
make install \
  PREFIX=$PKG/usr \
  BIN_PREFIX=$PKG/usr/games \
  DATA_PREFIX=$PKG/usr/share/games/$PRGNAM/ \
  MAN_PREFIX=$PKG/usr

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
rm -f docs/INSTALL.txt
cp docs/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# 20230819 bkw: icon is a modified data/images/boss/boss01.bmp
for i in $CWD/icons/*.png; do
  px="$( basename "$i" .png )"
  sz=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$sz/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

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
