#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage           : http://connect.creativelabs.com/openal/default.aspx
# source location    : http://connect.creativelabs.com/openal/Downloads/openal-soft-1.8.466.bz2
# last revised       : 12.9.2009
# dep                : 

softname='openal'
softnamesrc='openal-soft'
softversion='1.8.466'
packageversion='1fb'

arch='i486'
cpu='i686'

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softnamesrc-$softversion.bz2"
src="$buildir/$softnamesrc-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://connect.creativelabs.com/openal/Downloads/$softnamesrc-$softversion.bz2

# prepare the build result directory
dest="$buildir/$package"
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
      |-----handy-ruler------------------------------------------------------|
openal: openal (Open source OpenAL 3D audio API)
openal:
openal: OpenAL is a cross-platform software implementation of the
openal: OpenAL 3D audio API. It's built off of the open-sourced
openal: Windows version available originally from the SVN repository at
openal: openal.org
openal: This library is meant as a compatible update/replacement to the OpenAL
openal: Sample Implementation (the SI).
openal:
openal:
openal:
EOF

# check if source is present
if [ -f $softnamesrc-$softversion.bz2 ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar xvf $srcpkg
cd $src

# build
    cmake . -DCMAKE_INSTALL_PREFIX:PATH=/usr \
			-DCMAKE_CXX_FLAGS="-02 -march=i686 -mtune=i686" \
			-DCMAKE_C_FLAGS="-02 -march=i686 -mtune=i686" || return 1
    
    make
    make install DESTDIR=$dest

#Some apps expect to have libopenal.so.0
ln -s libopenal.so.1 $startdir/pkg/usr/lib/libopenal.so.0

#Some apps expect openal_sof and not openal for pkgconfig
ln -s openal.pc $startdir/pkg/usr/lib/pkgconfig/openal_soft.pc

# add 'default' files
    mkdir -p $dest/usr/doc/$softname-$softversion
    cp -a \
      COPYING \
      $dest/usr/doc/$softname-$softversion

# strip executables
( 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
)

# compress man pages
( cd $dest/usr/man
  find . -name "*.?" | xargs gzip -9
)

# SalixBuild
cp $buildir/build-$softname.sh $dest/usr/src/$softname-$softversion

# set target permissions
chown -R root:root $dest

cd $dest
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 {} \;

# real packaging work:
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
cd $buildir
echo -e "" > $package.dep

# src file
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/l/openal/build-openal.sh > $package.src
echo $source >> $package.src

# erase
rm -rf $src
rm -rf $dest

# log ;)
) 2>&1 | tee "$softname-$softversion-build.log"
