#!/bin/bash

# Create volume if it doesn't exist
docker volume create cppbuild-cache

# Directory to copy the executable to
OUTDIR="output"

# Build command to run in the container
BUILD_COMMAND="mkdir -p /app/build && \
        cd /app/build && \
        cmake -G Ninja /app/src && \
        ninja -j$(nproc) && \
        mkdir /app/src/$OUTDIR && \
        cp /app/build/ipdemo /app/src/$OUTDIR/ipdemo && \
        chown -R $(id -u):$(id -g) /app/src/$OUTDIR"

# Run the build in the container
docker run --rm \
    -v cppbuild-cache:/app/build \
    -v $(pwd):/app/src \
    -w /app/src \
    -e CCACHE_DIR=/app/build/.ccache \
    -e CC="ccache gcc" \
    -e CXX="ccache g++" \
    -e LD=mold \
    -e CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) \
    -e NINJA_STATUS="[%f/%t] " \
    cpp-httplib-builder:latest \
    -c "$BUILD_COMMAND"

# Run the executable if it exists
if [ -f $OUTDIR/ipdemo ]; then
    $OUTDIR/ipdemo
fi
