#!/bin/bash
# Copyright (c) 2000-2015 Synology Inc. All rights reserved.

echo -en "Installing Avahi\t"

AvahiSbinDir="/usr/sbin/"
AvahiEtcDir="/etc"

mkdir -p ${TmpInstDir}/usr/lib
mkdir -p ${TmpInstDir}/etc/avahi
mkdir -p ${TmpInstDir}/usr/syno/etc/rc.sysv
mkdir -p ${TmpInstDir}/usr/lib/systemd/system
mkdir -p ${TmpInstDir}/$AvahiSbinDir
mkdir -p ${TmpInstDir}/etc/init
mkdir -p ${TmpInstDir}${SynoPluginDir}/net/ipv4_change
mkdir -p ${TmpInstDir}${SynoPluginDir}/net/ipv6_change
mkdir -p ${TmpInstDir}${SynoPluginDir}/net/hostname_change
mkdir -p ${TmpInstDir}${SynoPluginDir}/net/mtu_change

# generate avahi-ipv{VER}-change-hook.sh
install -c -m755 Synology/AVA/avahi-ip-change-hook.in ${TmpInstDir}${SynoPluginDir}/net/ipv4_change/avahi-ipv4-change-hook.sh
install -c -m755 Synology/AVA/avahi-ip-change-hook.in ${TmpInstDir}${SynoPluginDir}/net/ipv6_change/avahi-ipv6-change-hook.sh
install -c -m755 Synology/AVA/avahi_hostname_sensitive.sh ${TmpInstDir}${SynoPluginDir}/net/hostname_change/avahi_hostname_sensitive.sh
install -c -m755 Synology/AVA/avahi_mtu_sensitive.sh ${TmpInstDir}${SynoPluginDir}/net/mtu_change/avahi_mtu_sensitive.sh
sed -i s/@IP_CHANGE@/ipv4_change/g ${TmpInstDir}${SynoPluginDir}/net/ipv4_change/avahi-ipv4-change-hook.sh
sed -i s/@IP_CHANGE@/ipv6_change/g ${TmpInstDir}${SynoPluginDir}/net/ipv6_change/avahi-ipv6-change-hook.sh


install -c -m755 avahi-daemon/.libs/avahi-daemon        ${TmpInstDir}/${AvahiSbinDir}
install -c -m755 Synology/AVA/avahi.sh                  ${TmpInstDir}/usr/syno/etc/rc.sysv/avahi.sh
install -c -m644 Synology/AVA/avahi.service             ${TmpInstDir}/usr/lib/systemd/system/avahi.service

install -c -m644 avahi-common/.libs/libavahi-common.so.3    ${TmpInstDir}/usr/lib
install -c -m644 avahi-core/.libs/libavahi-core.so.7        ${TmpInstDir}/usr/lib
install -c -m644 Synology/avahi-daemon.conf                 ${TmpInstDir}/etc/avahi/

if checkPlatformFunction SYNO_PLAT_AVAHI_LOW_RAM; then
    sed -i "s/#cache-entries-max=4096/cache-entries-max=128/g" ${TmpInstDir}/etc/avahi/avahi-daemon.conf
fi

if [ "x$NOSTRIP" != "xNOSTRIP" ]; then
    ${STRIP} ${TmpInstDir}/${AvahiSbinDir}/avahi-daemon
    ${STRIP} ${TmpInstDir}/usr/lib/libavahi-common.so.3
    ${STRIP} ${TmpInstDir}/usr/lib/libavahi-core.so.7
fi

mkdir -p ${TmpInstDir}/${AvahiEtcDir}/dbus-1/system.d/

install -c -m755 avahi-utils/.libs/avahi-browse                 ${TmpInstDir}/${AvahiSbinDir}
install -c -m644 avahi-client/.libs/libavahi-client.so.3        ${TmpInstDir}/usr/lib
install -c -m644 avahi-daemon/avahi-dbus.conf                   ${TmpInstDir}/${AvahiEtcDir}/dbus-1/system.d/
if [ "x$NOSTRIP" != "xNOSTRIP" ]; then
    find ${TmpInstDir}/${AvahiSbinDir} -type f | xargs ${STRIP}
    ${STRIP} ${TmpInstDir}/usr/lib/libavahi-client.so.3
fi

echo ".... [DONE]"

# vim:ft=sh
