#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

PYTHON_LIB_DIR=`ls -d $INSTALL/usr/lib/python*`

add_user avahi x 495 495 "avahi-daemon" "/var/run/avahi-daemon" "/bin/sh"
add_group avahi 495

add_user avahiautoipd x 496 496 "avahi-autoipd" "/var/lib/avahi-autoipd" "/bin/sh"
add_group avahiautoipd 496

mkdir -p $INSTALL/etc/avahi
  cp $PKG_BUILD/avahi-daemon/avahi-daemon.conf $INSTALL/etc/avahi
  # for some reason avai can fail to start see: http://forums.gentoo.org/viewtopic-p-7322172.html#7322172
    sed -e "s,^.*disallow-other-stacks=.*$,disallow-other-stacks=yes,g" -i $INSTALL/etc/avahi/avahi-daemon.conf
  # disable wide-area
    sed -e "s,^.*enable-wide-area=.*$,enable-wide-area=no,g" -i $INSTALL/etc/avahi/avahi-daemon.conf
  cp $PKG_BUILD/avahi-daemon/hosts $INSTALL/etc/avahi

mkdir -p $INSTALL/etc/avahi/services
#  cp $PKG_BUILD/avahi-daemon/ssh.service $INSTALL/etc/avahi/services

  if [ $SAMBA_SERVER = "yes" ]; then
    if [ -f $PROJECT_DIR/$PROJECT/avahi/samba.service ]; then
      cp $PROJECT_DIR/$PROJECT/avahi/samba.service $INSTALL/etc/avahi/services
    else
      cp $PKG_DIR/config/samba.service $INSTALL/etc/avahi/services
    fi
  fi

  if [ $SFTP_SERVER = "yes" ]; then
    cp $PKG_BUILD/avahi-daemon/sftp-ssh.service $INSTALL/etc/avahi/services
  fi

mkdir -p $INSTALL/etc/dbus-1/system.d
  cp $PKG_BUILD/avahi-daemon/avahi-dbus.conf $INSTALL/etc/dbus-1/system.d

mkdir -p $INSTALL/usr/bin
  cp $PKG_BUILD/avahi-utils/avahi-browse $INSTALL/usr/bin
    ln -sf avahi-browse $INSTALL/usr/bin/avahi-browse-domains
  cp $PKG_BUILD/avahi-utils/avahi-publish $INSTALL/usr/bin
    ln -sf avahi-publish $INSTALL/usr/bin/avahi-publish-address
    ln -sf avahi-publish $INSTALL/usr/bin/avahi-publish-service
  cp $PKG_BUILD/avahi-utils/avahi-resolve $INSTALL/usr/bin
    ln -sf avahi-resolve $INSTALL/usr/bin/avahi-resolve-address
    ln -sf avahi-resolve $INSTALL/usr/bin/avahi-resolve-host-name
  cp $PKG_BUILD/avahi-utils/avahi-set-host-name $INSTALL/usr/bin

mkdir -p $INSTALL/usr/sbin
  cp $PKG_BUILD/avahi-autoipd/avahi-autoipd $INSTALL/usr/sbin
  cp $PKG_BUILD/avahi-daemon/avahi-daemon $INSTALL/usr/sbin
  cp $PKG_BUILD/avahi-dnsconfd/avahi-dnsconfd $INSTALL/usr/sbin

mkdir -p $PYTHON_LIB_DIR/site-packages/avahi
  cp -P $PKG_BUILD/avahi-python/avahi/*.py $PYTHON_LIB_DIR/site-packages/avahi

mkdir -p $INSTALL/usr/share/avahi
  cp $PKG_BUILD/service-type-database/service-types $INSTALL/usr/share/avahi
  cp $PKG_BUILD/avahi-daemon/avahi-service.dtd $INSTALL/usr/share/avahi

mkdir -p $INSTALL/usr/share/dbus-1/interfaces
  cp $PKG_BUILD/avahi-daemon/org.freedesktop.Avahi.*.xml $INSTALL/usr/share/dbus-1/interfaces

