#!/bin/bash

#
# packager         : sebastian <zenwalkuser> seb@seb2.dyndns.org
#
# source location  : http://www.fox-toolkit.org/ftp/fox-1.6.29.tar.gz
#
#
# last revised     : 26/10/2007
#

_error() {
  echo -e $1
  echo -e "\nSee $soft-install.log for errors"
  exit 1
}

soft='fox'
softversion='1.6.30'
packageversion='50.1'

arch='i486'
cpu='i686'
makeopts='-j2'

buildir=$(pwd)
srcpkg="$buildir/$soft-$softversion.tar.gz"
src="$buildir/$soft-$softversion"
srcuri="http://www.fox-toolkit.org/ftp/$soft-$softversion.tar.gz"
package="$soft-$softversion-$arch-$packageversion"
dest="$buildir/$package"

echo "------------------------------------------------"
echo "Build package: $soft $softversion"
echo "------------------------------------------------"

cd $buildir
rm -rf $src
if [ ! -f $srcpkg ]; then
  echo "- Download source..."
  wget --passive --quiet $srcuri || _error "\tDownload failed!"
fi 
echo "- Unpacking source..."
tar -xzf $srcpkg
rm -f $dest.tgz
rm -rf $dest
mkdir -p $dest
chown -R root:root .
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS=$CFLAGS

cd $src
chown -R root:root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

echo "- Configuring package..."
cd $src
    ./configure --prefix=/usr \
    --sysconfdir=/etc \
    --with-xft \
    --localstatedir=/var > ../$soft-install.log

echo "- Compiling package..."
make $makeopts >> ../$soft-install.log 2>&1 || _error "\tCompile failed"
echo "- Install package from temp directory"
make install DESTDIR=$dest >> ../$soft-install.log

echo "- Stripped all binaries and gzipping man pages..."
( cd $dest
  find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
)
( cd $dest/usr/man
  find . -name "*.?" | xargs gzip -9
)

echo "- Copying documentation..."
mkdir -p $dest/usr/doc/$soft-$softversion
cp -af \
  AUTHORS ADDITIONS INSTALL LICENSE LICENSE_ADDENDUM README TRACING $dest/usr/doc/$soft-$softversion

mv $dest/usr/share/doc/fox-1.6/html $dest/usr/doc/$soft-$softversion
rm -fr $dest/usr/share

mkdir -p $dest/install
cat > $dest/install/slack-desc <<END
   |-----handy-ruler------------------------------------------------------|
$soft: FOX - a C++ based toolkit for GUI develepment 
$soft:
$soft: 
$soft: FOX is a C++ based Toolkit 
$soft: for developing Graphical User
$soft: Interfaces easily and effectively.
$soft: 
$soft:
$soft: 
$soft:
$soft: Build for Zenwalk
END

mkdir -p $dest/usr/src/$soft-$softversion
( cd $buildir
cp -a build-$soft.sh $dest/usr/src/$soft-$softversion/
)

#
# update the dynamic linked libs
#
cat << "EOF" > $dest/install/doinst.sh
/sbin/ldconfig
EOF


cd $dest

# creating package
echo "- Creating $soft package..."
makepkg -l y -c y $dest.tgz > /dev/null 2>&1
cd $buildir
md5sum $package.tgz > $dest.md5

echo "- Deleting temporary directory's..."
rm -rf $src
rm -rf $dest

echo "Package created complete."
exit 0
