#!/bin/sh
# Let Asterisk read the phone system out of the database.
#
# Everything nuxpbx knows lives in PostgreSQL, and Asterisk reads it through
# res_config_odbc: no endpoint, no trunk registration and no queue exists in
# any file. Debian's own modules.conf ships with
#
#     noload => res_config_odbc.so
#
# which is a reasonable default for a plain Asterisk and fatal for this one:
# the machine comes up, answers on every port, and serves nothing, because
# every realtime lookup ends in "the engine is not available". Nothing else
# says so, and the first anybody hears of it is a phone that will not
# register.
#
# So the line is commented out here, once, and the reason written beside it.
# Run at the first boot and again on every upgrade, because installations
# made before this existed are in exactly that state.
#
# The exit status says whether anything changed, and nothing else: zero only
# when this run rewrote the file. The postinst restarts Asterisk on that
# answer, and answering zero for "there was nothing to do" cut every call in
# progress on every ordinary package upgrade.
set -eu

conf=${1:-/etc/asterisk/modules.conf}
# No such file is nothing changed, not success.
[ -f "$conf" ] || exit 1

changed=1

# Only what is ours to change: a noload line, turned into a comment. A
# machine where somebody has already dealt with it is left alone.
if grep -qE '^[[:space:]]*noload[[:space:]]*=>[[:space:]]*res_config_odbc\.so' "$conf"; then
	tmp=$(mktemp)
	sed -E 's|^([[:space:]]*noload[[:space:]]*=>[[:space:]]*res_config_odbc\.so.*)$|; nuxpbx keeps the phone system in the database and reads it through this\n; module, so it cannot be left out. The line as Debian ships it:\n;\1|' \
		"$conf" >"$tmp"
	cat "$tmp" >"$conf"
	rm -f "$tmp"
	echo "asterisk: the database is readable again (res_config_odbc was switched off in $conf)"
	changed=0
fi

# And one voicemail. asterisk-modules on Debian and Ubuntu ships the ODBC
# variant beside the file one, and the packaged modules.conf autoloads both.
# Both register VoiceMail, the first to load wins, the other is declined, and
# which is first is up to the order the directory lists them in. Where the
# ODBC one won, every change nuxpbx applied failed on "module reload
# app_voicemail: Module not initialized". nuxpbx keeps voicemail in files and
# reloads app_voicemail, so the other variants stay off, as the Alpine
# modules.conf has them. Taking effect needs a restart, which the exit status
# asks for.
for variant in app_voicemail_odbc.so app_voicemail_imap.so; do
	grep -qE "^[[:space:]]*noload[[:space:]]*=>[[:space:]]*$variant" "$conf" && continue
	# Without a [modules] section there is nowhere to put it, and a change
	# reported but not made would restart Asterisk on every upgrade.
	grep -q '^\[modules\]' "$conf" || break
	tmp=$(mktemp)
	awk -v line="noload => $variant" '{ print } /^\[modules\]/ && !done { print line; done = 1 }' "$conf" >"$tmp"
	cat "$tmp" >"$conf"
	rm -f "$tmp"
	echo "asterisk: $variant no longer competes with app_voicemail.so ($conf)"
	changed=0
done

exit $changed
