#!/bin/bash

# bsd-games.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 19-Dec-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export ARCH_CFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) export ARCH_CFLAGS="" ;;
   sparc)   export ARCH_CFLAGS="" ;;
   *)       export ARCH_CFLAGS="" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Modify installation path.  We use the standard Slackware patch but 
# change the packaging location to $PKG:
zcat $CWD/bsd-games.config.params.diff.gz | sed 's?/tmp/package-bsdgames?'$PKG'?g' | patch -p1 --backup --suffix=.orig || failpatch

# Configure:
./configure || failconfig

# Apply more patches:
zcat $CWD/bsd-games.ospeed.diff.gz | patch -p1 --backup --suffix=.orig      || failpatch
zcat $CWD/bsd-games.fortunepath.diff.gz | patch -p1 --backup --suffix=.orig || failpatch
zcat $CWD/bsd-games.fortlen.diff.gz | patch -p1 --backup --suffix=.orig     || failpatch
zcat $CWD/bsd-games.strfile.8.diff.gz | patch -p1 --backup --suffix=.orig   || failpatch
zcat $CWD/bsd-games.pom.diff.gz | patch -p1 --verbose                       || failpatch

# Build:
make CC="cc $ARCH_CFLAGS" || failmake

# Fortune data:
# In the real bsd-games.SlackBuild, 'strfile' is called from $PATH
# Obviously if this is the first time that you have built bsd-games 
# (or don't have the package installed), then this will fail.
# Therefore, the bit below has been moved below the 'make' above:
cp fortune/datfiles/unamerican-o.real fortune/datfiles/unamerican-o.fake
cp fortune/datfiles/unamerican-o.real unamerican-o
( cd fortune/datfiles ; ../strfile/strfile unamerican-o unamerican-o.dat )

# Install:
# You didn't see this, right? See what? *nod*
mkdir -p /var/lib/bsdgames/phantasia
make install || failinstall
rm -rf /var/lib/bsdgames/phantasia

# Copy the docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a AUTHORS BUGS INSTALL NEWS PACKAGING README SECURITY THANKS \
      TODO YEAR2000 \
      $PKG/usr/doc/$PACKAGE-$VERSION
cp -a fortune/README $PKG/usr/doc/$PACKAGE-$VERSION/README.fortune
cp -a hunt/README $PKG/usr/doc/$PACKAGE-$VERSION/README.hunt
cp -a hunt/README.linux $PKG/usr/doc/$PACKAGE-$VERSION/README.linux.hunt
cp -a phantasia/README $PKG/usr/doc/$PACKAGE-$VERSION/README.phantasia
cp -a trek/README.linux $PKG/usr/doc/$PACKAGE-$VERSION/README.linux.trek

# Enter the package root:
cd $PKG

# Add profile scripts:
mkdir -p etc/profile.d
install -m755 $CWD/bsd-games-login-fortune.csh $CWD/bsd-games-login-fortune.sh \
              etc/profile.d

# Add some cool Linux related quotes:
tar zxvvf $CWD/fortunes-linuxcookie.tar.gz
# I'm not in favor of censorship of any kind.  If you are, you'll
# have to be the one to delete these:
# ( sw: These aren't in the real Slackware package )
#rm -f $PKG/usr/share/games/fortunes/limerick
#rm -f $PKG/usr/share/games/fortunes/limerick.dat
tar zxvvf $CWD/fortunes-o.tar.gz
# Run strfile on all fortune files:
( cd $PKG/usr/share/games/fortunes
  for file in *.dat ; do
    $PKG/usr/bin/strfile -r `basename $file .dat`
  done
  # These ones are rot13:
  for file in *-o.dat ; do
    $PKG/usr/bin/strfile -r -x `basename $file .dat`
  done
)

# Fix perms:
chmod 644 $PKG/var/lib/bsdgames/phantasia/characs

# Junk:
mkdir -p $PKG/usr/share/dict

# Install the hangman words into the dictionary.  These words
# are also used by another package, but I forget which atm:
zcat $CWD/hangman-words.gz > $PKG/usr/share/dict/words

# OK, I'll try to be nice but the first time you're still $cr3w3d unless you
# installpkg first.
( cd $PKG/var/lib/bsdgames
  find . -type f -exec mv {} {}.new \; )

# Create install script:
mkdir -p $PKG/install
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config var/lib/bsdgames/criblog.new
config var/lib/bsdgames/saillog.new
config var/lib/bsdgames/cfscores.new
config var/lib/bsdgames/atc_score.new
config var/lib/bsdgames/phantasia/gold.new
config var/lib/bsdgames/phantasia/mess.new
config var/lib/bsdgames/phantasia/motd.new
config var/lib/bsdgames/phantasia/void.new
config var/lib/bsdgames/phantasia/monsters.new
config var/lib/bsdgames/phantasia/lastdead.new
config var/lib/bsdgames/phantasia/characs.new
config var/lib/bsdgames/phantasia/scoreboard.new
config var/lib/bsdgames/battlestar.log.new
config var/lib/bsdgames/robots_roll.new
config var/lib/bsdgames/snakerawscores.new
config var/lib/bsdgames/snake.log.new
( cd var/lib/bsdgames
  rm -f criblog.new saillog.new cfscores.new atc_score.new 
  rm -f phantasia/gold.new phantasia/mess.new phantasia/motd.new phantasia/void.new phantasia/monsters.new phantasia/lastdead.new
  rm -f phantasia/characs.new phantasia/scoreboard.new battlestar.log.new robots_roll.new snakerawscores.new snake.log.new )
EOF

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links

