#!/usr/bin/env bash
set -exo pipefail
prefix=$(echo $PWD | sed -e "s|^$BIZ_ROOT/*||g" -e "s|/|.|g")
if [[ "$prefix" == "" ]]
then
  # TODO: make this accept any number of build targets
  target="$1"
else
  target="$prefix.$1"
fi
log=$(mktemp)
nix-build \
    -o "$BIZ_ROOT/_bild/$target" \
    $BIZ_ROOT/default.nix \
    --attr "$target" \
    --show-trace \
    2>&1 \
    | tee $log
if [[ ${PIPESTATUS[0]} != 0 ]]
then
    o=$(gawk "match(\$0, /'(.*)'\sfailed/, a) {print a[1]}" $log \
        | head -n 1)
    nix log $o
fi