#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://gitweb.compiz-fusion.org/?p=fusion/plugins/3d;a=shortlog
# 
# http://www.slacky.eu
#
#  Modified by amigib <amigib AT gmail DOT com>
#  http://www.amigib.prv.pl

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/compiz-fusion-freewins
NAME=compiz-fusion-freewins
VERSION=${VERSION:-`date +%Y%m%d`}
CHOST=i686
ARCH=${ARCH:-i686}
BUILD=1amg


if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486   -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
#tar zxvf $CWD/compiz3d-db3c51d6c5c0df268fc1ec29a4264ef3d21dbbb3.tar.gz
git clone git://anongit.compiz-fusion.org/users/warlock/freewins
cd freewins/

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 .

# Applico la patch al Makefile (commento la riga 76) per evitare che in fase di compilazione ottenga l'errore seguente: 
# Apply a simple patch to comment on the Makefile row 76 in order to avoid the following error:
# warning: failed to load external entity "/schemas.xslt"
# cannot parse /schemas.xslt
# make: *** [build/compiz-3d.schema] Error 4

#bzcat $CWD/patch.bz2 | patch -p0
make

mkdir -p $PKG/usr/lib/compiz
mkdir -p $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/share/compiz

cp -a build/.libs/libfreewins.so* $PKG/usr/lib/compiz
cp -a plugin.info $PKG/usr/doc/$NAME-$VERSION
cp build/freewins.xml $PKG/usr/share/compiz

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

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 -f1 -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

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

if [ `ls | wc -m` -le 8 ]; then
	echo "No package files found!"
else
	rm -rf $TMP
fi


