#!/bin/sh

CFG_NAME=avahi

create_config() {
    uci -q get $CFG_NAME
    if [ $? -ne 0 ]; then
        cp -f /barix/config/defaults/$CFG_NAME /barix/config/current/
    fi
}

start() {
    create_config

    # migrate old uci configs
    uci -q get network.avahi
    if [ $? -eq 0 ]; then
        uci -q set avahi.general.enabled=$(uci -q get network.avahi.enabled)
        uci -q del network.avahi
        uci commit
    fi

    AVAHI_ENABLED=$(uci -q get avahi.general.enabled)
    if [[ "$AVAHI_ENABLED" == "true" || "$AVAHI_ENABLED" == "on" ]]; then
        if [ -f /usr/bin/create-discovery-service.sh ] ; then
            /usr/bin/create-discovery-service.sh
        fi

        /etc/init.d/avahi-daemon start
    fi
}

stop() {
    /etc/init.d/avahi-daemon stop
}


# See how we were called.
case "$1" in
  start)
    start
    ;;
    
  stop)
    stop
    ;;

  restart)
    stop
    start
    ;;

  *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

exit $?

