#!/bin/sh
# set -u
function error () { printf >&2 "%s\n\nPlease see README.md for build instructions.\n" "$1"; exit 1; }

# ===============================
# = Check required dependencies =
# ===============================

for dep in capnp ninja ragel multimarkdown pgrep pkill; do
	which -s "$dep" || error "*** dependency missing: ‘${dep}’."
done

if [[ -e local.rave ]]; then
	echo >&2 "Skipping writing ‘local.rave’: File already exists"
else
	echo >> local.rave 'add FLAGS    "-I/usr/local/include"'
	echo >> local.rave 'add LN_FLAGS "-L/usr/local/lib"'

	for hdr in boost/{crc,variant}.hpp capnp/{message,serialize-packed}.h sparsehash/dense_hash_map; do
		test -f "/usr/local/include/${hdr}" || error "*** dependency missing: ‘/usr/local/include/${hdr}’. Install dependency and/or update ‘local.rave’ with correct path."
	done

	for lib in capnp kj; do
		test -f "/usr/local/lib/lib${lib}."* || error "*** dependency missing: ‘/usr/local/lib/lib${lib}.*’. Install dependency and/or update ‘local.rave’ with correct path."
	done
fi

# ========================
# = Generate build files =
# ========================

if [[ -d "$builddir" ]];
	then bin/rave -crelease -tTextMate -b"$builddir"
	else bin/rave -crelease -tTextMate
fi
