#! /bin/bash

trap "echo '*** BUILD FAILED! ***' 1>&2" ERR
set -ex -o pipefail
umask 022
export PATH='/bin:/usr/bin:/usr/local/bin'
export CFLAGS=${CFLAGS:--Os}
export CXXFLAGS=${CXXFLAGS:--Os}

rebuild () {
    xzcat xine-lib-1.1.21.tar.xz | tar xf -
    cd xine-lib-1.1.21
    bunzip2 -c ../xine-lib-1.1.20-fixup.patch.bz2 | patch -p1 -b

    CFLAGS=-O3 PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig \
	    ./configure --prefix=/usr/local --sysconfdir=/etc \
	    --localstatedir=/var --with-xv-path=/usr/lib \
	    --with-w32-path=/usr/local/lib/xine/win32 --disable-freetype \
	    --disable-glu
    make
    cd src/demuxers
    rm -f xineplug_dmx_ogg.la
    make xineplug_dmx_ogg.la VORBIS_LIBS='/usr/local/lib/libvorbis.a -lm' \
	    THEORA_LIBS=/usr/local/lib/libtheora.a \
	    OGG_LIBS=/usr/local/lib/libogg.a \
	    SPEEX_LIBS=/usr/local/lib/libspeex.a
    cd ../libxineadec
    rm -f xineplug_decode_vorbis.la
    make xineplug_decode_vorbis.la VORBIS_LIBS='/usr/local/lib/libvorbis.a \
	    -lm' OGG_LIBS=/usr/local/lib/libogg.a
    rm -f xineplug_decode_speex.la
    make xineplug_decode_speex.la SPEEX_LIBS='/usr/local/lib/libspeex.a \
	    /usr/local/lib/libogg.a -lm'
    cd ../libxinevdec
    # Too hard!
    #rm -f xineplug_decode_gdk_pixbuf.la
    #make xineplug_decode_gdk_pixbuf.la \
    #	    GDK_PIXBUF_LIBS='/usr/lib/libgdk_pixbuf-2.0.a -lm \
    #	    /usr/lib/libgobject-2.0.a /usr/lib/libgmodule-2.0.a \
    #	    /usr/lib/libglib-2.0.a /usr/lib/libtiff.a -lz /usr/lib/libjpeg.a \
    #	    /usr/lib/libpng12.a /usr/lib/libpcre.a'
    rm -f xineplug_decode_theora.la
    # The odd positioning of /usr/local/lib/libogg.a is needed to overcome a
    # link-time ordering problem.
    make xineplug_decode_theora.la THEORA_LIBS='/usr/local/lib/libtheora.a \
	    -lm /usr/local/lib/libogg.a' OGG_LIBS=''
    cd ../audio_out
    # ESD plugin not important?
    #rm -f xineplug_ao_out_esd.la
    #make xineplug_ao_out_esd.la ESD_LIBS='/usr/local/lib/libesd.a \
    #	    /usr/lib/libaudiofile.a -lasound'
    cd ../../misc
    gcc $CFLAGS -Wall -o cdda_server cdda_server.c -ldl
    strip -R .note -R .comment cdda_server
    mkdir -p ../../cdda-server/sbin
    cp cdda_server ../../cdda-server/sbin/
    cd ..
    VER=`awk -F/ '$1 ~ /^XINE_PLUGINDIR =/ {print $NF}' src/Makefile`
    # FIXME: not necessary?
    sudo make install
    sudo strip -R .note -R .comment /usr/local/lib/libxine.so.1 \
	    /usr/local/lib/xine/plugins/$VER/*.so \
	    /usr/local/lib/xine/plugins/$VER/*/*.so
    mkdir -p ../libxine1-minimal/lib \
	    ../libxine1-minimal/share/xine/libxine1/fonts \
	    ../libxine1-minimal/lib/xine/plugins/$VER/post
    cp /usr/local/lib/libxine.so.1 ../libxine1-minimal/lib/
    cp /usr/local/share/xine/libxine1/fonts/{cetus,sans}-20.xinefont.gz \
	    ../libxine1-minimal/share/xine/libxine1/fonts/
    # Postprocessing
    for PLUGIN in goom visualizations; do
	cp /usr/local/lib/xine/plugins/$VER/post/xineplug_post_$PLUGIN.so \
		../libxine1-minimal/lib/xine/plugins/$VER/post/
    done
    # Audio Out
    for PLUGIN in alsa none oss; do
	cp /usr/local/lib/xine/plugins/$VER/xineplug_ao_out_$PLUGIN.so \
		../libxine1-minimal/lib/xine/plugins/$VER/
    done
    # Decoders
    for PLUGIN in a52 dts faad ff lpcm mad mpeg2 real spucc spu sputext vorbis;\
	    do
	cp /usr/local/lib/xine/plugins/$VER/xineplug_decode_$PLUGIN.so \
		../libxine1-minimal/lib/xine/plugins/$VER/
    done
    ARCH=`ipkg -V 0 print_architecture | tail -n 1 | cut -f2 -d' '`
    if [ "$ARCH" = 'i386' ]; then
	for PLUGIN in qt w32dll; do
	    cp /usr/local/lib/xine/plugins/$VER/xineplug_decode_$PLUGIN.so \
		    ../libxine1-minimal/lib/xine/plugins/$VER/
	done
    fi
    # Demuxers
    for PLUGIN in asf audio avi flv matroska mpeg_block mpeg_elem mpeg_pes \
	    mpeg mpeg_ts ogg qt real slave sputext yuv4mpeg2; do
	cp /usr/local/lib/xine/plugins/$VER/xineplug_dmx_$PLUGIN.so \
		../libxine1-minimal/lib/xine/plugins/$VER/
    done
    # Input
    for PLUGIN in cdda dvb dvd file http mms net pnm rtp rtsp stdin_fifo v4l \
	    vcd; do
	cp /usr/local/lib/xine/plugins/$VER/xineplug_inp_$PLUGIN.so \
		../libxine1-minimal/lib/xine/plugins/$VER/
    done
    # Video Out
    for PLUGIN in none xshm xv xxmc; do
	cp /usr/local/lib/xine/plugins/$VER/xineplug_vo_out_$PLUGIN.so \
		../libxine1-minimal/lib/xine/plugins/$VER/
    done

    cd ..
    #rm -rf xine-lib-1.1.21
}

(cd libogg; ./build.sh)
(cd libtheora; ./build.sh)
(cd libvorbis; ./build.sh)
(cd speex; ./build.sh)

while read FN; do
    eval "[ -e $FN ] || rebuild"
done < MANIFEST

ARCH=`ipkg -V 0 print_architecture | tail -n 1 | cut -f2 -d' '`
for CTRLFILE in */CONTROL/control; do
    sed -i -e "s/__ARCH__/$ARCH/g" $CTRLFILE
    sudo ipkg-build -o root -g root `dirname $CTRLFILE`/..
done
