#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://developer.nvidia.com/object/cg_toolkit.html
# Packager Vincenzi Loris - slacky@slacky.it
# Rebuilt by alphamanitine for www.slacky.eu

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package
NAME=cg
VERSION=2.2_Apr2009
ARCH=${ARCH:-i386}
BUILD=2sl
TGZ=Cg-2.2_April2009_x86.tgz
SOURCE=http://developer.download.nvidia.com/cg/Cg_2.0/2.1.0016/$TGZ

if [ ! -e  $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 xvfz $CWD/$TGZ
cd usr
chown -R root:root .
find . \( -perm 777 -o -perm 775 -o -perm 711 \) -exec chmod 755 {} \;
find . \( -perm 700 -o -perm 555 -o -perm 511 \) -exec chmod 755 {} \;
find . \( -perm 666 -o -perm 664 -o -perm 600 \) -exec chmod 644 {} \;
find . \( -perm 444 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;
mkdir -p $PKG/usr/bin
cp -a bin/* $PKG/usr/bin
mkdir -p $PKG/usr/include
cp -a include/* $PKG/usr/include
mkdir -p $PKG/usr/lib
cp -a lib/* $PKG/usr/lib
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a local/Cg/* $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/man
cp -a share/man/* $PKG/usr/man
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 $PKG/usr/man -type f -exec gzip -9 {} \;
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.SlackBuild
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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

