#!/bin/bash

set -e

# Set the path to your FreeCAD executable here
FREECAD=~/dev/FreeCAD-asm3-Daily-Conda-Py3.10-20221128-glibc2.12-x86_64.AppImage

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

rm -rf ./dist

# Check script syntax before starting freecad
python3 -m py_compile tools/export-all-parts.py

# Export all versioned files
mkdir -p dist/FCStd
git archive HEAD . | tar -x -C dist/FCStd

# Start freecad to run the script. We must start freecad with GUI (otherwise we can't export colors). We start it hidden in a virtual framebuffer (xvfb) so that it can run cleanly in the background.
xvfb-run $FREECAD tools/export-all-parts.py

# Concatenate all PDFs in a single file
pdfunite dist/PDF/tubes/* dist/PDF/tubes.pdf