#!/bin/bash

# Slackware build script for freedoom

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

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

# 20191127 bkw: updated for v0.12.1, include shiny new PDF manual in pkg
# 20170721 bkw: updated for v0.11.3
# 20170324 bkw: updated for v0.11.2
# 20170301 bkw: updated for v0.11.1

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

PRGNAM=freedoom
VERSION=${VERSION:-0.12.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ARCH=noarch

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}

set -e

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

mkdir -p $PKG/usr/share/games/doom $PKG/usr/doc/$PRGNAM-$VERSION

for zipfile in $PRGNAM freedm; do
  unzip $CWD/$zipfile-$VERSION.zip
  cd $zipfile-$VERSION
  chown -R root:root .
  chmod 644 *
  cp *.wad $PKG/usr/share/games/doom
  cd -
done

# The docs are identical in both zipfiles, only keep one set.
cd $PRGNAM-$VERSION
cp -a COPYING* CREDITS* *.html $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $PRGNAM-manual.pdf $PKG/usr/doc/$PRGNAM-$VERSION 2>/dev/null || true
cd -

# Upstream recently renamed their wad files. The new names are less
# confusing (freedoom1.wad is Doom I, freedoom2.wad is Doom II). For now,
# we'll make symlinks to the old names, for compatibility with engines that
# don't yet know about the new names.
# zdoom will correctly find the files with these names, according to
# http://remilia.zdoom.org/wiki/IWAD

cd $PKG/usr/share/games/doom
ln -s freedoom1.wad freedoomu.wad
ln -s freedoom2.wad freedoom.wad

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$VERSION/README

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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