#!/bin/bash
# Watches the shared voicemail config file (written by the app container's
# VoicemailConfigService whenever an extension is created/edited/deleted)
# and reloads Asterisk's voicemail module automatically on change — this
# replaces the sudoers-based reload approach from the non-Docker deployment,
# since here both containers share a volume instead.

WATCH_FILE=/etc/asterisk-shared/voicemail_pbxapp.conf

while true; do
    inotifywait -e modify,close_write "$WATCH_FILE" 2>/dev/null
    echo "[voicemail-watcher] Change detected, reloading voicemail module..."
    asterisk -rx "voicemail reload" 2>&1 || echo "[voicemail-watcher] Reload failed (Asterisk may still be starting up)"
done
