#!/bin/sh
#
# SlackBuild for FOX
# http://www.fox-toolkit.org/
# By SukkoPera <enjoy.the.silence@iol.it>
# 27/08/2004
#
# Thanks a lot to CAT for his Slackware package cration
# guide (http://www.slacky.it/misto/tutorial/spunleashed.txt)
#
# Check out
# - http://www.sukkopera.tk
# - http://www.slacky.it
# - http://www.nyft.org


# Get the current and temporary directories
CWD=`pwd`
if [ "$TMP" = "" ]; then
 TMP=/tmp/pkg
fi

# Set up working directories
if [ ! -d $TMP ]
then
	mkdir -p $TMP 
fi

# Some useful variables about the package
NAME=fox
PKG=$TMP/package-$NAME
VERSION=1.2.9
ARCH=i486
BUILD=1suk
SOURCEARCH=$NAME-$VERSION

# Print a welcome screen
echo "+-----------+"
echo "| $NAME-$VERSION |"
echo "+-----------+"


if [ -d $PKG ]
then
	# Clean up a previous build
	#rm -rf $PKG
	#mkdir -p $PKG
	echo "Previous package build directory found, please remove it and"
        echo "restart the SlackBuild script: $PKG"
	exit 1
else
	mkdir -p $PKG
fi

for i in
do
	echo "--- Creating directory $PKG/$i"
	mkdir -p $PKG/$i
done

# Decompress
echo "------------------------- Uncompressing source -------------------------"
cd $TMP
tar zxvf $CWD/$SOURCEARCH.tar.gz
#mv $SOURCEARCH $NAME-$VERSION
cd $NAME-$VERSION


# Build
echo "------------------------------ Configuring -----------------------------"
CFLAGS="-O2 -march=i486 -mcpu=i686" \
CXXFLAGS="-O2 -march=i486 -mcpu=i686" \
	./configure --prefix=/usr \
	--with-x \
	--with-xft \
	--with-xcursor \
	--enable-release \
	--enable-cups \
	--with-opengl


echo "------------------------------- Patching -------------------------------"
# Needed because html documentation installation seems broken and will
# spread files here and there
sed -i "s/\$(datadir)\/doc\/fox-1.2\/html/\$(prefix)\/doc\/$NAME-$VERSION\/html/" doc/Makefile
sed -i "s/\$(prefix)\/fox\/html\/art/\$(prefix)\/doc\/$NAME-$VERSION\/html\/art/" doc/art/Makefile
sed -i "s/\$(prefix)\/fox\/html\/screenshots/\$(prefix)\/doc\/$NAME-$VERSION\/html\/screenshots/" doc/screenshots/Makefile


echo "------------------------------ Compiling -------------------------------"
make
res=$?
if [ $res -ne 0 ]
then
	# make failed, we cannot continue
	exit $res
fi

echo "------------------------------ Installing ------------------------------"
make install DESTDIR=$PKG
res=$?
if [ $res -ne 0 ]
then
	# make install failed, we cannot continue
	exit 1
fi

# Doc
echo "----------------- Copying documentation and other files ----------------"
mkdir -p $PKG/usr/doc/$NAME-$VERSION
for i in ADDITIONS AUTHORS INSTALL LICENSE* README TRACING
do
#        cat $i | gzip > $PKG/usr/doc/$NAME-$VERSION/$i.gz
        cp -a $i $PKG/usr/doc/$NAME-$VERSION/
done

# Gzip man pages
find $PKG/usr/man -name "*.[123456789]" -exec gzip -9 {} \;

# SlackBuild stuff
mkdir -p $PKG/usr/doc/$NAME-$VERSION/slackbuild
cp $CWD/$0 $CWD/slack-desc $PKG/usr/doc/$NAME-$VERSION/slackbuild


# Strip binaries
echo "--------------------------- Stripping binaries -------------------------"
find $PKG -type f | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

# Set permissions
echo "--------------------------- Setting permissions ------------------------"
chown -R root.bin $PKG/usr/bin
chown -R root.root $PKG/usr/doc
chmod -R 755 $PKG/usr/doc/*
find $PKG/usr/doc -type f -exec chmod 644 {} \;
#chown -R root.root $PKG/usr/share
#chmod -R 755 $PKG/usr/share/*
#find $PKG/usr/share -type f -exec chmod 644 {} \;

# Copy Slackware package files
echo "--------------------- Copying Slackware package files ------------------"
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
#cat $CWD/slack-required > $PKG/install/slack-required

# Create package
echo "---------------------------- Creating package --------------------------"
echo "Creating package"
cd $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

# Clean up
if [ "$1" = "--cleanup" ]; then
	echo "---------------------- Cleaning up working directory -------------------"
	rm -rf $TMP/$NAME-$VERSION
	rm -rf $PKG
fi

# Package created
echo "Package creation finished!"
