#!/bin/bash
# author           : P.R. Faasse (faasse@nlr.nl) <prf>
# maintainer       : P.R. Faasse
# source location  : see $srcurl below
# additional files : 
# 
# update history: 
# 44.1 -> 44.2: rebuild with flac-1.1.3 (API change)
# 25-aug-2008: version 3.5.10

#
# what package is going to be built
#
softname='akode'
softversion='2.0.2'
srcarch="30375-akode-2.0.2.tar.bz2"

#
# The ZenWalk package version: indicated for/on what ZenWalk version
# the package has been created
#
# example: packageversion='1Z20' means: 
# 1  -> build 1 = first build, special: 0 = experimental/unsupported build
# Z  -> ZenWalk
# 20 -> ZenWalk version 2.0; 26 -> zenwalk 2.6 
#
packageversion='64.1'

#
# where could you get the source
#
srcurl="http://www.kde-apps.org/CONTENT/content-files/30375-akode-2.0.2.tar.bz2"

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

#
#---------------------------------------------------------------------
# Note: for a rebuild you should normally not have to change anything
# below this comment
#---------------------------------------------------------------------
#

#
# a lonesome 'hook': the other end is at the bottom of the buildfile
# the combination gives me a full log of the build-process in the
# file: "$softname-$softversion-info.txt"
#
(

#
# we're going to do some cwd-hopping: this requires that we keep track
# of the starting-point
#
buildir=$(pwd)

#
# We'll use the SlackWare/ZenWalk database of installed packages: 
# The directory: /var/log/packages keeps a set of all installed 
# ZenWalk packages: one file per installed package
#

echo "-- dependencies check -----"

depsok=passed

cd /var/log/packages
 
#
# 
#
for file in \
	libsamplerate \
; 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

#
# check if the source files archive is present: we can do little without
# it
#
echo "-- source archive check -----"

if [ -r "$srcarch" ]; then 

  echo "Usable source archive: $srcarch found: good ...";
else 
  echo "No source archive: $srcarch found"
  echo "It is not possible to build the package without a source archive"
  echo "file; Please download the source archive from:"
  echo
  echo "$srcurl"
  echo
  exit 1;
fi

#
# start the real work
#
echo "-- start of the package building -----"

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

#
# The architecture that the package is built for:
#
# arch='i486' -> the compiler uses only 80486 instructions
# tune='i686'  -> the optimising is done for a PII and above CPU
#
arch='i686'
tune='i686'

#
# parameters needed for the build process
#
package="$softname-$softversion-$arch-$packageversion"
srcname="$softname-$softversion-noarch-$packageversion"

#
# do package-specific settings 
#

#
# prepare the build result directory
#
dest="$buildir/$package"
#
rm -rf $dest
#
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/sbin
mkdir -p $dest/$prefix/man/man8
#
# prepare the directory for the build-file and assorted other 
# user-build related things
#
destsrc="$dest/usr/src/$softname-$softversion"
mkdir -p $destsrc

#
# put a slack-desc in place
#
bbstring=`echo $softname | sed "s/./ /g"`
cat <<EOF > $dest/install/slack-desc
$bbstring|-----handy-ruler------------------------------------------------------|
$softname: A set of decoders for sound files
$softname:
$softname: 
$softname: 
$softname: 
$softname: 
$softname: 
$softname: 
$softname: 
$softname:
$softname: 
EOF

echo "-- Unpacking the source archive -----"

#
# extract the source archive
#
cd $buildir
srcdir="$softname-$softversion"
rm -rf $srcdir
tar xf $srcarch
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=$tune"  
export CXXFLAGS="-O2 -march=$arch -mtune=$tune"  

#
# the thing has no configure script: it's too simple for that....
#
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir="/usr/man" \
  --with-ffmpeg=no \
  --program-prefix="" \
  --program-suffix="" 

#
# patching
#
patch -p0 <<EOF
*** akode/lib/void_sink.bak	2009-11-12 13:48:03.557909700 +0100
--- akode/lib/void_sink.cpp	2009-11-12 13:48:30.743909132 +0100
***************
*** 19,24 ****
--- 19,25 ----
  */
  
  #include <config.h>
+ #include <stdlib.h>
  
  #include <audioframe.h>
  #include "void_sink.h"
EOF

patch -p0 <<EOF
*** akode/lib/auto_sink.bak	2009-11-12 13:51:51.196909202 +0100
--- akode/lib/auto_sink.cpp	2009-11-12 13:52:08.556910524 +0100
***************
*** 22,27 ****
--- 22,28 ----
  #include "auto_sink.h"
  
  #include <iostream>
+ #include <stdlib.h>
  
  namespace aKode {
EOF

patch -p0 <<EOF
*** akode/lib/player.bak	2009-11-12 13:55:39.455909221 +0100
--- akode/lib/player.cpp	2009-11-12 13:55:58.563911231 +0100
***************
*** 21,26 ****
--- 21,27 ----
  #include <pthread.h>
  #include <semaphore.h>
  #include <assert.h>
+ #include <string.h>
  
  #include "audioframe.h"
  #include "audiobuffer.h"
EOF

patch -p0 <<EOF
*** akode/lib/magic.bak	2009-11-12 13:57:23.061909970 +0100
--- akode/lib/magic.cpp	2009-11-12 13:57:40.694910224 +0100
***************
*** 22,27 ****
--- 22,28 ----
  #include "magic.h"
  #include "file.h"
  #include <iostream>
+ #include <string.h>
  using std::cerr;
  
  namespace aKode {
EOF

patch -p0 <<EOF
*** akode/akodeplay/akodeplay.bak	2009-11-12 13:59:17.537907854 +0100
--- akode/akodeplay/akodeplay.cpp	2009-11-12 14:01:44.962909667 +0100
***************
*** 19,24 ****
--- 19,25 ----
  */
  
  #include <iostream>
+ #include <stdlib.h>
  
  #include "../lib/akodelib.h"
  #include "../lib/player.h"
EOF

#
# 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 from the package source
#
echo "-- adding default files -----"

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

# 
# as per package rules: add the build-file itself
#
echo "-- adding package-rules files -----"

cp "$buildir/build-$softname.sh" \
   "$destsrc/build-$softname.sh"
chmod a+x "$destsrc/build-$softname.sh"
#
# generate an executable script that can be used to get the source
#
cat <<EOF > "$destsrc/getsrc.sh"
# 
# get the source(s) script
#
wget -c $srcurl
EOF

#
# it works easier if this file is actually executable :-)
#
chmod a+x "$destsrc/getsrc.sh"

#
# post-build processing: 
# - strip executables 
# - compress man-pages
#
echo "-- pre-package processing files -----"

( 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
)
(
 cd $buildir
 echo "-- computing the md5 sum -----"
 md5sum $package.txz >$package.md5
 echo "-- determining the dependencies -----"
 requiredbuilder -z $package.txz >$package.dep
)

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

