#!/bin/sh
# Heavily based on the Slackware 11.0 SlackBuild
# http://glew.sourceforge.net
# Packagers Andrea Sciucca  ( gohanz at infinito.it)
# http://www.slacky.it

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/glew
NAME=glew
VERSION=1.4.0
ARCH=i686
BUILD=1as

SOURCE=http://prdownloads.sourceforge.net/glew/$NAME-$VERSION-src.tgz

if [ ! -e $NAME-$VERSION-src.tgz ]; 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/$NAME-$VERSION-src.tgz

echo "+---------------------------------+"
echo "| Starting SlackBuild  $NAME-$VERSION |"
echo "+---------------------------------+"

cd $NAME

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 .

make

mkdir -p $PKG/usr/X11R6/include/GL
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/lib

install -s -m 0755 lib/libGLEW.so.$VERSION $PKG/usr/lib
install -m 0644 include/GL/wglew.h $PKG/usr/X11R6/include/GL
install -m 0644 include/GL/glew.h $PKG/usr/X11R6/include/GL
install -m 0644 include/GL/glxew.h $PKG/usr/X11R6/include/GL
install -s -m 0644 lib/libGLEW.a $PKG/usr/lib
install -s -m 0755 bin/glewinfo bin/visualinfo $PKG/usr/bin

( 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/usr/doc/$NAME-$VERSION
cp -a README.txt doc/* $PKG/usr/doc/$NAME-$VERSION

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/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME

	echo "( cd usr/lib ; rm -rf libGLEW.so.1.3 )" >> $PKG/install/doinst.sh
	echo "( cd usr/lib ; ln -sf libGLEW.so.$VERSION libGLEW.so.1.3 )" >> $PKG/install/doinst.sh
	echo "( cd usr/lib ; rm -rf libGLEW.so )" >> $PKG/install/doinst.sh
	echo "( cd usr/lib ; ln -sf libGLEW.so.$VERSION libGLEW.so )" >> $PKG/install/doinst.sh

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l n -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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