#!/bin/bash

MIKBILL_DIR="/var/www/mikbill"
ADMIN_CONF="admin/app/etc/config.xml"

if [[ -f "$MIKBILL_DIR/$ADMIN_CONF" ]]; then
	xml_config=`cat $MIKBILL_DIR/$ADMIN_CONF`

	host=$(grep -oPm1 "(?<=<host>)[^<]+" <<< "$xml_config")
	username=$(grep -oPm1 "(?<=<username>)[^<]+" <<< "$xml_config")
	password=$(grep -oPm1 "(?<=<password>)[^<]+" <<< "$xml_config")
	database=$(grep -oPm1 "(?<=<dbname>)[^<]+" <<< "$xml_config")

	radius_name="radiusd"
	dhcp_name="dhcpd"
	if  [[ -d "/etc/freeradius" ]]; then
		radius_name="freeradius"
		dhcp_name="isc-dhcp-server"
	fi

	mysql -h $host -u$username -p$password $database --execute="UPDATE system_options SET \`value\`='/bin/systemctl restart $radius_name' WHERE \`key\`='radiusd_path'"
	mysql -h $host -u$username -p$password $database --execute="UPDATE system_options SET \`value\`='/bin/systemctl restart $dhcp_name' WHERE \`key\`='DHCPD_RESTART'"
else
	echo "config not found"
fi
