#!/bin/bash
# author           : P.R. faasse (p.faase1@chello.nl) <prf>
# maintainer       : P.R. Faasse
# source location  : www.kde.org
# additional files : none 
# depends upon     : Qt, arts, kdebase, kdelibs
# update history:
# 12-feb-2008: preparing for the upcoming 3.5.9 kde version
# 25-aug-2008: version = 3.5.10 build = 64.1

#
# what package is going to be built
#
softversion='3.5.10'
softname='kdeutils'

#
# a lonesome hook
#
(

echo "-- start -----"

#
# where is the source of the thing
#
srcurl="ftp://ftp.kde.org/pub/kde/stable/$softversion/src/$softname-$softversion.tar.bz2"

#
# ZenWalk standard: CPU type and architecture, as well as the
# version number of ZenWalk this package is made for (on?).
#
# arch = 'i686' -> the compiler uses only 80486 instructions
# tune = 'i686' -> the optimising is done for a PII and above CPU
#
arch='i686'
cpu='i686'
packageversion='64.1'

#
# an extra parameter: The set of packages is part of the build of
# kde-3.5.10. I find little merit in makeing a whole load of diretories
# in /usr/src (destination) for each individual package. I'll put the
# lot of them in /usr/src/kde-3.5.10 instead
#
kdeversion="3.5.10"

#
# the package 'destination'
#
prefix=/usr

#
# building-computer dependent: the number of parallel 'jobs' of 'make'
#
# 'heavy' computer: 6 build-jobs at once
#
#numjobs='-j6'
#
# 'light' computer: 2 build-jobs at once
#
numjobs='-j2'

#
# parameters needed for the build process
#
buildir=$(pwd)
package="$softname-$softversion-$arch-$packageversion"

echo "-- build-time dependencies check -----"

depsok=passed

cd /var/log/packages
 
#
# dependencies check
#
for file in \
  qt-base \
  arts \
  kdelibs \
  kdebase \
; do
   #
   # Check the dependency
   #
   if [ -f $file-?.* ] ; then 
      echo "package: $file is installed: `ls -d $file*`: good ...";
   else
      echo "package: $file not found: please install a $file package first";
      depsok=fail
   fi
   #
done

cd $buildir
 
echo "Dependencies check result: $depsok"
#
# check if all dependencies are there
#
if [[ $depsok == passed ]]; then
   unset $depsok;
else
   exit 1;
fi

#
# do package-specific settings 
#
# This avoids compiling a version number into KDE's .la files:
QTDIR=/usr/lib/qt
export QTDIR
#
# KDE additions:
#
KDEDIR=$prefix
PATH=$PATH:$KDEDIR/bin
MANPATH=$MANPATH:$KDEDIR/man
export KDEDIR PATH MANPATH

#
# prepare the build result directory
#
dest="$buildir/$package"

rm -rf $dest
#
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/bin

#
# the /usr/src part
#
destsrc="$dest/usr/src/kde-$kdeversion"
mkdir -p $destsrc

#
# put slack-desc in place. Either a supplied one, or some default
# text.
#
bbstring=`echo $softname | sed "s/./ /g"`
cat <<EOF > $dest/install/slack-desc
$bbstring|-----handy-ruler------------------------------------------------------|
$softname: Utilities for KDE
$softname: 
$softname: A lot of small utilities for kde. 
$softname:
$softname: 
$softname: 
$softname: 
$softname: 
$softname: 
$softname:
$softname: 
EOF
#
# loop over expanded package source files
#
echo "-- unpacking the source archive -----"

cd $buildir
file=$softname-$softversion.tar.bz2 
srcdir="$(basename $file .tar.bz2)" 
rm -rf $srcdir
tar xf $file
cd $srcdir  
#
# set target permissions
#
chown -R root.root . 
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
#
# configure package for compilation
#
echo "-- configure -----"

export CFLAGS="-O2 -march=$arch -mtune=$cpu"  
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"  
./configure \
  --prefix=$prefix \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --with-ssl-dir=/usr \
  --with-xinerama \
  --disable-debug \
  --disable-libfam \
  --enable-dnotify \
  --program-prefix="" \
  --program-suffix="" \
  || exit 1 
#
# make package
#
echo "-- make -----"
make $numjobs || exit 1
#
# install package to the target directory
#
echo "-- make install -----"
make install DESTDIR=$dest || exit 1
#
# add 'default' files
#
echo "-- adding default files -----"

mkdir -p $dest/usr/doc/$srcdir
cp -a \
   AUTHORS COPYING INSTALL NEWS README TODO \
   $dest/usr/doc/$srcdir 

#
# add some package-rules files
#
echo "-- adding package-rules files -----"

cp "$buildir/build-$softname.sh" \
   "$destsrc/build-$softname.sh"
chmod a+x "$destsrc/build-$softname.sh"
cat <<EOF >"$destsrc/getsrc-$softname.sh"
#
# get-the-source script
#
wget -c $srcurl
EOF

#
# a shell-scrip is more usable when it's executable
#
chmod a+x "$destsrc/getsrc-$softname.sh"

#
# post-build processing: 
# - strip executables 
# - compress man-pages
#
echo "-- pre-packaging configuration -----"

( cd $dest
  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
  for mandir in $(find . -name man -type d) ; do
    if [ -d $mandir ]; then
      ( cd $mandir
        find . -type f -name "*.?" -exec gzip -9 {} \;
      )
    fi
  done
)

#
# real packaging work: 
#
chown -R root.root $dest/$prefix
echo "-- packaging -----"

cd $dest
makepkg -p -l y -c n $dest.txz
(
 echo "-- generating md5 and dep file -----"
 cd $buildir
 md5sum $package.txz >$package.md5
 requiredbuilder -z $package.txz >$package.dep
 echo "$srcurl" >$package.src
)

#
# the other end of the lonesome hook
#
) 2>&1 | tee "$softname-$softversion-info.txt"
