#!/bin/bash

# Slackware build script for paranoia

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

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

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

PRGNAM=paranoia
VERSION=${VERSION:-1.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAME=bsd-games.src

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"
else
  SLKCFLAGS="-O2"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
tar xvf $CWD/$SRCNAME-$VERSION.tar.gz --strip-components=2 games/paranoia
chown -R root:root .
chmod 644 *

# Don't bother with the Makefile, this is a single source file,
# and it'd be more work to beat the Makefile into shape than to just
# compile with our options.

# The source is K&R C, without ANSI prototypes. Forcing these includes
# at least gives us prototypes for stuff like printf() and srand().
# We still get a boatload of warnings, but the game runs correctly.
INCS="-include stdio.h -include stdlib.h -include time.h"

# Silence unknown escape seq warning.
sed -i 's,\\`,`,g' $PRGNAM.c

# Build binary stripped.
SLKCFLAGS+=" -Wl,-s"

mkdir -p $PKG/usr/games
set -x
${CC:-gcc} $SLKCFLAGS $INCS -o $PKG/usr/games/$PRGNAM $PRGNAM.c
set +x

# .desktop and icon created by SlackBuild author. The icon's a chunk
# of the cover of the original Paranoia pen-and-paper RPG.
mkdir -p $PKG/usr/share/icons/hicolor/32x32/apps $PKG/usr/share/{pixmaps,applications}
cat $CWD/$PRGNAM.png > $PKG/usr/share/icons/hicolor/32x32/apps/$PRGNAM.png
ln -s ../icons/hicolor/32x32/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

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