#!/bin/sh

# we run with -x so : works instead of echo for output (not logging)
set -eux

if [ -r kvmsetup.sh.sample ]; then
    cat <<EOF

${PWD} is a top-level source directory.
Not CDing to /source

EOF
else
    cd /source
fi

if [ -f /usr/libexec/ipsec/pluto ]; then
    : ABORT: existing pluto found at /usr/libexec/ipsec/pluto
    exit 1
fi

# we don't trust all makefile dependencies, esp lib/
rm -rf $(make showobjdir) modobj

targets="base module"
echo ">>> $0 'make $targets' on $(hostname) at $(date)" >>compile-log.txt
if make "$@" $targets >>compile-log.txt 2>&1; then
    : 'make $targets' success
else
    status=$?
    tail -20 compile-log.txt
    : 'make $targets' failed: ${status}\; see compile-log.txt for details
    exit ${status}
fi

echo "$0 finished on $(hostname) at $(date)"
