#! /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 kbuild-2.6.39-dietpc.tar.bz2
    # FIXME: woody needs -I.
    cd kbuild-2.6.39-dietpc
    case "`cat /etc/debian_version 2>/dev/null`" in
    6.0*)
	sed -i -e 's/-lncurses/-lncurses -lgpm/' Makefile
	;;
    wheezy/sid|7.0*)
	sed -i -e 's/-lncurses/-lncurses -ltinfo -lgpm/' Makefile
	;;
    esac
    make nconf
    strip -R .note -R .comment nconf
    sudo cp nconf /usr/local/bin/
    cd ..
    rm -rf kbuild-2.6.39-dietpc
}

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