#!/bin/sh # Regenerate configured Telegram channels with tg2zola (once, or in a daily # loop when invoked with --daemon for OpenRC). set -eu CONFIG_FILE="${TG2ZOLA_CONF:-/etc/tg2zola/tg2zola.conf}" CONFIG_DIR="${TG2ZOLA_CONF_DIR:-/etc/tg2zola}" TG2ZOLA_BIN="${TG2ZOLA_BIN:-/usr/bin/tg2zola}" DAEMON=0 if [ "${1:-}" = "--daemon" ]; then DAEMON=1 fi run_once() { if [ ! -r "${CONFIG_FILE}" ]; then printf 'tg2zola-sync: missing readable config %s\n' "${CONFIG_FILE}" >&2 return 1 fi CHANNELS="" OUTPUT_DIR="/var/lib/tg2zola" TG2ZOLA_ARGS="" BASE_URL_TEMPLATE="" RUN_ZOLA="auto" INTERVAL_SECONDS="86400" set -a # shellcheck disable=SC1090 . "${CONFIG_FILE}" set +a if [ -z "${CHANNELS}" ]; then printf 'tg2zola-sync: no channels configured in %s (set CHANNELS)\n' "${CONFIG_FILE}" >&2 return 1 fi mkdir -p "${OUTPUT_DIR}" failed=0 for raw in ${CHANNELS}; do channel="${raw#@}" case "${channel}" in ""|\#*) continue ;; */*|*..*) printf 'tg2zola-sync: skipping invalid channel id: %s\n' "${channel}" >&2 failed=1 continue ;; esac site_dir="${OUTPUT_DIR}/${channel}" mkdir -p "${site_dir}" set -- --channel "${channel}" --site "${site_dir}" --init-site if [ -r "${CONFIG_DIR}/${channel}.toml" ]; then set -- "$@" --config "${CONFIG_DIR}/${channel}.toml" fi if [ -n "${BASE_URL_TEMPLATE}" ]; then case "${BASE_URL_TEMPLATE}" in *%s*) base_url="${BASE_URL_TEMPLATE%%"%s"*}${channel}${BASE_URL_TEMPLATE#*"%s"}" ;; *) base_url="${BASE_URL_TEMPLATE}" ;; esac set -- "$@" --base-url "${base_url}" fi if [ -n "${TG2ZOLA_ARGS}" ]; then # Intentional word splitting for user-supplied CLI flags. # shellcheck disable=SC2086 set -- "$@" ${TG2ZOLA_ARGS} fi printf 'tg2zola-sync: regenerating %s into %s\n' "${channel}" "${site_dir}" if ! "${TG2ZOLA_BIN}" "$@"; then printf 'tg2zola-sync: tg2zola failed for %s\n' "${channel}" >&2 failed=1 continue fi case "${RUN_ZOLA}" in true|1|yes) if ! command -v zola >/dev/null 2>&1; then printf 'tg2zola-sync: RUN_ZOLA=%s but zola is not in PATH\n' "${RUN_ZOLA}" >&2 failed=1 elif ! zola --root "${site_dir}" build; then printf 'tg2zola-sync: zola build failed for %s\n' "${channel}" >&2 failed=1 fi ;; auto) if command -v zola >/dev/null 2>&1; then if ! zola --root "${site_dir}" build; then printf 'tg2zola-sync: zola build failed for %s\n' "${channel}" >&2 failed=1 fi fi ;; false|0|no|"") ;; *) printf 'tg2zola-sync: unknown RUN_ZOLA=%s\n' "${RUN_ZOLA}" >&2 failed=1 ;; esac done return "${failed}" } if [ "${DAEMON}" -eq 0 ]; then run_once exit $? fi STOP=0 SLEEP_PID="" handle_stop() { STOP=1 if [ -n "${SLEEP_PID}" ]; then kill "${SLEEP_PID}" 2>/dev/null || true fi } trap handle_stop TERM INT while [ "${STOP}" -eq 0 ]; do run_once || true if [ "${STOP}" -ne 0 ]; then break fi interval="${INTERVAL_SECONDS:-86400}" case "${interval}" in ''|*[!0-9]*|0) interval=86400 ;; esac sleep "${interval}" & SLEEP_PID=$! wait "${SLEEP_PID}" 2>/dev/null || true SLEEP_PID="" done