#!/bin/sh
# Copyright 2019-2026 The Wazo Authors  (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

PATH=/bin:/usr/bin:/sbin:/usr/sbin

usage() {
    cat <<-EOF
	usage: $(basename $0) action
	    action: start or stop to enable/disable slave services
	EOF
}

start_dhcp() {
    # apply replicated configuration
    xivo-create-config
    xivo-update-config

    # fetch updated phone MAC prefixes
    dhcpd-update -dr

    service isc-dhcp-server start
    update-rc.d isc-dhcp-server defaults
}

stop_dhcp() {
    update-rc.d isc-dhcp-server remove
    service isc-dhcp-server stop
}

enable_service() {
    wazo-service status all >/dev/null && exit 0
    start_dhcp
    wazo-confgen asterisk/pjsip.conf --invalidate
    wazo-service enable
    wazo-service start
    wazo-agentd-cli relog all --timeout 60
}

disable_service() {
    wazo-service stop wazo
    wazo-service disable
    stop_dhcp
    wazo-service start
}

case $1 in
    start) enable_service;;
    stop)  disable_service;;
    *) usage;;
esac
