#! /bin/bash
# This script configures one or two rtpproxy servers
# The second rtpproxy is sometimes used for communication with the provider's SBC via
# a separate network.
# Leonid Fainshtein <leonid.fainshtein@xorcom.com>
# Xorcom Ltd 2004-2026


LXDINTERFACE="lxdbr0"
minport_ext="10000"
maxport_ext="16000"
minport_trunk="16002"
maxport_trunk="20000"

me=$(basename "$0")

die() {
	echo >&2 "$@"
	exit 1
}

find_ip_of() {
	local netif="$1"
	ip -4 address show "$netif" >/dev/null || die "E: Wrong network interface '$netif'"
	ip -4 addr show "$netif" | grep -m 1 -Po 'inet \K[\d.]+'
}

rtpproxy_config() {
	local fname="/etc/default/rtpproxy"
	local ctrlsock_ext="udp:127.0.0.1:22222"
	local ctrlsock_trunk="udp:127.0.0.1:22223"

	OPTS="-m $minport_ext -M $maxport_ext -s $ctrlsock_ext -l $bridge_ip/$ip_ext"
	if [ -n "$adv_ext" ]; then
		OPTS=$OPTS" -A $bridge_ip/$adv_ext"
	fi
	echo OPTIONS="'$OPTS'"  > "$fname.tmp"

	if [ -n "$ip_trunk" ]; then
		OPTS="-m $minport_trunk -M $maxport_trunk -s $ctrlsock_trunk -l $bridge_ip/$ip_trunk"
		if [ -n "$adv_trunk" ]; then
			OPTS=$OPTS" -A $bridge_ip/$adv_trunk"
		fi
		echo OPTIONS2="'$OPTS'"  >> "$fname.tmp"

		cat <<- 'EOF' > /lib/systemd/system/rtpproxy2.service
			[Unit]
			Description=A symmetric RTP proxy second instance
			After=network.target

			[Service]
			Type=notify
			User=rtpproxy
			Group=rtpproxy
			EnvironmentFile=/etc/default/rtpproxy
			PIDFile=/var/run/rtpproxy/rtpproxy2.pid
			ExecStart=/usr/bin/rtpproxy -f -p /var/run/rtpproxy/rtpproxy2.pid $OPTIONS2

			[Install]
			Also=rtpproxy2.socket
			WantedBy=multi-user.target
		EOF

		cat <<- EOF > /lib/systemd/system/rtpproxy2.socket
			[Unit]
			Description=RTPproxy controlling socket for second instance

			[Socket]
			ListenStream=127.0.0.1:22223
			Accept=false

			[Install]
			WantedBy=sockets.target
		EOF

		mkdir -p /lib/systemd/system/rtpproxy2.service.d
		cat <<- EOF > /lib/systemd/system/rtpproxy2.service.d/cpbx-host.conf
			[Service]
			LimitNOFILE=20000
			[Unit]
			After=wait-for-lxdbr0.service
			Wants=wait-for-lxdbr0.service
		EOF

		systemctl enable rtpproxy2.service rtpproxy2.socket
	fi
	mv -f "$fname.tmp" "$fname"

	echo "Configured '$fname'"
	cat "$fname"
}

rtpengine_config() {
	local fname="/etc/rtpengine/rtpengine-mt.conf"

	# Configure the /etc/rtpengine/rtpengine-mt.conf file.
	if [ -n "$adv_ext" ]; then
		$adv_ext="advertised = $adv_ext"
	fi
	if [ -n "$adv_trunk" ]; then
		$adv_trunk="advertised = $adv_trunk"
	fi
	cat <<- EOF > $fname.tmp
		[rtpengine]

		table = 0
		interfaces-config = interface

		listen-ng  = localhost:22223
		listen-cli = localhost:2224

		timeout        = 60
		silent-timeout = 3600
		tos            = 184
		delete-delay   = 0

		port-min = 10000
		port-max = 20000

		dtls-passive = true

		[interface-ext]
		address = $ip_ext
		$adv_ext

		[interface-int]
		address = $bridge_ip
	EOF
	if [ -n "$ip_trunk" ]; then
		cat <<- EOF >> $fname.tmp
			[interface-sbc]
			address = $ip_trunk
			$adv_trunk
		EOF
	fi
	mv -f "$fname.tmp" "$fname"

	# Configure the /etc/default/rtpengine-daemon file.
	sed -i  's|CONFIG_FILE=.*$|CONFIG_FILE=/etc/rtpengine/rtpengine-mt.conf|' /etc/default/rtpengine-daemon

	mkdir -p  /lib/systemd/system/rtpengine-daemon.service.d
	cat <<- 'EOF' > /lib/systemd/system/rtpengine-daemon.service.d/cpbx-host.conf
		[Service]
		ExecStart=
		ExecStart=/usr/bin/rtpengine -f -E --no-log-timestamps --pidfile /run/rtpengine/rtpengine-daemon.pid --config-file "$CONFIG_FILE"

		[Unit]
		After=wait-for-lxdbr0.service
		Wants=wait-for-lxdbr0.service
	EOF

	systemctl disable --now rtpengine-recording-daemon.service
	systemctl disable --now rtpengine-recording-nfs-mount.service
	systemctl daemon-reload
	systemctl restart rtpengine-daemon.service
}

[ "$#" -lt 1 ] && die "Usage: $me <external-network-interface>[/<advertize_ip>] [<trunk-network-interface>[/<advertize_ip>]]"
arr=(${1//\// })
if_ext=${arr[0]}
adv_ext=${arr[1]}

if_trunk=""
adv_trunk=""
if [ "$#" -eq 2 ]; then
	arr=(${2//\// })
	if_trunk=${arr[0]}
	adv_trunk=${arr[1]} 
fi

bridge_ip="$(find_ip_of $LXDINTERFACE)"
[ -n "$bridge_ip" ] || die "E: Cannot find IP of '$LXDINTERFACE'"

ip_ext="$(find_ip_of "$if_ext")"
[ -n "$ip_ext" ] || die "E: Cannot find IP of '$if_ext'"

if [ -n "$if_trunk" ]; then
	ip_trunk="$(find_ip_of "$if_trunk")"
	[ -n "$ip_trunk" ] || die "E: Cannot find IP of '$if_trunk'"
fi

# Check what kind of RTP proxy we need to configure.
# If rtpengine is installed then we will configure it. Otherwise, we will try to configure the rtpproxy.
if dpkg -s "rtpengine" >/dev/null 2>&1; then
	rtpengine_config
else
	rtpproxy_config
fi
