#!/bin/sh
#
# License: Copyright 2007 SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Starting/stopping mDNS for Bonsai discovery
# Description: Starting/stopping mDNS for Bonsai discovery
### END INIT INFO
# chkconfig: 235 91 09
#

# Init script information
INIT_NAME=raperca-mdns
DESC="Bonsai Bonjour services"

# config / daemon
DAEMON1=/usr/sbin/avahi-daemon
BASENAME1=avahi-daemon
SAFEINDICATOR=/var/run/raperca.safe
CONFWEB=/etc/avahi/services/raperca-web.service
CONFPLAYER=/etc/avahi/services/raperca.service
CONFIM=/etc/avahi/services/raperca-im.service
RCMAIN=/usr/share/resources/default/init/main
RCRAPERCA=/usr/share/resources/default/init/raperca

# Load init script configuration
[ -f /etc/default/$INIT_NAME ] && . /etc/default/$INIT_NAME

# Load resources
[ -f "$RCMAIN" ] && . "$RCMAIN"
[ -f "$RCRAPERCA" ] && . "$RCRAPERCA"

# Source the init script functions
. /etc/init.d/init-functions

# Verify daemons are installed
[ "$1" = "status" ] && NFOUND=4 || NFOUND=5
test -f $DAEMON1 || exit $NFOUND

update_services() {
    local mode sn fw netapion netapiport name model
    if [ -f $SAFEINDICATOR ]; then
	mode="safe"
    else
	mode="normal"
    fi
    if [ -e /etc/spinetix/configured ]; then
	configured="yes"
    else
	configured="no"
    fi
    sn="$(sed -ne '/^Serial\s\+/{s,.*:\s\+,,p}' /proc/cpuinfo)"
    if [ -z "$sn" -o "$sn" = "000000000000" -o "$sn" = "0000000000000000" ]; then
	sn="$(sed -e 's/://g' /sys/class/net/eth0/address)"
    fi
    fw="$(sed -e 's/.*[[:space:]]version[[:space:]]\+\([^[:space:]]\+\)[[:space:]]\+build[[:space:]]\+\([^[:space:]]\+\)/\1-\2/' /etc/spinetix-release)"
    netapion="$(sed -ne '/\bremoteClientsEnabled=/{s/.*\bremoteClientsEnabled=['\''"]\([^'\''"]*\).*/\1/;p}' /etc/raperca/spxconfig.xml)"
    netapiport="$(sed -ne '/\bsharedVarPort=/{s/.*\bsharedVarPort=['\''"]\([^'\''"]*\).*/\1/;p}' /etc/raperca/spxconfig.xml)"
    name="$(sed -ne '/\bdeviceName=/{s/.*\bdeviceName=['\''"]\([^'\''"]*\).*/\1/;p}' /etc/raperca/spxconfig.xml)"
    [ -n "$name" ] || name="$sn"
    [ -n "$netapiport" ] || netapion="false"
    model="$rc_model"
    type_player="$rc_dns_sd_type_player"
    type_im="$rc_dns_sd_type_im"

    ss="s!@@name@@!$name!g;s!@@mode@@!$mode!g;s!@@configured@@!$configured!g;s!@@model@@!$model!g;s!@@type_player@@!$type_player!g;s!@@type_im@@!$type_im!g;s!@@sn@@!$sn!g;s!@@fw@@!$fw!g;s!@@im-port@@!$netapiport!g"

    local ret=0

    sed -e "$ss" "$CONFWEB".tmpl > "$CONFWEB" || ret=1
    sed -e "$ss" "$CONFPLAYER".tmpl > "$CONFPLAYER" || ret=1
    if [ "$netapion" = "yes" -o "$netapion" = "true" -o "$netapion" = "on" ]; then
	sed -e "$ss" "$CONFIM".tmpl > "$CONFIM" || ret=1
    else
	rm -f "$CONFIM" || ret=1
    fi

    return $ret
}

remove_services() {
    rm -f "$CONFWEB" "$CONFPLAYER" "$CONFIM"
}

start() {
	local RET ERROR=

	log_status_msg "Starting $DESC: " -n

	log_status_msg "creating config" -n
	update_services
	RET=$?
	if [ $RET -eq 0 ]; then
	    log_status_msg ", " -n
	else
	    log_failure_msg "failed ($RET: $ERROR), " -n
	    return 1
	fi

	log_status_msg "reloading $BASENAME1" -n
	if $DAEMON1 -c; then
	    $DAEMON1 -r
	    RET=$?
	else
	    RET=0
	fi
	if [ $RET -eq 0 ]; then
	    log_success_msg "."
	else
	    log_failure_msg " failed ($RET: $ERROR)."
	    return 1
	fi
	
	# repeat above for each daemon...
	
	log_status_msg ""
	return 0
}

stop () {
	local RET ERROR=

	log_status_msg "Stopping $DESC: " -n
	log_status_msg "removing config" -n
	remove_services
	RET=$?
	if [ $RET -eq 0 ]; then
	    log_status_msg ", " -n
	else
	    log_failure_msg "failed ($RET: $ERROR), " -n
	    return 1
	fi

	log_status_msg "reloading $BASENAME1" -n
	if $DAEMON1 -c; then
	    $DAEMON1 -r
	    RET=$?
	else
	    RET=0
	fi
	if [ $RET -eq 0 ]; then
	    log_success_msg "."
	else
	    log_failure_msg " failed ($RET: $ERROR)."
	    return 1
	fi
	
	# repeat above for each daemon...

	log_status_msg ""
	return 0
}

restart() {
	local RET

	log_status_msg "Restarting $DESC..."
	stop
	sleep 2
	start
	RET=$?

	return $RET
}

tryrestart() {
	local RET

	# This script only changes config files, so tryrestart is same as restart
	restart
	RET=$?

	return $RET
}

#
# if the service does not support reload return code 3 should
# be the result...
#
reload() {
	local RET

	log_status_msg "Reloading $DESC configuration..." -n

	log_status_msg "updating config" -n
	update_services
	RET=$?
	if [ $RET -eq 0 ]; then
	    log_status_msg ", " -n
	else
	    log_failure_msg "failed ($RET: $ERROR), " -n
	    return 1
	fi

	log_status_msg "reloading $BASENAME1" -n
	if $DAEMON1 -c; then
	    $DAEMON1 -r
	    RET=$?
	else
	    RET=0
	fi
	if [ $RET -eq 0 ]; then
	    log_success_msg "."
	else
	    log_failure_msg " failed ($RET: $ERROR)."
	    return 1
	fi

	return 0
}

forcereload() {
	local RET

	reload
	RET=$?
	if [ $RET -ne 0 ]; then
		restart
		RET=$?
	fi
	
	return $RET
}

status() {
	local RET
	
	printstatus $BASENAME1
	RET=$?

	return $RET
}

#
# Everything after this should be the same for all init scripts
#
# See the policy manual for information on actions and return codes.
#

parse() {
	case "$1" in
  start)
			start
			return $?
	;;
  stop)
			stop
			return $?
	;;
		restart)
			restart
			return $?
			;;
		try-restart)
			tryrestart
			return $?
			;;
		reload)
			reload
			return $?
			;;
		force-reload)
			forcereload
			return $?
			;;
		status)
			status
			return $?
	;;
  *)
			echo "Usage: $INIT_NAME " \
			"{start|stop|restart|try-restart|reload|" \
			"force-reload|status}" >&2
	;;
	esac
	
	return 1
}

parse $@

