#!/bin/bash
#
# Writes the main Janus config file. The config file is based on the config
# template, and it depends on the values found in /home/tinypilot/settings.yml.
# Note that you have to restart both the Janus and uStreamer systemd services
# after running this script, in order to effectuate the config changes.

# Exit on unset variable.
set -u

# Exit on first error.
set -e

print_help() {
  cat << EOF
Usage: ${0##*/} [--help]
(Re-)writes the Janus configuration, based on the settings.yml file.
  --help   Display this help and exit.
EOF
}

# Parse command-line arguments.
while (( "$#" > 0 )); do
  case "$1" in
    --help)
      print_help
      exit
      ;;
    *)
      >&2 print_help
      exit 1
  esac
done

# Generate and write config file from template.
pushd /opt/tinypilot
. venv/bin/activate
runuser tinypilot \
  --command 'PYTHONPATH=/opt/tinypilot/app ./scripts/render-template' \
  < /usr/share/tinypilot/templates/janus.jcfg.j2 \
  > /etc/janus/janus.jcfg
deactivate
popd
