#! /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 () {
    # FIXME: compile kernel module as well?
    tar xzf cloop_2.639-2.tar.gz
    cd cloop-2.639
    gcc $CFLAGS -o extract_compressed_fs extract_compressed_fs.c -lz
    strip -R .note -R .comment extract_compressed_fs
    sudo cp extract_compressed_fs /usr/local/bin/
    cd advancecomp-1.15
    ./configure --prefix=/usr --disable-bz2
    # configure doesn't honour --disable-bz2!
    sed -i '/^#define USE_BZIP2 1/d' config.h
    make advfs LIBS='-lz'
    strip -R .note -R .comment advfs
    sudo cp advfs /usr/local/bin/create_compressed_fs
    cd ../..
    rm -rf cloop-2.639
}

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