#!/bin/bash

set -e

if [ -z ${BINDIR+x} -o -z ${RULEDIR+x} ]; then
  echo "This script is meant to be ran by Bazel genrule. Exiting."
  exit 1
fi

. external/qnx_7.0/qnxsdp-env.sh

export QNX_CONFIGURATION="/opt/blackberry/qnx-7.0.0/.qnx"
export QNX_CONFIGURATION_EXCLUSIVE="/opt/blackberry/qnx-7.0.0/.qnx"
export QNXLM_LICENSE_FILE="27007@sun-license03.zooxlabs.com"

# Create a license file directory for the current process.
# This workaround a bug that qcc could deadlock waiting for a lock file.
TMPDIR=$(mktemp -d)
function cleanup {
  rm -fr "${TMPDIR}"
}
trap cleanup EXIT
mkdir "${TMPDIR}/license"
ln -s "$(readlink -f ${QNX_CONFIGURATION_EXCLUSIVE}/license/licenses)" "${TMPDIR}/license/licenses"
export QNX_CONFIGURATION_EXCLUSIVE="${TMPDIR}"

mkifs -r "${BINDIR}/firmware/emu/bsp/emu2v0_qnx7v0/install" firmware/emu/bsp/emu2v0_qnx7v0/src/hardware/startup/boards/imx6x/emu2v0/build "${RULEDIR}/qnx-ifs.bin"
