# $Id: PKGBUILD,v 1.8 2009/02/11 17:22:38 thotypous Exp $
# Maintainer: Paulo Matias <matiasΘarchlinux-br·org>

pkgname="oss"
pkgver="4.1_1051"
pkgrel=7
pkgdesc="Open Sound System UNIX audio architecture"
arch=('i686' 'x86_64')
url="http://developer.opensound.com/"
license=('GPL2')
depends=('gcc' 'make' 'kernel-headers' 'module-init-tools' 'libtool' 'sed')
makedepends=('pkgconfig' 'gawk' 'gtk2')
conflicts=('oss-linux' 'oss-linux-free' 'oss-testing')
install='oss.install'
source=("http://www.4front-tech.com/developer/sources/stable/gpl/oss-v${pkgver/_*}-build${pkgver/*_}-src-gpl.tar.bz2"
        'oss.rm-init-scripts.patch' 'oss.rc' 'oss.install' 'hg-latest-fixes.diff')


optdepends=('gtk2: for graphical mixer (ossxmix)'
            'hal: for automatic USB audio configuration'
            'libflashsupport-oss: for Flash plugin support')

build() {
    _dir="oss-v${pkgver/_*}-build${pkgver/*_}-src-gpl"

    cd "$srcdir/$_dir"

    # Apply patchs from hg.
    msg "Applying latest fixes from hg."
    patch -p1 < "$srcdir/hg-latest-fixes.diff" || return 1

    cd "$srcdir"

    # Avoid these flags conflicting with OSS build system.
    unset CFLAGS
    unset OSFLAGS
    unset LIBRARIES

    # Remove libflashsupport, as it belongs to a separate package.
    rm -f "$srcdir/$_dir/oss/lib/flashsupport.c" &> /dev/null

    msg "Preparing the build environment."

    if [ ! -d build ]; then
        # Create build directory and configure
        mkdir build && cd build
        "$srcdir/$_dir/configure" --enable-libsalsa=NO || return 1
    else
        # Change to existing build directory
        cd build
    fi

    msg "Building OSS."
    make build || return 1

    msg "Patching init scripts."
    cd "$srcdir/build/prototype"
    rm usr/lib/oss/etc/S89oss
    patch -b -p0 < "$srcdir/oss.rm-init-scripts.patch" || return 1

    msg "Copying files."
    cp -R * "$pkgdir"
    install -D -m755 "$srcdir/oss.rc" "$pkgdir/etc/rc.d/oss"
################################
PREFIX=$startdir/pkg
OSSLIBDIR=$PREFIX/usr/lib/oss
UNAME=2.6.28-LinHES
KERNELDIR=/lib/modules/$UNAME/build
mkdir -p ${PREFIX}/lib/modules/$UNAME/kernel/oss

cd $OSSLIBDIR/build
rm -f $OSSLIBDIR/.cuckoo_installed
   REGPARM=REGPARM
   rm -rf $OSSLIBDIR/objects
   ln -s $OSSLIBDIR/objects.regparm $OSSLIBDIR/objects
   rm -rf $OSSLIBDIR/modules
   ln -s $OSSLIBDIR/modules.regparm $OSSLIBDIR/modules

echo OSS build environment set up for $REGPARM kernels

if ! test -f $OSSLIBDIR/objects/osscore.o
then
    echo Error: OSS core module for $REGPARM kernel is not available in $OSSLIBDIR/objects
    exit 1
fi
OK=1

if ! test -d /lib/modules/$UNAME
then
    echo Error: Kernel directory /lib/modules/$UNAME does not exist
    exit 1
fi

cp -f ../objects/osscore.o osscore_mainline.o

ln -sf ../include/sys/*.h ../include/sys/ossddk .

rm -f Makefile
ln -s Makefile.osscore Makefile

echo Building module osscore

if ! make KERNELDIR=$KERNELDIR> build.list 2>&1
then
    echo Failed to compile OSS
    cat build.list
    exit 2
fi

if ! test -d /lib/modules/$UNAME/kernel/oss
then
  mkdir /lib/modules/$UNAME/kernel/oss
fi

if ! test -d /lib/modules/$UNAME/kernel/oss
then
    echo OSS module directory /lib/modules/$UNAME/kernel/oss does not exist.
    exit 3
fi

if ! ld -r osscore.ko osscore_mainline.o -o /lib/modules/$UNAME/kernel/oss/osscore.ko
then
    echo Linking the osscore module failed
    exit 5
fi

if test -f Module.symvers
then
    #Take generated symbol information and add it to module.inc
    rm -f osscore_symbols.inc
    echo "static const struct modversion_info ____versions[]" >> osscore_symbols.inc
    echo " __attribute__((used))" >> osscore_symbols.inc
    echo "__attribute__((section(\"__versions\"))) = {" >> osscore_symbols.inc
    sed -e "s:^:{:" -e "s:\t:, \":" -e "s:\t\(.\)*:\"},:" < Module.symvers >> osscore_symbols.inc
    echo "};" >> osscore_symbols.inc
else
    echo > osscore_symbols.inc
fi

#depmod -a

for n in ../modules/*.o
do
    N=`basename $n .o`
    echo Building module $N

    rm -f $N_mainline.o Makefile

    sed "s/MODNAME/$N/" < Makefile.tmpl > Makefile
    ln -s $n $N_mainline.o

    if ! make KERNELDIR=$KERNELDIR > build.list 2>&1
    then
        echo Compiling module $N failed
        cat build.list
        exit 4
    fi

    if ! ld -r $N.ko $N_mainline.o -o /lib/modules/$UNAME/kernel/oss/$N.ko
    then
        echo Linking $N module failed
        exit 6
    fi

    rm -f $N_mainline.o
    make clean

done
rm -f Makefile

cp -rp /lib/modules/$UNAME/kernel/oss/*  ${PREFIX}/lib/modules/$UNAME/kernel/oss/




















































}