#!/bin/sh

# Slackware build script for scid

# written by Marco Pessotto <melmothx@gmail.com>
# public domain

# Modified by Robby Workman <rworkman@slackbuilds.org>

PRGNAM=scid
VERSION=${VERSION:-3.6.26}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM
chmod -R u+w,go+r-w,a-s .
chown -R root:root .

./configure \
  BINDIR="/usr/bin" \
  SHAREDIR="/usr/share/scid" \
  OPTIMIZE="$SLKCFLAGS -fno-rtti -fno-exceptions"

make all_scid  
make strip
make install_scid DESTDIR=$PKG

## Add some stuff if it's present
if [ -f $CWD/scidlet40k.zip ] ; then
    mkdir -p $PKG/usr/share/scid/books
    unzip $CWD/scidlet40k.zip -d $PKG/usr/share/scid/books
fi
if [ -f $CWD/ratings_2008_4.zip ] ; then 
    mkdir -p $PKG/usr/share/scid/data
    unzip $CWD/ratings_2008_4.zip -d $PKG/usr/share/scid/data
fi
if [ -f $CWD/photos2007.zip ] ; then 
    mkdir -p $PKG/usr/share/scid/photos
    unzip $CWD/photos2007.zip -d $PKG/usr/share/scid/photos
fi

# Fix some permissions 
find $PKG/usr/share/scid -type d -exec chmod 0755 {} \;
find $PKG/usr/share/scid -type f -exec chmod 0644 {} \;

# We're going to diverge from upstream a bit in location but not functionality
# This will require users to be in the 'games' group for write permissions
mkdir -p $PKG/var/games/scid 
mv $PKG/usr/share/scid/{bases,books} $PKG/var/games/scid
chown -R root:games $PKG/var/games/scid
find $PKG/var/games/scid -type d -exec chmod 2775 {} \;
find $PKG/var/games/scid -type f -exec chmod 0664 {} \;
( cd $PKG/usr/share/scid
  ln -s ../../../var/games/scid/bases .
  ln -s ../../../var/games/scid/books .
)

# Add a desktop menu entry
mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/scid.desktop > $PKG/usr/share/applications/scid.desktop
cat $CWD/scid.png > $PKG/usr/share/pixmaps/scid.png

## Add the manpage (Thanks to debian)
mkdir -p $PKG/usr/man/man1
( cd $PKG/usr/man/man1
  sed "s%@VERSION@%$VERSION%g" $CWD/scid.1 | gzip -9c > scid.1.gz
  for i in \
    sc_remote scmerge pgnscid sc_spell spliteco sc_eco sc_tree scidpgn pgnfix \
    tkscid sc_epgn sc_addmove tcscid sc_import scidlet scidt ; 
    do ln -s scid.1.gz $i.1.gz
  done
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  ChangeLog CHANGES COPYING help README THANKS TODO \
    $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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:-tgz}