#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://pyopengl.sourceforge.net
# Packager Andrea Sciucca   ( gohanz at infinito.it)
# http://www.slacky.it
# Requirements: Setuptools

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/PyOpenGL
NAME=PyOpenGL
VERSION=3.0.0b6
ARCH=${ARCH:-i486}
BUILD=1as

SOURCE=http://garr.dl.sourceforge.net/sourceforge/pyopengl/$NAME-$VERSION.tar.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xvzf $CWD/PyOpenGL-$VERSION.tar.gz

echo -e "\E[0;32m+-----------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+-----------------------------------+\E[0;0m"

cd PyOpenGL-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

python setup.py build  \

python setup.py install --root=$PKG \

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a PKG-INFO $PKG/usr/doc/$NAME-$VERSION

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/pyopengl.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/pyopengl.SlackBuild

cd $PKG
ADD=$(ls /var/log/packages/ | sed -ne 's/\(setuptools\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2,/p') \
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/pyopengl-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
