#!/bin/bash
set -eu

usage() { 
    echo "Usage: $0 [-p <zmq port>] [-w <width>] [-h <height>] [-d]" 1>&2; 
    exit 1; 
}

export THEME="light"
while getopts ":p:w:h:d" o; do
    case "${o}" in
        p)
            export ZMQ_PORT=${OPTARG}
            ;;
        w)
            export CAD_WIDTH=${OPTARG}
            ;;
        h)
            export CAD_HEIGHT=${OPTARG}
            ;;
        d)
            export THEME="dark"
            ;;
        *)
            usage
            ;;
    esac
done

echo "[JCV] Creating a Jupyter kernel specification called 'jcv' for this conda environment"
python -m ipykernel install --user --name jcv --display-name jcv

JCV_PATH=~/.jcv
VIEWER_PATH=$(python -c "import os, jupyter_cadquery.viewer.server as c; print(os.path.dirname(c.__file__))")

echo "[JCV] Copying the voila notebook to $JCV_PATH"
mkdir -p $JCV_PATH
cp $VIEWER_PATH/viewer.ipynb $JCV_PATH

echo "[JCV] Signing the volia notebook"
jupyter trust $JCV_PATH/viewer.ipynb

echo "[JCV] Starting Jupyter CadQuery Viewer"
voila --theme $THEME \
    --Voila.ip=0.0.0.0 \
    --show_tracebacks=True \
    --enable_nbextensions=True \
    --VoilaExecutor.kernel_name=jcv \
    --VoilaConfiguration.file_whitelist="favicon.ico" \
    --VoilaConfiguration.file_whitelist=".*\.js" \
    $JCV_PATH/viewer.ipynb



