#!/bin/bash
#
# Installs and runs nginx as an Ubuntu (or Debian) system daemon, and adds our
# nginx-meshcat-proxy.conf as a site to be served.
#
# This is intended for use by pydrake.geometry.SetupMeshcat() to configure a
# Deepnote notebook to allow for MeshCat traffic to flow. Refer to the
# implementation in bindings/pydrake/_geometry_extra.py for details.

set -euo pipefail

# Install nginx; disable its default site.
if [[ $(dpkg-query -W -f'${db:Status-Abbrev}' nginx-light) != "ii " ]]; then
  apt-get update
  apt-get install -y --no-install-recommends nginx-light
fi
rm -f /etc/nginx/sites-enabled/default

# Enable our meshcat site.
my_dir="$(cd "$(dirname "${BASH_SOURCE}")" && pwd)"
site="/etc/nginx/sites-available/deepnote-meshcat-proxy"
cp "${my_dir}/nginx-meshcat-proxy.conf" "${site}"
ln -sf "${site}" /etc/nginx/sites-enabled/
service nginx start
