#!/bin/sh
echo "Apply hostname"
hostname="$(/sbin/uci get system.@system[0].hostname)"
echo "$hostname" > /proc/sys/kernel/hostname
echo "Reload network config"
/sbin/reload_config

[ -x /etc/init.d/alfred ] && {
	echo "Reload ALFRED"
	/etc/init.d/alfred restart &
}

echo "Reload routing protocols"

# Workaround until "bmx6 configReload" updates hostname
[ -e /etc/init.d/bmx6 -a -x /etc/init.d/bmx6 ] && {
	bmx6_hostname=$(bmx6 -c jshow status | jsonfilter -e '@.status.name')
	sys_hostname=$(uci get system.@system[0].hostname)
	[ "$bmx6_hostname" == "$sys_hostname" ] && bmx6 -c configReload || {
		/etc/init.d/watchping stop
		/etc/init.d/bmx6 restart
		sleep 1 && /etc/init.d/watchping start
	}
}

[ -e /etc/init.d/bmx7 -a -x /etc/init.d/bmx7 ] && {
	bmx7_hostname=$(bmx7 -c jshow status | jsonfilter -e '@.status.name')
	sys_hostname=$(uci get system.@system[0].hostname)
	[ "$bmx7_hostname" == "$sys_hostname" ] && bmx7 -c configReload || {
		/etc/init.d/watchping stop
		/etc/init.d/bmx7 restart
		sleep 1 && /etc/init.d/watchping start
	}
}

