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

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