#!/bin/bash

set -euo pipefail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT="$(basename "${SCRIPT_DIR}")"

rm -rf "${SCRIPT_DIR}/output" 
mkdir -p "${SCRIPT_DIR}/output"

# make sure we have the latest base image.
docker pull gitea.jde.nz/public/dropshell-build-base:latest

# Build with or without cache based on NO_CACHE environment variable
CACHE_FLAG=""
if [ "${NO_CACHE:-false}" = "true" ]; then
    CACHE_FLAG="--no-cache"
fi

docker build \
  ${CACHE_FLAG} \
  -t "${PROJECT}-build" \
  -f "${SCRIPT_DIR}/Dockerfile.dropshell-build" \
  --build-arg PROJECT="${PROJECT}" \
  --build-arg CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:-DEBUG}" \
  --output "${SCRIPT_DIR}/output" \
  "${SCRIPT_DIR}"

