#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Avahi functions
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

function create_avahi_service {
	service_name=$1
	service_type=$2
	service_protocol=$3
	service_port=$4
	service_description="$5"

	if [ ! -d /etc/avahi ]; then
	    echo $'create_avahi_service: avahi was not installed'
		exit 52925
	fi

	echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->' > /etc/avahi/services/${service_name}.service
	echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> /etc/avahi/services/${service_name}.service
	echo '<service-group>' >> /etc/avahi/services/${service_name}.service
	echo "  <name replace-wildcards=\"yes\">%h ${service_type}</name>" >> /etc/avahi/services/${service_name}.service
	echo '  <service>' >> /etc/avahi/services/${service_name}.service
	echo "    <type>_${service_type}._${service_protocol}</type>" >> /etc/avahi/services/${service_name}.service
	echo "    <port>${service_port}</port>" >> /etc/avahi/services/${service_name}.service
	if [ "$service_description" ]; then
		echo "    <txt-record>$description</txt-record>" >> /tmp/zeronet-blog.service
	fi
	echo '  </service>' >> /etc/avahi/services/${service_name}.service
	echo '</service-group>' >> /etc/avahi/services/${service_name}.service
}

function configure_avahi {
	if grep -Fxq "configure_avahi" $COMPLETION_FILE; then
		return
	fi
	# only enable avahi if we're doing mesh networking
	if [[ $ENABLE_BABEL != "yes" && $ENABLE_BATMAN != "yes" && $ENABLE_CJDNS != "yes" ]]; then
		return
	fi

	${PROJECT_NAME}-mesh-install -f avahi
	if [ ! "$?" = "0" ]; then
		echo $'Failed to install avahi'
		exit 68442
	fi

	if [ $DEFAULT_DOMAIN_NAME ]; then
		sed -i "s|#host-name=.*|host-name=$DEFAULT_DOMAIN_NAME|g" /etc/avahi/avahi-daemon.conf
		sed -i "s|host-name=.*|host-name=$DEFAULT_DOMAIN_NAME|g" /etc/avahi/avahi-daemon.conf
	else
		decarray=( 1 2 3 4 5 6 7 8 9 0 )
		PEER_ID=${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}
		sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
		sed -i "s|host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
	fi

	echo 'configure_avahi' >> $COMPLETION_FILE
}

# NOTE: deliberately there is no "exit 0"
