#!/bin/sh
# Runs KumoYami Server in the foreground, with what the systemd unit (kumoyami.service) gives it: for systems without
# systemd, containers and the tarball (packaging/linux/README.md). Reads /etc/default/kumoyami when there is one.
# Data: DATA_DIR, else /var/lib/kumoyami for the user "kumoyami", else ~/.local/share/kumoyami. Stop it with Ctrl+C or
# SIGTERM.
here=$(cd "$(dirname "$0")" && pwd -P) || exit 1
if [ -r /etc/default/kumoyami ]; then
  set -a
  # shellcheck disable=SC1091 # the owner's file
  . /etc/default/kumoyami
  set +a
fi
if [ -z "${DATA_DIR:-}" ]; then
  if [ "$(id -un)" = kumoyami ]; then DATA_DIR=/var/lib/kumoyami; else DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/kumoyami"; fi
fi
mkdir -p "$DATA_DIR" || exit 1
# bash names the machine in HOSTNAME: only the default file's or KUMOYAMI_HOSTNAME is an address to listen on
listen="${KUMOYAMI_HOSTNAME:-0.0.0.0}"
if [ -r /etc/default/kumoyami ] && grep -q '^HOSTNAME=' /etc/default/kumoyami; then listen="$HOSTNAME"; fi
export DATA_DIR
export HOSTNAME="$listen"
export PORT="${PORT:-4890}"
export NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 KUMOYAMI_PLATFORM=linux
export FFMPEG_PATH="$here/bin/ffmpeg" FFPROBE_PATH="$here/bin/ffprobe" FONTCONFIG_FILE="$here/fonts/fonts.conf"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$DATA_DIR/.cache}"
export PATH="$here/bin:$PATH"
cd "$here/app" || exit 1
exec "$here/node/bin/node" kumoyami.js
