#!/bin/bash

# Slackware build script for dopewars

# Copyright 2013 Fernando Giannasi.
# Modified and now maintained by B. Watson <urchlay@slackware.uk>.
# This work is free. You can redistribute it and/or modify it under
# the terms of the WTFPL, Version 2, as published by Sam Hocevar.
# See http://www.wtfpl.net/txt/copying/

# Modified by the SlackBuilds.org project

# 20260711 bkw:
# - take over maintenance.
# - update for v1.6.2.
# - move binary to /usr/games.

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

PRGNAM="dopewars"
VERSION=${VERSION:-1.6.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"
  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 

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 . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# 20260711 bkw: fix desktop-file-validate warnings.
cat $CWD/$PRGNAM.desktop > $PRGNAM.desktop

# Configure
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --bindir=/usr/games \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --mandir=/usr/man

make
make install-strip DESTDIR=$PKG
strip $PKG/usr/lib*/$PRGNAM/*.so # install-strip misses these
gzip -9 $PKG/usr/man/man*/*

# move the menu entry and the docs
PKGAPP=$PKG/usr/share/applications
mkdir -p $PKGAPP
mv $PKG/usr/share/gnome/apps/Games/*.desktop $PKGAPP/$PRGNAM.desktop
rm -rf $PKG/usr/share/gnome

# Add documentation
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
cp -a NEWS* TODO* README* AUTHOR* ChangeLog* COPYING $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# Copy the slack-desc and doisnt.sh
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Build the package
cd "$PKG"
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
