#! /bin/bash

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

rebuild () {
    tar xjf libfontenc-1.1.1.tar.bz2
    cd libfontenc-1.1.1
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --enable-static \
	    --with-fontrootdir=/usr/lib/X11/fonts
    make
    cd ..

    tar xjf libXfont-1.4.5.tar.bz2
    cd libXfont-1.4.5
    bunzip2 -c ../libXfont-1.4.3-dietpc-patch.bz2 | patch -p1 -b
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --enable-static --disable-freetype \
	    --disable-bdfformat --disable-snfformat --disable-builtins
    make
    cd ..

    #tar xjf libpciaccess-0.12.1.tar.bz2
    #cd libpciaccess-0.12.1
    #./configure --prefix=/usr --enable-static \
    #	    --with-pciids-path=/usr/share/misc --with-zlib
    #make
    #cd ..

    # FIXME: lib/X11/xkb/rules/base?
    ln -sf base xserver-xorg/lib/X11/xkb/rules/xorg

    tar xjf xorg-server-1.10.6.tar.bz2
    cd xorg-server-1.10.6
    bunzip2 -c ../xorg-server-1.10.6-dietpc-patch.bz2 | patch -p1 -b
    bunzip2 -c ../xorg-server-1.9.3-xnest-fullscreen-patch.bz2 | patch -p1 -b
    bunzip2 -c ../xorg-server-1.10.4-ephyr-fullscreen-noglx-patch.bz2 | patch \
	    -p1 -b
    mkdir -p ../xserver-kdrive-fbdev/lib/xorg ../xephyr/lib/xorg \
	    ../xnest/lib/xorg ../xserver-xorg/lib/xorg
    cp dix/protocol.txt ../xserver-kdrive-fbdev/lib/xorg/
    cp dix/protocol.txt ../xephyr/lib/xorg/
    cp dix/protocol.txt ../xnest/lib/xorg/
    cp dix/protocol.txt ../xserver-xorg/lib/xorg/
    sed -i -e '/^dridriverdir=/s@`.*`@/usr/lib/xorg/modules/dri@' configure
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --disable-static --disable-config-hal \
	    --disable-dmx --disable-glx --enable-xnest --disable-xfake \
	    --disable-xvfb --enable-kdrive --disable-xorg \
	    --with-sha1=libcrypto --with-fontrootdir=/usr/lib/X11/fonts
    ed -s hw/kdrive/fbdev/Makefile >/dev/null <<EOF
/^Xfbdev_LDADD =/
/-lXfont/
s@-lXfont@../../../../libfontenc-1.1.1/src/.libs/libfontenc.a -lz ../../../../libXfont-1.4.5/src/.libs/libXfont.a@
w
q
EOF
    ed -s hw/kdrive/ephyr/Makefile >/dev/null <<EOF
/^Xephyr_LDADD =/
/-lXfont/
s@-lXfont@../../../../libfontenc-1.1.1/src/.libs/libfontenc.a -lz ../../../../libXfont-1.4.5/src/.libs/libXfont.a@
/-lXau -lXdmcp -lXv\W*\$/
s@-lXau -lXdmcp -lXv@/usr/lib/libXv.a@
w
q
EOF
    ed -s hw/xnest/Makefile >/dev/null <<EOF
/^XSERVER_SYS_LIBS =/
s@-lXfont@../../../libfontenc-1.1.1/src/.libs/libfontenc.a -lz ../../../libXfont-1.4.5/src/.libs/libXfont.a@
w
q
EOF
    make
    strip -R .note -R .comment hw/kdrive/fbdev/Xfbdev hw/kdrive/ephyr/Xephyr \
	    hw/xnest/Xnest
    mkdir -p ../xserver-kdrive-fbdev/bin ../xephyr/bin ../xnest/bin
    cp hw/kdrive/fbdev/Xfbdev ../xserver-kdrive-fbdev/bin/
    cp hw/kdrive/ephyr/Xephyr ../xephyr/bin/
    cp hw/xnest/Xnest ../xnest/bin/
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --disable-static --disable-config-hal \
	    --disable-dmx --disable-xnest --disable-xfake --disable-xvfb \
	    --disable-kdrive --enable-xorg --with-sha1=libcrypto \
	    --with-fontrootdir=/usr/lib/X11/fonts
#/^XORG_SYS_LIBS =\W*-lpciaccess/
#s@-lpciaccess@../../../libpciaccess-0.12.1/src/.libs/libpciaccess.a@
    ed -s hw/xfree86/Makefile >/dev/null <<EOF
/^XSERVER_SYS_LIBS =/
s@-lXfont@../../../libfontenc-1.1.1/src/.libs/libfontenc.a -lz ../../../libXfont-1.4.5/src/.libs/libXfont.a@
w
q
EOF
    make
    cd hw/xfree86
    strip -R .note -R .comment Xorg */.libs/*.so \
	    dixmods/extmod/.libs/libextmod.so
    cp Xorg ../../../xserver-xorg/bin/
    mkdir -p ../../../xserver-xorg/lib/xorg/modules/{extensions,linux}
    cp dixmods/.libs/lib{fb,shadow}.so int10/.libs/libint10.so \
	    vbe/.libs/libvbe.so ../../../xserver-xorg/lib/xorg/modules/
    cp dixmods/extmod/.libs/libextmod.so dixmods/.libs/libdbe.so \
	    ../../../xserver-xorg/lib/xorg/modules/extensions/
    cp fbdevhw/.libs/libfbdevhw.so \
	    ../../../xserver-xorg/lib/xorg/modules/linux/
    mkdir -p ../../../xserver-xorg-dri/lib/xorg/modules/extensions
    cp dixmods/.libs/libglx.so dri/.libs/libdri.so dri2/.libs/libdri2.so \
	    ../../../xserver-xorg-dri/lib/xorg/modules/extensions/
    mkdir -p ../../../xserver-xorg-accel/lib/xorg/modules
    cp exa/.libs/libexa.so shadowfb/.libs/libshadowfb.so \
	    vgahw/.libs/libvgahw.so xaa/.libs/libxaa.so \
	    ../../../xserver-xorg-accel/lib/xorg/modules/
    cd ../../..
    #rm -rf xorg-server-1.10.6

    mkdir -p xserver-xorg/lib/xorg/modules/input
    tar xjf xf86-input-keyboard-1.6.0.tar.bz2
    cd xf86-input-keyboard-1.6.0
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --disable-static
    make
    strip -R .note -R .comment src/.libs/kbd_drv.so
    cp src/.libs/kbd_drv.so ../xserver-xorg/lib/xorg/modules/input/
    cd ..
    rm -rf xf86-input-keyboard-1.6.0
    tar xjf xf86-input-mouse-1.7.1.tar.bz2
    cd xf86-input-mouse-1.7.1
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --disable-static
    make
    strip -R .note -R .comment src/.libs/mouse_drv.so
    cp src/.libs/mouse_drv.so ../xserver-xorg/lib/xorg/modules/input/
    cd ..
    rm -rf xf86-input-mouse-1.7.1

    mkdir -p xserver-xorg/lib/xorg/modules/drivers
    tar xjf xf86-video-fbdev-0.4.2.tar.bz2
    cd xf86-video-fbdev-0.4.2
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --disable-static
    make
    strip -R .note -R .comment src/.libs/fbdev_drv.so
    cp src/.libs/fbdev_drv.so ../xserver-xorg/lib/xorg/modules/drivers/
    cd ..
    rm -rf xf86-video-fbdev-0.4.2
    tar xjf xf86-video-vesa-2.3.0.tar.bz2
    cd xf86-video-vesa-2.3.0
    ./configure --prefix=/usr --sysconfdir=/etc/X11 --localstatedir=/var \
	    --datarootdir=/usr/lib --disable-static
    make
    strip -R .note -R .comment src/.libs/vesa_drv.so
    cp src/.libs/vesa_drv.so ../xserver-xorg/lib/xorg/modules/drivers/
    cd ..
    rm -rf xf86-video-vesa-2.3.0
}

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
