#! /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 xzf squashfs2.2-r2.tar.gz
    cd squashfs2.2-r2/squashfs-tools
    ed -s Makefile >/dev/null <<EOF
/^CFLAGS :=/
s/ -g/ $CFLAGS/
w
q
EOF
    make
    strip -R .note -R .comment mksquashfs
    sudo cp mksquashfs /usr/local/bin/mksquashfs2
    cd ../..
    rm -rf squashfs2.2-r2
}

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